GraphQL supports several directives: @include, @skip and @deprecated. This module opens a new dimension by giving you the possibility to define your custom directives.
This code defines a directive called dateFormat that accepts one argument format of type String. The directive can be used on FIELD (query) and FIELD_DEFINITION (schema).
FIELD AND FIELD_DEFINITION are the only two directive locations supported.
2. Add directive resolver
The second step consists in adding a resolver for the custom directive.
import{addDirectiveResolveFunctionsToSchema}from'graphql-directive'// Attach a resolver map to schemaaddDirectiveResolveFunctionsToSchema(schema,{asyncdateFormat(resolve,source,args){constvalue=awaitresolve()returnformat(newDate(value),args.format)},})
3. Use directive in query
You can now use your directive either in schema or in query.
addDirectiveResolveFunctionsToSchema takes two arguments, a GraphQLSchema and a resolver map. It modifies the schema in place by attaching directive resolvers. Internally your resolvers are wrapped into another one.
import{addDirectiveResolveFunctionsToSchema}from'graphql-directive'constresolverMap={// Will be called when a @upperCase directive is applied to a field.asyncupperCase(resolve){constvalue=awaitresolve()returnvalue.toString().toUpperCase()},}// Attach directive resolvers to schema.addDirectiveResolveFunctionsToSchema(schema,resolverMap)
Directive resolver function signature
Every directive resolver accepts five positional arguments:
directiveName(resolve, obj, directiveArgs, context, info) { result }
These arguments have the following conventional names and meanings:
resolve: Resolve is a function that returns the result of the directive field. For consistency, it always returns a promise resolved with the original field resolver.
obj: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-level Query field, the rootValue passed from the server configuration. This argument enables the nested nature of GraphQL queries.
directiveArgs: An object with the arguments passed into the directive in the query or schema. For example, if the directive was called with @dateFormat(format: "DD/MM/YYYY"), the args object would be: { "format": "DD/MM/YYYY" }.
context: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query.
info: This argument should only be used in advanced cases, but it contains information about the execution state of the query, including the field name, path to the field from the root, and more. It’s only documented in the GraphQL.js source code.
Examples of directives
Text formatting: @upperCase
Text formatting is a good use case for directives. It can be helpful to directly format your text in your queries or to ensure that a field has a specific format server-side.
import{buildSchema}from'graphql'import{addDirectiveResolveFunctionsToSchema}from'graphql-directive'// Schemaconstschema=buildSchema(` directive @upperCase on FIELD_DEFINITION | FIELD`)// ResolveraddDirectiveResolveFunctionsToSchema(schema,{asyncupperCase(resolve){constvalue=awaitresolve()returnvalue.toUpperCase()},})
Authentication is a very good usage of FIELD_DEFINITION directives. By using a directive you can restrict only one specific field without modifying your resolvers.
import{buildSchema}from'graphql'import{addDirectiveResolveFunctionsToSchema}from'graphql-directive'// Schemaconstschema=buildSchema(` directive @requireAuth on FIELD_DEFINITION`)// ResolveraddDirectiveResolveFunctionsToSchema(schema,{requireAuth(resolve,directiveArgs,obj,context,info){if(!context.isAuthenticated)thrownewError(`You must be authenticated to access "${info.fieldName}"`)returnresolve()},})
请发表评论