Skip to content

Commit 3e7d0a7

Browse files
authored
Return error on an undeclared directive (#527)
1 parent 64f8084 commit 3e7d0a7

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

internal/schema/schema.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,20 @@ func resolveNamedType(s *types.Schema, t types.NamedType) error {
268268
return err
269269
}
270270
}
271+
if err := resolveDirectives(s, t.Directives, "INTERFACE"); err != nil {
272+
return err
273+
}
271274
case *types.InputObject:
272275
if err := resolveInputObject(s, t.Values); err != nil {
273276
return err
274277
}
278+
if err := resolveDirectives(s, t.Directives, "INPUT_OBJECT"); err != nil {
279+
return err
280+
}
281+
case *types.ScalarTypeDefinition:
282+
if err := resolveDirectives(s, t.Directives, "SCALAR"); err != nil {
283+
return err
284+
}
275285
}
276286
return nil
277287
}
@@ -335,6 +345,11 @@ func resolveInputObject(s *types.Schema, values types.ArgumentsDefinition) error
335345
return err
336346
}
337347
v.Type = t
348+
349+
if err := resolveDirectives(s, v.Directives, "ARGUMENT_DEFINITION"); err != nil {
350+
return err
351+
}
352+
338353
}
339354
return nil
340355
}

internal/schema/schema_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,60 @@ Second line of the description.
936936
return nil
937937
},
938938
},
939+
{
940+
name: "Decorating scalar with an undeclared directive should return an error",
941+
sdl: `
942+
scalar S @undeclareddirective
943+
`,
944+
validateError: func(err error) error {
945+
prefix := `graphql: directive "undeclareddirective" not found`
946+
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
947+
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
948+
}
949+
return nil
950+
},
951+
},
952+
{
953+
name: "Decorating argument with an undeclared directive should return an error",
954+
sdl: `
955+
type Query {
956+
hello(name: String! @undeclareddirective): String!
957+
}
958+
`,
959+
validateError: func(err error) error {
960+
prefix := `graphql: directive "undeclareddirective" not found`
961+
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
962+
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
963+
}
964+
return nil
965+
},
966+
},
967+
{
968+
name: "Decorating input object with an undeclared directive should return an error",
969+
sdl: `
970+
input InputObject @undeclareddirective{}
971+
`,
972+
validateError: func(err error) error {
973+
prefix := `graphql: directive "undeclareddirective" not found`
974+
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
975+
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
976+
}
977+
return nil
978+
},
979+
},
980+
{
981+
name: "Decorating interface with an undeclared directive should return an error",
982+
sdl: `
983+
interface I @undeclareddirective {}
984+
`,
985+
validateError: func(err error) error {
986+
prefix := `graphql: directive "undeclareddirective" not found`
987+
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
988+
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
989+
}
990+
return nil
991+
},
992+
},
939993
} {
940994
t.Run(test.name, func(t *testing.T) {
941995
s, err := schema.ParseSchema(test.sdl, test.useStringDescriptions)

0 commit comments

Comments
 (0)