Skip to content

Commit 801181b

Browse files
authored
Allow directives without visitors (#591)
1 parent 2da409a commit 801181b

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

graphql_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ func TestCustomDirective(t *testing.T) {
453453
}
454454
`,
455455
},
456+
{
457+
Schema: graphql.MustParseSchema(`
458+
# this test ensures that directives without a Go visitor counterpart are allowed
459+
directive @awesome on FIELD_DEFINITION
460+
461+
type Query {
462+
hello: String! @awesome
463+
}`,
464+
&helloResolver{},
465+
),
466+
Query: `
467+
{
468+
hello
469+
}
470+
`,
471+
ExpectedResult: `
472+
{
473+
"hello": "Hello world!"
474+
}
475+
`,
476+
},
456477
})
457478
}
458479

@@ -613,23 +634,7 @@ func TestParseSchemaWithInvalidCustomDirectives(t *testing.T) {
613634
Args args
614635
Want want
615636
}{
616-
"Missing required directive": {
617-
Args: args{
618-
Resolver: &helloSnakeResolver1{},
619-
Schema: `
620-
directive @customDirective on FIELD_DEFINITION
621-
622-
schema {
623-
query: Query
624-
}
625-
626-
type Query {
627-
hello_html: String! @customDirective
628-
}
629-
`,
630-
},
631-
Want: want{Error: `no visitors have been registered for directive "customDirective"`},
632-
},
637+
633638
"Duplicate directive implementations": {
634639
Args: args{
635640
Directives: []directives.Directive{&customDirectiveVisitor{}, &customInvalidDirective{}},

internal/exec/resolvable/resolvable.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -283,32 +283,6 @@ func applyDirectives(s *ast.Schema, visitors []directives.Directive) (map[string
283283
byName[name] = v
284284
}
285285

286-
for name, def := range s.Directives {
287-
// TODO: directives other than FIELD_DEFINITION also need to be supported, and later addition of
288-
// capabilities to 'visit' other kinds of directive locations shouldn't break the parsing of existing
289-
// schemas that declare those directives, but don't have a visitor for them?
290-
var acceptedType bool
291-
for _, l := range def.Locations {
292-
if l == "FIELD_DEFINITION" {
293-
acceptedType = true
294-
break
295-
}
296-
}
297-
298-
if !acceptedType {
299-
continue
300-
}
301-
302-
if _, ok := byName[name]; !ok {
303-
if name == "include" || name == "skip" || name == "deprecated" || name == "specifiedBy" {
304-
// Special case directives, ignore
305-
continue
306-
}
307-
308-
return nil, fmt.Errorf("no visitors have been registered for directive %q", name)
309-
}
310-
}
311-
312286
return byName, nil
313287
}
314288

0 commit comments

Comments
 (0)