graphql
graphql copied to clipboard
Enum as Directive Argument Causes Introspection Error
I'm trying to implement a custom directive with the schema definition:
directive @authenticated(type: AuthenticationType) on FIELD_DEFINITION
and I've create the enum
and directive
objects like so...
var AuthenticationTypeEnum = graphql.NewEnum(graphql.EnumConfig{
Name: "AuthenticationType",
Values: graphql.EnumValueConfigMap{
"API_KEY": &graphql.EnumValueConfig{Value: util.AuthenticationTypeAPIKey},
"JWT": &graphql.EnumValueConfig{Value: util.AuthenticationTypeJWT},
},
})
var AuthenticatedDirective = graphql.NewDirective(graphql.DirectiveConfig{
Name: "authenticated",
Description: "Identifies a query or mutation as requiring authentication for resolution",
Locations: []string{graphql.DirectiveLocationFieldDefinition},
Args: graphql.FieldConfigArgument{
"type": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(AuthenticationTypeEnum),
},
},
})
This issue is there's no way to attach the directive to a field definition object or implement the logic for processing it and when I go into GraphiQL
to inspect it, the error below comes up continuously saying that the enum
is an invalid or incomplete definition.

Has anyone encountered or resolved this before?
I have just met the same error, the solution is to add explicitely the enum type to the schema:
graphql.NewSchema(graphql.SchemaConfig{
Types: []graphql.Type{AuthenticationTypeEnum},
Directives: []*graphql.Directive{AuthenticatedDirective},
})