graphql / graphql/graphql-spec
RFC: Kinds in Directive arg definitions
- Dominant language
- JavaScript
- Stars
- 14.6k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
# Context
Right now in order to reference other named types in a directive, as far as I can find, and confirmed by the [GraphqlInputType](https://github.com/graphql/graphql-js/blob/6a5f51f3bd07f3914c7f1ec01908e9e325b77d58/src/type/definition.ts#L208) definition you need to use a loose `String` type and resolve this away from the type system, in your directive runtime.
```graphql
"""
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](https://github.com/graphql/graphql-js/blob/6a5f51f3bd07f3914c7f1ec01908e9e325b77d58/src/language/kinds.ts#L4) to reference types meaning that directives could be more sound at the type level.
```graphql
"""
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.
```graphql
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.
```typescript
export GraphQLDirectiveInputType = GraphQLInputType | GraphQLKind | GraphQLNonNull
export interface GraphQLDirectiveArgument extends GraphQLArgument {
type: GraphQLDirectiveInputType;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.