-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement UniqueArgumentNames validator
- Loading branch information
Showing
3 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package validator | ||
|
||
import ( | ||
"github.com/vektah/gqlparser" | ||
) | ||
|
||
func init() { | ||
fieldVisitors = append(fieldVisitors, uniqueFieldArgumentNames) | ||
directiveVisitors = append(directiveVisitors, uniqueDirectiveArgumentNames) | ||
} | ||
|
||
func checkUniqueArgs(ctx *vctx, args []gqlparser.Argument) { | ||
knownArgNames := map[string]bool{} | ||
|
||
for _, arg := range args { | ||
if knownArgNames[arg.Name] { | ||
ctx.errors = append(ctx.errors, Error( | ||
Rule("UniqueArgumentNames"), | ||
Message(`There can be only one argument named "%s".`, arg.Name), | ||
)) | ||
} | ||
|
||
knownArgNames[arg.Name] = true | ||
} | ||
} | ||
|
||
// A GraphQL field is only valid if all supplied arguments are defined by that field. | ||
func uniqueFieldArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, fieldDef *gqlparser.FieldDefinition, field *gqlparser.Field) { | ||
checkUniqueArgs(ctx, field.Arguments) | ||
} | ||
|
||
func uniqueDirectiveArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation) { | ||
checkUniqueArgs(ctx, directive.Arguments) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters