graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Enum as Directive Argument Causes Introspection Error

Open callensm opened this issue 4 years ago • 1 comments

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.

Screen Shot 2020-04-14 at 11 24 11 AM

Has anyone encountered or resolved this before?

callensm avatar Apr 14 '20 15:04 callensm

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},
})

t-botz avatar Sep 24 '21 04:09 t-botz