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

Add check for directive location #3

Merged
merged 3 commits into from
Sep 29, 2020
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
17 changes: 13 additions & 4 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error {
if err := validateArgs(schema, field.Arguments, nil); err != nil {
return err
}
if err := validateDirectives(schema, field.Directives, nil); err != nil {
if err := validateDirectives(schema, field.Directives, LocationFieldDefinition, nil); err != nil {
return err
}
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error {
}
}

return validateDirectives(schema, def.Directives, nil)
return validateDirectives(schema, def.Directives, DirectiveLocation(def.Kind), nil)
}

func validateTypeRef(schema *Schema, typ *Type) *gqlerror.Error {
Expand Down Expand Up @@ -274,14 +274,14 @@ func validateArgs(schema *Schema, args ArgumentDefinitionList, currentDirective
def.Kind,
)
}
if err := validateDirectives(schema, arg.Directives, currentDirective); err != nil {
if err := validateDirectives(schema, arg.Directives, LocationArgumentDefinition, currentDirective); err != nil {
return err
}
}
return nil
}

func validateDirectives(schema *Schema, dirs DirectiveList, currentDirective *DirectiveDefinition) *gqlerror.Error {
func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLocation, currentDirective *DirectiveDefinition) *gqlerror.Error {
for _, dir := range dirs {
if err := validateName(dir.Position, dir.Name); err != nil {
// now, GraphQL spec doesn't have reserved directive name
Expand All @@ -293,6 +293,15 @@ func validateDirectives(schema *Schema, dirs DirectiveList, currentDirective *Di
if schema.Directives[dir.Name] == nil {
return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name)
}
validKind := false
for _, dirLocation := range schema.Directives[dir.Name].Locations {
if dirLocation == location {
validKind = true
}
}
if !validKind {
return gqlerror.ErrorPosf(dir.Position, "Directive %s is not applicable on %s.", dir.Name, location)
}
dir.Definition = schema.Directives[dir.Name]
}
return nil
Expand Down
17 changes: 17 additions & 0 deletions validator/schema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,23 @@ directives:
message: 'cannot use Interface as argument a because INTERFACE is not a valid input type'
locations: [{line: 2, column: 14}]

- name: Invalid location usage not allowed
input: |
directive @test on FIELD_DEFINITION
input I1 @test { f: String }

error:
message: 'Directive test is not applicable on INPUT_OBJECT.'
locations: [{line: 2, column: 11}]

- name: Valid location usage
input: |
directive @test on FIELD_DEFINITION
directive @inp on INPUT_OBJECT
input I1 @inp { f: String }
type P { name: String @test }


entry points:
- name: multiple schema entry points
input: |
Expand Down
2 changes: 1 addition & 1 deletion validator/testdata/extensions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ extend type Dog {
owner: Person! @permission(permission: "admin")
}

directive @permission(permission: String!) on FIELD
directive @permission(permission: String!) on FIELD_DEFINITION