Skip to content

Commit

Permalink
Disallow repeat of non repeatable directives (#525)
Browse files Browse the repository at this point in the history
* Disallow repeat of non repeatable directives

* Remove unnecessary scallar
  • Loading branch information
ostrea authored Jul 20, 2022
1 parent 5a1c172 commit 64f8084
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func resolveField(s *types.Schema, f *types.FieldDefinition) error {
}

func resolveDirectives(s *types.Schema, directives types.DirectiveList, loc string) error {
alreadySeenNonRepeatable := make(map[string]struct{})
for _, d := range directives {
dirName := d.Name.Name
dd, ok := s.Directives[dirName]
Expand All @@ -315,6 +316,14 @@ func resolveDirectives(s *types.Schema, directives types.DirectiveList, loc stri
d.Arguments = append(d.Arguments, &types.Argument{Name: arg.Name, Value: arg.Default})
}
}

if dd.Repeatable {
continue
}
if _, seen := alreadySeenNonRepeatable[dirName]; seen {
return errors.Errorf(`non repeatable directive %q can not be repeated. Consider adding "repeatable".`, dirName)
}
alreadySeenNonRepeatable[dirName] = struct{}{}
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,22 @@ Second line of the description.
return nil
},
},
{
name: "Disallow repeat of a directive if it is not `repeatable`",
sdl: `
directive @nonrepeatabledirective on FIELD_DEFINITION
type Foo {
bar: String @nonrepeatabledirective @nonrepeatabledirective
}
`,
validateError: func(err error) error {
prefix := `graphql: non repeatable directive "nonrepeatabledirective" can not be repeated. Consider adding "repeatable"`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
} {
t.Run(test.name, func(t *testing.T) {
s, err := schema.ParseSchema(test.sdl, test.useStringDescriptions)
Expand Down

0 comments on commit 64f8084

Please sign in to comment.