Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing directive definitions #2369

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
"fmt"
"sort"

"github.com/sourcenetwork/graphql-go/language/ast"
gqlp "github.com/sourcenetwork/graphql-go/language/parser"
"github.com/sourcenetwork/graphql-go/language/source"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/request/graphql/schema/types"

"github.com/sourcenetwork/graphql-go/language/ast"
gqlp "github.com/sourcenetwork/graphql-go/language/parser"
"github.com/sourcenetwork/graphql-go/language/source"
)

// FromString parses a GQL SDL string into a set of collection descriptions.
Expand Down Expand Up @@ -390,18 +389,15 @@ func setCRDTType(field *ast.FieldDefinition, kind client.FieldKind) (client.CTyp
for _, arg := range directive.Arguments {
switch arg.Name.Value {
case "type":
cType := arg.Value.GetValue().(string)
switch cType {
case client.PN_COUNTER.String():
if !client.PN_COUNTER.IsCompatibleWith(kind) {
return 0, client.NewErrCRDTKindMismatch(cType, kind.String())
}
return client.PN_COUNTER, nil
case client.LWW_REGISTER.String():
return client.LWW_REGISTER, nil
default:
return 0, client.NewErrInvalidCRDTType(field.Name.Value, cType)
cTypeString := arg.Value.GetValue().(string)
cType, validCRDTEnum := types.CRDTEnum.ParseValue(cTypeString).(client.CType)
if !validCRDTEnum {
return 0, client.NewErrInvalidCRDTType(field.Name.Value, cTypeString)
}
if !cType.IsCompatibleWith(kind) {
return 0, client.NewErrCRDTKindMismatch(cType.String(), kind.String())
}
return cType, nil
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions request/graphql/schema/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ func defaultMutationType() *gql.Object {
// default directives type.
func defaultDirectivesType() []*gql.Directive {
return []*gql.Directive{
schemaTypes.CRDTFieldDirective,
schemaTypes.ExplainDirective,
schemaTypes.IndexDirective,
schemaTypes.IndexFieldDirective,
schemaTypes.PrimaryDirective,
schemaTypes.RelationDirective,
}
}

Expand Down Expand Up @@ -166,6 +169,7 @@ func defaultTypes() []gql.Type {
schemaTypes.CommitLinkObject,
schemaTypes.CommitObject,

schemaTypes.CRDTEnum,
schemaTypes.ExplainEnum,
}
}
3 changes: 3 additions & 0 deletions request/graphql/schema/types/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ Sort the results in ascending order, e.g. null,1,2,3,a,b,c.
`
descOrderDescription string = `
Sort the results in descending order, e.g. c,b,a,3,2,1,null.
`
crdtDirectiveDescription string = `
Allows the explicit definition of a field's CRDT type. By default it is defined as LWWRegister.
`
primaryDirectiveDescription string = `
Indicate the primary side of a one-to-one relationship.
Expand Down
34 changes: 34 additions & 0 deletions request/graphql/schema/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package types

import (
gql "github.com/sourcenetwork/graphql-go"

"github.com/sourcenetwork/defradb/client"
)

const (
Expand All @@ -24,6 +26,9 @@ const (
ExplainArgExecute string = "execute"
ExplainArgDebug string = "debug"

CRDTDirectiveLabel = "crdt"
CRDTDirectivePropType = "type"

IndexDirectiveLabel = "index"
IndexDirectivePropName = "name"
IndexDirectivePropUnique = "unique"
Expand Down Expand Up @@ -127,6 +132,35 @@ var (
},
})

CRDTEnum = gql.NewEnum(gql.EnumConfig{
Name: "CRDTType",
Description: "One of the possible CRDT Types.",
Values: gql.EnumValueConfigMap{
client.LWW_REGISTER.String(): &gql.EnumValueConfig{
Value: client.LWW_REGISTER,
Description: "Last Write Wins register",
},
client.PN_COUNTER.String(): &gql.EnumValueConfig{
Value: client.PN_COUNTER,
Description: "Positive-Negative Counter",
},
},
})

// CRDTFieldDirective @crdt is used to define the CRDT type of a field
CRDTFieldDirective *gql.Directive = gql.NewDirective(gql.DirectiveConfig{
Name: CRDTDirectiveLabel,
Description: crdtDirectiveDescription,
Args: gql.FieldConfigArgument{
CRDTDirectivePropType: &gql.ArgumentConfig{
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{
Expand Down
Loading