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

Disallow repeat of non repeatable directives #525

Merged
merged 2 commits into from
Jul 20, 2022
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
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