Skip to content

Allow directives without a Go visitor counterpart #591

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

Merged
merged 1 commit into from
Feb 22, 2023
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
39 changes: 22 additions & 17 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,27 @@ func TestCustomDirective(t *testing.T) {
}
`,
},
{
Schema: graphql.MustParseSchema(`
# this test ensures that directives without a Go visitor counterpart are allowed
directive @awesome on FIELD_DEFINITION

type Query {
hello: String! @awesome
}`,
&helloResolver{},
),
Query: `
{
hello
}
`,
ExpectedResult: `
{
"hello": "Hello world!"
}
`,
},
})
}

Expand Down Expand Up @@ -613,23 +634,7 @@ func TestParseSchemaWithInvalidCustomDirectives(t *testing.T) {
Args args
Want want
}{
"Missing required directive": {
Args: args{
Resolver: &helloSnakeResolver1{},
Schema: `
directive @customDirective on FIELD_DEFINITION

schema {
query: Query
}

type Query {
hello_html: String! @customDirective
}
`,
},
Want: want{Error: `no visitors have been registered for directive "customDirective"`},
},

"Duplicate directive implementations": {
Args: args{
Directives: []directives.Directive{&customDirectiveVisitor{}, &customInvalidDirective{}},
Expand Down
26 changes: 0 additions & 26 deletions internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,32 +283,6 @@ func applyDirectives(s *ast.Schema, visitors []directives.Directive) (map[string
byName[name] = v
}

for name, def := range s.Directives {
// TODO: directives other than FIELD_DEFINITION also need to be supported, and later addition of
// capabilities to 'visit' other kinds of directive locations shouldn't break the parsing of existing
// schemas that declare those directives, but don't have a visitor for them?
var acceptedType bool
for _, l := range def.Locations {
if l == "FIELD_DEFINITION" {
acceptedType = true
break
}
}

if !acceptedType {
continue
}

if _, ok := byName[name]; !ok {
if name == "include" || name == "skip" || name == "deprecated" || name == "specifiedBy" {
// Special case directives, ignore
continue
}

return nil, fmt.Errorf("no visitors have been registered for directive %q", name)
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might add some schema option which adds an exception list from the above rule, however, until then I'm removing this restriction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still considering if we need to restrict directive locations or not 🤔

return byName, nil
}

Expand Down