Skip to content

polish(fragmentArgs): simplify fragment args execution #4182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
79 changes: 33 additions & 46 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ export interface DeferUsage {
parentDeferUsage: DeferUsage | undefined;
}

export interface FragmentVariables {
signatures: ObjMap<GraphQLVariableSignature>;
values: ObjMap<unknown>;
}

export interface FieldDetails {
node: FieldNode;
deferUsage?: DeferUsage | undefined;
fragmentVariables?: FragmentVariables | undefined;
scopedVariableValues?: { [variable: string]: unknown } | undefined;
}

export type FieldGroup = ReadonlyArray<FieldDetails>;
Expand Down Expand Up @@ -136,14 +131,14 @@ export function collectSubfields(
for (const fieldDetail of fieldGroup) {
const selectionSet = fieldDetail.node.selectionSet;
if (selectionSet) {
const { deferUsage, fragmentVariables } = fieldDetail;
const { deferUsage, scopedVariableValues } = fieldDetail;
collectFieldsImpl(
context,
selectionSet,
subGroupedFieldSet,
newDeferUsages,
deferUsage,
fragmentVariables,
scopedVariableValues,
);
}
}
Expand All @@ -161,33 +156,35 @@ function collectFieldsImpl(
groupedFieldSet: AccumulatorMap<string, FieldDetails>,
newDeferUsages: Array<DeferUsage>,
deferUsage?: DeferUsage,
fragmentVariables?: FragmentVariables,
scopedVariableValues?: { [variable: string]: unknown },
): void {
const {
schema,
fragments,
variableValues,
variableValues: operationVariableValues,
runtimeType,
operation,
visitedFragmentNames,
} = context;

const variableValues = scopedVariableValues ?? operationVariableValues;

for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
if (!shouldIncludeNode(selection, variableValues, fragmentVariables)) {
if (!shouldIncludeNode(variableValues, selection)) {
continue;
}
groupedFieldSet.add(getFieldEntryKey(selection), {
node: selection,
deferUsage,
fragmentVariables,
scopedVariableValues,
});
break;
}
case Kind.INLINE_FRAGMENT: {
if (
!shouldIncludeNode(selection, variableValues, fragmentVariables) ||
!shouldIncludeNode(variableValues, selection) ||
!doesFragmentConditionMatch(schema, selection, runtimeType)
) {
continue;
Expand All @@ -196,7 +193,6 @@ function collectFieldsImpl(
const newDeferUsage = getDeferUsage(
operation,
variableValues,
fragmentVariables,
selection,
deferUsage,
);
Expand All @@ -208,7 +204,7 @@ function collectFieldsImpl(
groupedFieldSet,
newDeferUsages,
deferUsage,
fragmentVariables,
scopedVariableValues,
);
} else {
newDeferUsages.push(newDeferUsage);
Expand All @@ -218,7 +214,7 @@ function collectFieldsImpl(
groupedFieldSet,
newDeferUsages,
newDeferUsage,
fragmentVariables,
scopedVariableValues,
);
}

Expand All @@ -230,15 +226,14 @@ function collectFieldsImpl(
const newDeferUsage = getDeferUsage(
operation,
variableValues,
fragmentVariables,
selection,
deferUsage,
);

if (
!newDeferUsage &&
(visitedFragmentNames.has(fragName) ||
!shouldIncludeNode(selection, variableValues, fragmentVariables))
!shouldIncludeNode(variableValues, selection))
) {
continue;
}
Expand All @@ -252,17 +247,22 @@ function collectFieldsImpl(
}

const fragmentVariableSignatures = fragment.variableSignatures;
let newFragmentVariables: FragmentVariables | undefined;
let newScopedVariableValues:
| { [variable: string]: unknown }
| undefined;
if (fragmentVariableSignatures) {
newFragmentVariables = {
signatures: fragmentVariableSignatures,
values: experimentalGetArgumentValues(
selection,
Object.values(fragmentVariableSignatures),
variableValues,
fragmentVariables,
),
};
newScopedVariableValues = experimentalGetArgumentValues(
selection,
Object.values(fragmentVariableSignatures),
variableValues,
);
for (const [variableName, value] of Object.entries(
operationVariableValues,
)) {
if (!fragment.variableSignatures?.[variableName]) {
newScopedVariableValues[variableName] = value;
}
}
}

if (!newDeferUsage) {
Expand All @@ -273,7 +273,7 @@ function collectFieldsImpl(
groupedFieldSet,
newDeferUsages,
deferUsage,
newFragmentVariables,
newScopedVariableValues,
);
} else {
newDeferUsages.push(newDeferUsage);
Expand All @@ -283,7 +283,7 @@ function collectFieldsImpl(
groupedFieldSet,
newDeferUsages,
newDeferUsage,
newFragmentVariables,
newScopedVariableValues,
);
}
break;
Expand All @@ -300,16 +300,10 @@ function collectFieldsImpl(
function getDeferUsage(
operation: OperationDefinitionNode,
variableValues: { [variable: string]: unknown },
fragmentVariables: FragmentVariables | undefined,
node: FragmentSpreadNode | InlineFragmentNode,
parentDeferUsage: DeferUsage | undefined,
): DeferUsage | undefined {
const defer = getDirectiveValues(
GraphQLDeferDirective,
node,
variableValues,
fragmentVariables,
);
const defer = getDirectiveValues(GraphQLDeferDirective, node, variableValues);

if (!defer) {
return;
Expand All @@ -335,16 +329,10 @@ function getDeferUsage(
* directives, where `@skip` has higher precedence than `@include`.
*/
function shouldIncludeNode(
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
variableValues: { [variable: string]: unknown },
fragmentVariables: FragmentVariables | undefined,
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
): boolean {
const skip = getDirectiveValues(
GraphQLSkipDirective,
node,
variableValues,
fragmentVariables,
);
const skip = getDirectiveValues(GraphQLSkipDirective, node, variableValues);
if (skip?.if === true) {
return false;
}
Expand All @@ -353,7 +341,6 @@ function shouldIncludeNode(
GraphQLIncludeDirective,
node,
variableValues,
fragmentVariables,
);
if (include?.if === false) {
return false;
Expand Down
8 changes: 3 additions & 5 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,7 @@ function executeField(
const args = experimentalGetArgumentValues(
fieldGroup[0].node,
fieldDef.args,
exeContext.variableValues,
fieldGroup[0].fragmentVariables,
fieldGroup[0].scopedVariableValues ?? exeContext.variableValues,
);

// The resolve function's optional third argument is a context value that
Expand Down Expand Up @@ -1061,8 +1060,7 @@ function getStreamUsage(
const stream = getDirectiveValues(
GraphQLStreamDirective,
fieldGroup[0].node,
exeContext.variableValues,
fieldGroup[0].fragmentVariables,
fieldGroup[0].scopedVariableValues ?? exeContext.variableValues,
);

if (!stream) {
Expand Down Expand Up @@ -1091,7 +1089,7 @@ function getStreamUsage(
const streamedFieldGroup: FieldGroup = fieldGroup.map((fieldDetails) => ({
node: fieldDetails.node,
deferUsage: undefined,
fragmentVariables: fieldDetails.fragmentVariables,
scopedVariableValues: fieldDetails.scopedVariableValues,
}));

const streamUsage = {
Expand Down
20 changes: 4 additions & 16 deletions src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type { GraphQLSchema } from '../type/schema.js';
import { coerceInputValue } from '../utilities/coerceInputValue.js';
import { valueFromAST } from '../utilities/valueFromAST.js';

import type { FragmentVariables } from './collectFields.js';
import type { GraphQLVariableSignature } from './getVariableSignature.js';
import { getVariableSignature } from './getVariableSignature.js';

Expand Down Expand Up @@ -156,7 +155,6 @@ export function experimentalGetArgumentValues(
node: FieldNode | DirectiveNode | FragmentSpreadNode,
argDefs: ReadonlyArray<GraphQLArgument | GraphQLVariableSignature>,
variableValues: Maybe<ObjMap<unknown>>,
fragmentVariables?: Maybe<FragmentVariables>,
): { [argument: string]: unknown } {
const coercedValues: { [argument: string]: unknown } = {};

Expand Down Expand Up @@ -188,12 +186,9 @@ export function experimentalGetArgumentValues(

if (valueNode.kind === Kind.VARIABLE) {
const variableName = valueNode.name.value;
const scopedVariableValues = fragmentVariables?.signatures[variableName]
? fragmentVariables.values
: variableValues;
if (
scopedVariableValues == null ||
!Object.hasOwn(scopedVariableValues, variableName)
variableValues == null ||
!Object.hasOwn(variableValues, variableName)
) {
if (argDef.defaultValue !== undefined) {
coercedValues[name] = argDef.defaultValue;
Expand All @@ -206,7 +201,7 @@ export function experimentalGetArgumentValues(
}
continue;
}
isNull = scopedVariableValues[variableName] == null;
isNull = variableValues[variableName] == null;
}

if (isNull && isNonNullType(argType)) {
Expand All @@ -217,12 +212,7 @@ export function experimentalGetArgumentValues(
);
}

const coercedValue = valueFromAST(
valueNode,
argType,
variableValues,
fragmentVariables?.values,
);
const coercedValue = valueFromAST(valueNode, argType, variableValues);
if (coercedValue === undefined) {
// Note: ValuesOfCorrectTypeRule validation should catch this before
// execution. This is a runtime check to ensure execution does not
Expand Down Expand Up @@ -254,7 +244,6 @@ export function getDirectiveValues(
directiveDef: GraphQLDirective,
node: { readonly directives?: ReadonlyArray<DirectiveNode> | undefined },
variableValues?: Maybe<ObjMap<unknown>>,
fragmentVariables?: Maybe<FragmentVariables>,
): undefined | { [argument: string]: unknown } {
const directiveNode = node.directives?.find(
(directive) => directive.name.value === directiveDef.name,
Expand All @@ -265,7 +254,6 @@ export function getDirectiveValues(
directiveNode,
directiveDef.args,
variableValues,
fragmentVariables,
);
}
}
Loading
Loading