Skip to content

Commit

Permalink
Return new instances of types.go gql types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 25, 2024
1 parent 913b3ec commit a42a05c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 53 deletions.
2 changes: 1 addition & 1 deletion internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func setCRDTType(field *ast.FieldDefinition, kind client.FieldKind) (client.CTyp
switch arg.Name.Value {
case "type":
cTypeString := arg.Value.GetValue().(string)
cType, validCRDTEnum := types.CRDTEnum.ParseValue(cTypeString).(client.CType)
cType, validCRDTEnum := types.CRDTEnum().ParseValue(cTypeString).(client.CType)
if !validCRDTEnum {
return 0, client.NewErrInvalidCRDTType(field.Name.Value, cTypeString)
}
Expand Down
46 changes: 32 additions & 14 deletions internal/request/graphql/schema/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,26 @@ type SchemaManager struct {
func NewSchemaManager() (*SchemaManager, error) {
sm := &SchemaManager{}

orderEnum := schemaTypes.OrderingEnum()
crdtEnum := schemaTypes.CRDTEnum()
explainEnum := schemaTypes.ExplainEnum()

commitLinkObject := schemaTypes.CommitLinkObject()
commitObject := schemaTypes.CommitObject(commitLinkObject)
commitsOrderArg := schemaTypes.CommitsOrderArg()
commitsOrderArg := schemaTypes.CommitsOrderArg(orderEnum)

schema, err := gql.NewSchema(gql.SchemaConfig{
Types: defaultTypes(commitObject, commitLinkObject, commitsOrderArg),
Types: defaultTypes(
commitObject,
commitLinkObject,
commitsOrderArg,
orderEnum,
crdtEnum,
explainEnum,
),
Query: defaultQueryType(commitObject, commitsOrderArg),
Mutation: defaultMutationType(),
Directives: defaultDirectivesType(),
Directives: defaultDirectivesType(crdtEnum, explainEnum, orderEnum),
})
if err != nil {
return sm, err
Expand Down Expand Up @@ -117,15 +128,19 @@ func defaultMutationType() *gql.Object {
}

// default directives type.
func defaultDirectivesType() []*gql.Directive {
func defaultDirectivesType(
crdtEnum *gql.Enum,
explainEnum *gql.Enum,
orderEnum *gql.Enum,
) []*gql.Directive {
return []*gql.Directive{
schemaTypes.CRDTFieldDirective,
schemaTypes.ExplainDirective,
schemaTypes.PolicyDirective,
schemaTypes.IndexDirective,
schemaTypes.IndexFieldDirective,
schemaTypes.PrimaryDirective,
schemaTypes.RelationDirective,
schemaTypes.CRDTFieldDirective(crdtEnum),
schemaTypes.ExplainDirective(explainEnum),
schemaTypes.PolicyDirective(),
schemaTypes.IndexDirective(orderEnum),
schemaTypes.IndexFieldDirective(orderEnum),
schemaTypes.PrimaryDirective(),
schemaTypes.RelationDirective(),
}
}

Expand All @@ -147,6 +162,9 @@ func defaultTypes(
commitObject *gql.Object,
commitLinkObject *gql.Object,
commitsOrderArg *gql.InputObject,
orderEnum *gql.Enum,
crdtEnum *gql.Enum,
explainEnum *gql.Enum,
) []gql.Type {
return []gql.Type{
// Base Scalar types
Expand All @@ -164,7 +182,7 @@ func defaultTypes(
// Base Query types

// Sort/Order enum
schemaTypes.OrderingEnum,
orderEnum,

// Filter scalar blocks
schemaTypes.BooleanOperatorBlock(),
Expand All @@ -182,7 +200,7 @@ func defaultTypes(
commitLinkObject,
commitObject,

schemaTypes.CRDTEnum,
schemaTypes.ExplainEnum,
crdtEnum,
explainEnum,
}
}
10 changes: 5 additions & 5 deletions internal/request/graphql/schema/types/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,27 @@ func CommitLinkObject() *gql.Object {
})
}

func CommitsOrderArg() *gql.InputObject {
func CommitsOrderArg(orderEnum *gql.Enum) *gql.InputObject {
return gql.NewInputObject(
gql.InputObjectConfig{
Name: "commitsOrderArg",
Description: OrderArgDescription,
Fields: gql.InputObjectConfigFieldMap{
"height": &gql.InputObjectFieldConfig{
Description: commitHeightFieldDescription,
Type: OrderingEnum,
Type: orderEnum,
},
"cid": &gql.InputObjectFieldConfig{
Description: commitCIDFieldDescription,
Type: OrderingEnum,
Type: orderEnum,
},
request.DocIDArgName: &gql.InputObjectFieldConfig{
Description: commitDocIDFieldDescription,
Type: OrderingEnum,
Type: orderEnum,
},
"collectionID": &gql.InputObjectFieldConfig{
Description: commitCollectionIDFieldDescription,
Type: OrderingEnum,
Type: orderEnum,
},
},
},
Expand Down
82 changes: 50 additions & 32 deletions internal/request/graphql/schema/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const (
FieldOrderDESC = "DESC"
)

var (
// OrderingEnum is an enum for the Ordering argument.
OrderingEnum = gql.NewEnum(gql.EnumConfig{
// OrderingEnum is an enum for the Ordering argument.
func OrderingEnum() *gql.Enum {
return gql.NewEnum(gql.EnumConfig{
Name: "Ordering",
Values: gql.EnumValueConfigMap{
"ASC": &gql.EnumValueConfig{
Expand All @@ -59,8 +59,10 @@ var (
},
},
})
}

ExplainEnum = gql.NewEnum(gql.EnumConfig{
func ExplainEnum() *gql.Enum {
return gql.NewEnum(gql.EnumConfig{
Name: "ExplainType",
Description: "ExplainType is an enum selecting the type of explanation done by the @explain directive.",
Values: gql.EnumValueConfigMap{
Expand All @@ -80,13 +82,15 @@ var (
},
},
})
}

ExplainDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
func ExplainDirective(explainEnum *gql.Enum) *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: ExplainLabel,
Description: "@explain is a directive that can be used to explain the query.",
Args: gql.FieldConfigArgument{
ExplainArgNameType: &gql.ArgumentConfig{
Type: ExplainEnum,
Type: explainEnum,
},
},

Expand All @@ -97,8 +101,10 @@ var (
gql.DirectiveLocationMutation,
},
})
}

PolicyDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
func PolicyDirective() *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: PolicySchemaDirectiveLabel,
Description: "@policy is a directive that can be used to link a policy on a collection type.",
Args: gql.FieldConfigArgument{
Expand All @@ -113,8 +119,10 @@ var (
gql.DirectiveLocationObject,
},
})
}

IndexDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
func IndexDirective(orderingEnum *gql.Enum) *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: IndexDirectiveLabel,
Description: "@index is a directive that can be used to create an index on a type.",
Args: gql.FieldConfigArgument{
Expand All @@ -125,15 +133,17 @@ var (
Type: gql.NewList(gql.String),
},
IndexDirectivePropDirections: &gql.ArgumentConfig{
Type: gql.NewList(OrderingEnum),
Type: gql.NewList(orderingEnum),
},
},
Locations: []string{
gql.DirectiveLocationObject,
},
})
}

IndexFieldDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
func IndexFieldDirective(orderingEnum *gql.Enum) *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: IndexDirectiveLabel,
Description: "@index is a directive that can be used to create an index on a field.",
Args: gql.FieldConfigArgument{
Expand All @@ -144,15 +154,17 @@ var (
Type: gql.Boolean,
},
IndexDirectivePropDirection: &gql.ArgumentConfig{
Type: OrderingEnum,
Type: orderingEnum,
},
},
Locations: []string{
gql.DirectiveLocationField,
},
})
}

CRDTEnum = gql.NewEnum(gql.EnumConfig{
func CRDTEnum() *gql.Enum {
return gql.NewEnum(gql.EnumConfig{
Name: "CRDTType",
Description: "One of the possible CRDT Types.",
Values: gql.EnumValueConfigMap{
Expand All @@ -163,51 +175,57 @@ var (
client.PN_COUNTER.String(): &gql.EnumValueConfig{
Value: client.PN_COUNTER,
Description: `Positive-Negative Counter.
WARNING: Incrementing an integer and causing it to overflow the int64 max value
will cause the value to roll over to the int64 min value. Incremeting a float and
causing it to overflow the float64 max value will act like a no-op.`,
WARNING: Incrementing an integer and causing it to overflow the int64 max value
will cause the value to roll over to the int64 min value. Incremeting a float and
causing it to overflow the float64 max value will act like a no-op.`,
},
client.P_COUNTER.String(): &gql.EnumValueConfig{
Value: client.P_COUNTER,
Description: `Positive Counter.
WARNING: Incrementing an integer and causing it to overflow the int64 max value
will cause the value to roll over to the int64 min value. Incremeting a float and
causing it to overflow the float64 max value will act like a no-op.`,
WARNING: Incrementing an integer and causing it to overflow the int64 max value
will cause the value to roll over to the int64 min value. Incremeting a float and
causing it to overflow the float64 max value will act like a no-op.`,
},
},
})
}

// CRDTFieldDirective @crdt is used to define the CRDT type of a field
CRDTFieldDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
// CRDTFieldDirective @crdt is used to define the CRDT type of a field
func CRDTFieldDirective(crdtEnum *gql.Enum) *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: CRDTDirectiveLabel,
Description: crdtDirectiveDescription,
Args: gql.FieldConfigArgument{
CRDTDirectivePropType: &gql.ArgumentConfig{
Type: CRDTEnum,
Type: crdtEnum,
},
},
Locations: []string{
gql.DirectiveLocationField,
},
})
}

// PrimaryDirective @primary is used to indicate the primary
// side of a one-to-one relationship.
PrimaryDirective = gql.NewDirective(gql.DirectiveConfig{
// PrimaryDirective @primary is used to indicate the primary
// side of a one-to-one relationship.
func PrimaryDirective() *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: PrimaryLabel,
Description: primaryDirectiveDescription,
Locations: []string{
gql.DirectiveLocationFieldDefinition,
},
})
}

// RelationDirective @relation is used to explicitly define
// the attributes of a relationship, specifically, the name
// if you don't want to use the default generated relationship
// name.
RelationDirective = gql.NewDirective(gql.DirectiveConfig{
// RelationDirective @relation is used to explicitly define
// the attributes of a relationship, specifically, the name
// if you don't want to use the default generated relationship
// name.
func RelationDirective() *gql.Directive {
return gql.NewDirective(gql.DirectiveConfig{
Name: RelationLabel,
Description: relationDirectiveDescription,
Args: gql.FieldConfigArgument{
Expand All @@ -220,7 +238,7 @@ causing it to overflow the float64 max value will act like a no-op.`,
gql.DirectiveLocationFieldDefinition,
},
})
)
}

func NewArgConfig(t gql.Type, description string) *gql.ArgumentConfig {
return &gql.ArgumentConfig{
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/schema/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestIntrospectionExplainTypeDefined(t *testing.T) {
"__schema": map[string]any{
"types": []any{
map[string]any{
"description": schemaTypes.ExplainEnum.Description(),
"description": schemaTypes.ExplainEnum().Description(),
"kind": "ENUM",
"name": "ExplainType",
},
Expand Down

0 comments on commit a42a05c

Please sign in to comment.