graphql-spec
graphql-spec copied to clipboard
RFC: Kinds in Directive arg definitions
Context
Right now in order to reference other named types in a directive, as far as I can find, and confirmed by the GraphqlInputType definition you need to use a loose String
type and resolve this away from the type system, in your directive runtime.
"""
Superficial directive to copy description from one object field to another.
"""
directive @copy(from: String) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
type MyType {
field: String
}
input MyInput {
field: String @copy(from: "MyType")
}
Proposal
It would be great if we could use kinds to reference types meaning that directives could be more sound at the type level.
"""
Superficial directive to copy description from one object field to another.
Note the "from" arg is now of type Kind
"""
directive @copy(from: NamedType) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
type MyType {
field: String
}
input MyInput {
field: String @copy(from: MyType)
}
You may also look to have a differentiated syntax for kinds.
directive @copy(from: 'NamedType) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
Outcome
The type system can now complain if MyType
was not defined.
Solution
I'd suspect you wouldn't want GraphQLArgument
or GraphqlInputType
to allow for kind types (ie. resolving the type on a client seems non-trivial perhaps?) so a first implementation could introduce GraphQLDirectiveArgument
which extends GraphQLArgument
with support for kinds.
export GraphQLDirectiveInputType = GraphQLInputType | GraphQLKind | GraphQLNonNull<GraphQLKind>
export interface GraphQLDirectiveArgument extends GraphQLArgument {
type: GraphQLDirectiveInputType;
}