Skip to content

Commit 415a658

Browse files
author
Arijit Das
authored
Return error list while validating schema. (#5576)
* Return error list while validating schema.
1 parent 9765e3f commit 415a658

File tree

5 files changed

+700
-343
lines changed

5 files changed

+700
-343
lines changed

graphql/e2e/custom_logic/custom_logic_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,9 +1838,9 @@ func TestCustomGraphqlInvalidInputFormatForBatchedField(t *testing.T) {
18381838
res := updateSchema(t, schema)
18391839
require.Equal(t, `{"updateGQLSchema":null}`, string(res.Data))
18401840
require.Len(t, res.Errors, 1)
1841-
require.Equal(t, "couldn't rewrite mutation updateGQLSchema because input:9: Type Post"+
1842-
"; Field comments: inside graphql in @custom directive, for BATCH mode, query"+
1843-
" `getPosts` can have only one argument whose value should be a variable.\n",
1841+
require.Equal(t, "couldn't rewrite mutation updateGQLSchema because input:9: "+
1842+
"Type Post; Field comments: inside graphql in @custom directive, for BATCH mode, "+
1843+
"query `getPosts` can have only one argument whose value should be a variable.\n",
18441844
res.Errors[0].Error())
18451845
}
18461846

graphql/schema/gqlschema.go

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type directiveValidator func(
190190
typ *ast.Definition,
191191
field *ast.FieldDefinition,
192192
dir *ast.Directive,
193-
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error
193+
secrets map[string]x.SensitiveByteSlice) gqlerror.List
194194

195195
type searchTypeIndex struct {
196196
gqlType string
@@ -289,38 +289,32 @@ var scalarToDgraph = map[string]string{
289289
"Password": "password",
290290
}
291291

292+
func ValidatorNoOp(
293+
sch *ast.Schema,
294+
typ *ast.Definition,
295+
field *ast.FieldDefinition,
296+
dir *ast.Directive,
297+
secrets map[string]x.SensitiveByteSlice) gqlerror.List {
298+
return nil
299+
}
300+
292301
var directiveValidators = map[string]directiveValidator{
293-
inverseDirective: hasInverseValidation,
294-
searchDirective: searchValidation,
295-
dgraphDirective: dgraphDirectiveValidation,
296-
idDirective: idValidation,
297-
secretDirective: passwordValidation,
298-
customDirective: customDirectiveValidation,
299-
remoteDirective: remoteDirectiveValidation,
300-
deprecatedDirective: func(
301-
sch *ast.Schema,
302-
typ *ast.Definition,
303-
field *ast.FieldDefinition,
304-
dir *ast.Directive,
305-
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error {
306-
return nil
307-
},
302+
inverseDirective: hasInverseValidation,
303+
searchDirective: searchValidation,
304+
dgraphDirective: dgraphDirectiveValidation,
305+
idDirective: idValidation,
306+
secretDirective: passwordValidation,
307+
customDirective: customDirectiveValidation,
308+
remoteDirective: ValidatorNoOp,
309+
deprecatedDirective: ValidatorNoOp,
308310
// Just go get it printed into generated schema
309-
authDirective: func(
310-
sch *ast.Schema,
311-
typ *ast.Definition,
312-
field *ast.FieldDefinition,
313-
dir *ast.Directive,
314-
secrets map[string]x.SensitiveByteSlice) *gqlerror.Error {
315-
return nil
316-
},
311+
authDirective: ValidatorNoOp,
317312
}
318313

319314
var schemaDocValidations []func(schema *ast.SchemaDocument) gqlerror.List
320315
var schemaValidations []func(schema *ast.Schema, definitions []string) gqlerror.List
321-
var defnValidations, typeValidations []func(schema *ast.Schema,
322-
defn *ast.Definition) *gqlerror.Error
323-
var fieldValidations []func(typ *ast.Definition, field *ast.FieldDefinition) *gqlerror.Error
316+
var defnValidations, typeValidations []func(schema *ast.Schema, defn *ast.Definition) gqlerror.List
317+
var fieldValidations []func(typ *ast.Definition, field *ast.FieldDefinition) gqlerror.List
324318

325319
func copyAstFieldDef(src *ast.FieldDefinition) *ast.FieldDefinition {
326320
var dirs ast.DirectiveList
@@ -425,8 +419,7 @@ func postGQLValidation(schema *ast.Schema, definitions []string,
425419
if directiveValidators[dir.Name] == nil {
426420
continue
427421
}
428-
errs = appendIfNotNull(errs,
429-
directiveValidators[dir.Name](schema, typ, field, dir, secrets))
422+
errs = append(errs, directiveValidators[dir.Name](schema, typ, field, dir, secrets)...)
430423
}
431424
}
432425
}
@@ -463,21 +456,19 @@ func applySchemaValidations(schema *ast.Schema, definitions []string) gqlerror.L
463456
}
464457

465458
func applyDefnValidations(defn *ast.Definition, schema *ast.Schema,
466-
rules []func(schema *ast.Schema, defn *ast.Definition) *gqlerror.Error) gqlerror.List {
459+
rules []func(schema *ast.Schema, defn *ast.Definition) gqlerror.List) gqlerror.List {
467460
var errs []*gqlerror.Error
468-
469461
for _, rule := range rules {
470-
errs = appendIfNotNull(errs, rule(schema, defn))
462+
errs = append(errs, rule(schema, defn)...)
471463
}
472-
473464
return errs
474465
}
475466

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

479470
for _, rule := range fieldValidations {
480-
errs = appendIfNotNull(errs, rule(typ, field))
471+
errs = append(errs, rule(typ, field)...)
481472
}
482473

483474
return errs

0 commit comments

Comments
 (0)