UNPKG

@graphql-yoga/plugin-defer-stream

Version:
23 lines (22 loc) 945 B
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 })); } }, }; }