Skip to content

Commit

Permalink
Return error list while validating schema. (#5576)
Browse files Browse the repository at this point in the history
* Return error list while validating schema.
  • Loading branch information
Arijit Das authored Jun 11, 2020
1 parent 9765e3f commit 415a658
Show file tree
Hide file tree
Showing 5 changed files with 700 additions and 343 deletions.
6 changes: 3 additions & 3 deletions graphql/e2e/custom_logic/custom_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,9 +1838,9 @@ func TestCustomGraphqlInvalidInputFormatForBatchedField(t *testing.T) {
res := updateSchema(t, schema)
require.Equal(t, `{"updateGQLSchema":null}`, string(res.Data))
require.Len(t, res.Errors, 1)
require.Equal(t, "couldn't rewrite mutation updateGQLSchema because input:9: Type Post"+
"; Field comments: inside graphql in @custom directive, for BATCH mode, query"+
" `getPosts` can have only one argument whose value should be a variable.\n",
require.Equal(t, "couldn't rewrite mutation updateGQLSchema because input:9: "+
"Type Post; Field comments: inside graphql in @custom directive, for BATCH mode, "+
"query `getPosts` can have only one argument whose value should be a variable.\n",
res.Errors[0].Error())
}

Expand Down
59 changes: 25 additions & 34 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ type directiveValidator func(
typ *ast.Definition,
field *ast.FieldDefinition,
dir *ast.Directive,
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error
secrets map[string]x.SensitiveByteSlice) gqlerror.List

type searchTypeIndex struct {
gqlType string
Expand Down Expand Up @@ -289,38 +289,32 @@ var scalarToDgraph = map[string]string{
"Password": "password",
}

func ValidatorNoOp(
sch *ast.Schema,
typ *ast.Definition,
field *ast.FieldDefinition,
dir *ast.Directive,
secrets map[string]x.SensitiveByteSlice) gqlerror.List {
return nil
}

var directiveValidators = map[string]directiveValidator{
inverseDirective: hasInverseValidation,
searchDirective: searchValidation,
dgraphDirective: dgraphDirectiveValidation,
idDirective: idValidation,
secretDirective: passwordValidation,
customDirective: customDirectiveValidation,
remoteDirective: remoteDirectiveValidation,
deprecatedDirective: func(
sch *ast.Schema,
typ *ast.Definition,
field *ast.FieldDefinition,
dir *ast.Directive,
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error {
return nil
},
inverseDirective: hasInverseValidation,
searchDirective: searchValidation,
dgraphDirective: dgraphDirectiveValidation,
idDirective: idValidation,
secretDirective: passwordValidation,
customDirective: customDirectiveValidation,
remoteDirective: ValidatorNoOp,
deprecatedDirective: ValidatorNoOp,
// Just go get it printed into generated schema
authDirective: func(
sch *ast.Schema,
typ *ast.Definition,
field *ast.FieldDefinition,
dir *ast.Directive,
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error {
return nil
},
authDirective: ValidatorNoOp,
}

var schemaDocValidations []func(schema *ast.SchemaDocument) gqlerror.List
var schemaValidations []func(schema *ast.Schema, definitions []string) gqlerror.List
var defnValidations, typeValidations []func(schema *ast.Schema,
defn *ast.Definition) *gqlerror.Error
var fieldValidations []func(typ *ast.Definition, field *ast.FieldDefinition) *gqlerror.Error
var defnValidations, typeValidations []func(schema *ast.Schema, defn *ast.Definition) gqlerror.List
var fieldValidations []func(typ *ast.Definition, field *ast.FieldDefinition) gqlerror.List

func copyAstFieldDef(src *ast.FieldDefinition) *ast.FieldDefinition {
var dirs ast.DirectiveList
Expand Down Expand Up @@ -425,8 +419,7 @@ func postGQLValidation(schema *ast.Schema, definitions []string,
if directiveValidators[dir.Name] == nil {
continue
}
errs = appendIfNotNull(errs,
directiveValidators[dir.Name](schema, typ, field, dir, secrets))
errs = append(errs, directiveValidators[dir.Name](schema, typ, field, dir, secrets)...)
}
}
}
Expand Down Expand Up @@ -463,21 +456,19 @@ func applySchemaValidations(schema *ast.Schema, definitions []string) gqlerror.L
}

func applyDefnValidations(defn *ast.Definition, schema *ast.Schema,
rules []func(schema *ast.Schema, defn *ast.Definition) *gqlerror.Error) gqlerror.List {
rules []func(schema *ast.Schema, defn *ast.Definition) gqlerror.List) gqlerror.List {
var errs []*gqlerror.Error

for _, rule := range rules {
errs = appendIfNotNull(errs, rule(schema, defn))
errs = append(errs, rule(schema, defn)...)
}

return errs
}

func applyFieldValidations(typ *ast.Definition, field *ast.FieldDefinition) gqlerror.List {
var errs []*gqlerror.Error

for _, rule := range fieldValidations {
errs = appendIfNotNull(errs, rule(typ, field))
errs = append(errs, rule(typ, field)...)
}

return errs
Expand Down
Loading

0 comments on commit 415a658

Please sign in to comment.