Skip to content

Commit 7914f56

Browse files
Merge pull request #16 from dgraph-io/anurags92/gwlparserfix
fix(Parser): Add directive argument validator
2 parents eb159c9 + f3f3532 commit 7914f56

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

validator/schema.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ func validateArgs(schema *Schema, args ArgumentDefinitionList, currentDirective
289289
return nil
290290
}
291291

292+
func validateDirectiveArgs(dir *Directive, schema *Schema) *gqlerror.Error {
293+
allowedArgs := make(map[string]struct{})
294+
for _, arg := range schema.Directives[dir.Name].Arguments {
295+
allowedArgs[arg.Name] = struct{}{}
296+
}
297+
298+
for _, arg := range dir.Arguments {
299+
if _, ok := allowedArgs[arg.Name]; !ok {
300+
return gqlerror.ErrorPosf(dir.Position, "%s is not supported as an argument for %s directive.", arg.Name, dir.Name)
301+
}
302+
}
303+
return nil
304+
305+
}
306+
292307
func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLocation, currentDirective *DirectiveDefinition) *gqlerror.Error {
293308
for _, dir := range dirs {
294309
if err := validateName(dir.Position, dir.Name); err != nil {
@@ -301,6 +316,9 @@ func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLo
301316
if schema.Directives[dir.Name] == nil {
302317
return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name)
303318
}
319+
if err := validateDirectiveArgs(dir, schema); err != nil {
320+
return err
321+
}
304322
validKind := false
305323
for _, dirLocation := range schema.Directives[dir.Name].Locations {
306324
if dirLocation == location {

validator/schema_test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ directives:
505505
506506
directive @A(a: Input, b: Scalar, c: Enum) on FIELD_DEFINITION
507507
508+
- name: Valid arg for directive
509+
input: |
510+
type User @include(aggregate: false) {
511+
name: String
512+
}
513+
514+
error:
515+
message: 'aggregate is not supported as an argument for include directive.'
516+
locations: [{line: 1, column: 12}]
517+
508518
- name: Objects not allowed
509519
input: |
510520
type Object { id: ID }

0 commit comments

Comments
 (0)