@graphql-yoga/plugin-defer-stream
Version:
Defer/Stream plugin for GraphQL Yoga.
23 lines (22 loc) • 945 B
JavaScript
import { isListType, isWrappingType, } from 'graphql';
import { createGraphQLError, GraphQLStreamDirective, } from '@graphql-tools/utils';
/**
* Stream directive on list field
*
* A GraphQL document is only valid if stream directives are used on list fields.
*/
export function StreamDirectiveOnListFieldRule(context) {
return {
Directive(node) {
const fieldDef = context.getFieldDef();
const parentType = context.getParentType();
if (fieldDef &&
parentType &&
node.name.value === GraphQLStreamDirective.name &&
!(isListType(fieldDef.type) ||
(isWrappingType(fieldDef.type) && isListType(fieldDef.type.ofType)))) {
context.reportError(createGraphQLError(`Stream directive cannot be used on non-list field "${fieldDef.name}" on type "${parentType.name}".`, { nodes: node }));
}
},
};
}