Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AccumulatorMap } from '../jsutils/AccumulatorMap.js';
import type { ObjMap } from '../jsutils/ObjMap.js';

import type {
DirectiveNode,
FieldNode,
FragmentDefinitionNode,
FragmentSpreadNode,
Expand Down Expand Up @@ -57,8 +56,7 @@ interface CollectFieldsContext {
runtimeType: GraphQLObjectType;
visitedFragmentNames: Set<string>;
hideSuggestions: boolean;
forbiddenDirectiveInstances: Array<DirectiveNode>;
forbidSkipAndInclude: boolean;
shouldIncludeNodeFn: typeof shouldIncludeNode;
}

/**
Expand All @@ -78,11 +76,10 @@ export function collectFields(
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
hideSuggestions: boolean,
forbidSkipAndInclude = false,
shouldIncludeNodeFn = shouldIncludeNode,
): {
groupedFieldSet: GroupedFieldSet;
newDeferUsages: ReadonlyArray<DeferUsage>;
forbiddenDirectiveInstances: ReadonlyArray<DirectiveNode>;
} {
const groupedFieldSet = new AccumulatorMap<string, FieldDetails>();
const newDeferUsages: Array<DeferUsage> = [];
Expand All @@ -93,15 +90,13 @@ export function collectFields(
runtimeType,
visitedFragmentNames: new Set(),
hideSuggestions,
forbiddenDirectiveInstances: [],
forbidSkipAndInclude,
shouldIncludeNodeFn,
};

collectFieldsImpl(context, selectionSet, groupedFieldSet, newDeferUsages);
return {
groupedFieldSet,
newDeferUsages,
forbiddenDirectiveInstances: context.forbiddenDirectiveInstances,
};
}

Expand Down Expand Up @@ -134,8 +129,7 @@ export function collectSubfields(
runtimeType: returnType,
visitedFragmentNames: new Set(),
hideSuggestions,
forbiddenDirectiveInstances: [],
forbidSkipAndInclude: false,
shouldIncludeNodeFn: shouldIncludeNode,
};
const subGroupedFieldSet = new AccumulatorMap<string, FieldDetails>();
const newDeferUsages: Array<DeferUsage> = [];
Expand Down Expand Up @@ -177,17 +171,18 @@ function collectFieldsImpl(
runtimeType,
visitedFragmentNames,
hideSuggestions,
shouldIncludeNodeFn,
} = context;

for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
if (
!shouldIncludeNode(
context,
!shouldIncludeNodeFn(
selection,
variableValues,
fragmentVariableValues,
hideSuggestions,
)
) {
continue;
Expand All @@ -201,11 +196,11 @@ function collectFieldsImpl(
}
case Kind.INLINE_FRAGMENT: {
if (
!shouldIncludeNode(
context,
!shouldIncludeNodeFn(
selection,
variableValues,
fragmentVariableValues,
hideSuggestions,
) ||
!doesFragmentConditionMatch(schema, selection, runtimeType)
) {
Expand Down Expand Up @@ -247,11 +242,11 @@ function collectFieldsImpl(

if (
visitedFragmentNames.has(fragName) ||
!shouldIncludeNode(
context,
!shouldIncludeNodeFn(
selection,
variableValues,
fragmentVariableValues,
hideSuggestions,
)
) {
continue;
Expand Down Expand Up @@ -348,25 +343,21 @@ function getDeferUsage(
* directives, where `@skip` has higher precedence than `@include`.
*/
function shouldIncludeNode(
context: CollectFieldsContext,
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
variableValues: VariableValues,
fragmentVariableValues: VariableValues | undefined,
hideSuggestions: boolean,
): boolean {
const skipDirectiveNode = node.directives?.find(
(directive) => directive.name.value === GraphQLSkipDirective.name,
);
if (skipDirectiveNode && context.forbidSkipAndInclude) {
context.forbiddenDirectiveInstances.push(skipDirectiveNode);
return false;
}
const skip = skipDirectiveNode
? experimentalGetArgumentValues(
skipDirectiveNode,
GraphQLSkipDirective.args,
variableValues,
fragmentVariableValues,
context.hideSuggestions,
hideSuggestions,
)
: undefined;
if (skip?.if === true) {
Expand All @@ -376,17 +367,13 @@ function shouldIncludeNode(
const includeDirectiveNode = node.directives?.find(
(directive) => directive.name.value === GraphQLIncludeDirective.name,
);
if (includeDirectiveNode && context.forbidSkipAndInclude) {
context.forbiddenDirectiveInstances.push(includeDirectiveNode);
return false;
}
const include = includeDirectiveNode
? experimentalGetArgumentValues(
includeDirectiveNode,
GraphQLIncludeDirective.args,
variableValues,
fragmentVariableValues,
context.hideSuggestions,
hideSuggestions,
)
: undefined;
if (include?.if === false) {
Expand Down
45 changes: 34 additions & 11 deletions src/validation/rules/SingleFieldSubscriptionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import type { ObjMap } from '../../jsutils/ObjMap.js';

import { GraphQLError } from '../../error/GraphQLError.js';

import type { FieldNode, OperationDefinitionNode } from '../../language/ast.js';
import type {
DirectiveNode,
FieldNode,
OperationDefinitionNode,
} from '../../language/ast.js';
import { Kind } from '../../language/kinds.js';
import type { ASTVisitor } from '../../language/visitor.js';

import {
GraphQLIncludeDirective,
GraphQLSkipDirective,
} from '../../type/directives.js';

import type {
FieldDetailsList,
FragmentDetails,
Expand Down Expand Up @@ -46,16 +55,30 @@ export function SingleFieldSubscriptionsRule(
fragments[definition.name.value] = { definition };
}
}
const { groupedFieldSet, forbiddenDirectiveInstances } =
collectFields(
schema,
fragments,
variableValues,
subscriptionType,
node.selectionSet,
context.hideSuggestions,
true,
);
const forbiddenDirectiveInstances: Array<DirectiveNode> = [];
const { groupedFieldSet } = collectFields(
schema,
fragments,
variableValues,
subscriptionType,
node.selectionSet,
context.hideSuggestions,
(selection) => {
for (const directive of [
GraphQLSkipDirective,
GraphQLIncludeDirective,
]) {
const directiveNode = selection.directives?.find(
(d) => d.name.value === directive.name,
);
if (directiveNode !== undefined) {
forbiddenDirectiveInstances.push(directiveNode);
return false;
}
}
return true;
},
);
if (forbiddenDirectiveInstances.length > 0) {
context.reportError(
new GraphQLError(
Expand Down
Loading