Skip to content

fix(replaceVariables): preserve sources for fragment variable values #4389

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

Merged
merged 6 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
101 changes: 101 additions & 0 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
import { GraphQLBoolean, GraphQLString } from '../../type/scalars.js';
import { GraphQLSchema } from '../../type/schema.js';

import { valueFromASTUntyped } from '../../utilities/valueFromASTUntyped.js';

import { executeSync, experimentalExecuteIncrementally } from '../execute.js';
import { getVariableValues } from '../values.js';

Expand Down Expand Up @@ -64,6 +66,16 @@ const TestComplexScalar = new GraphQLScalarType({
},
});

const TestJSONScalar = new GraphQLScalarType({
name: 'JSONScalar',
coerceInputValue(value) {
return value;
},
coerceInputLiteral(value) {
return valueFromASTUntyped(value);
},
});

const NestedType: GraphQLObjectType = new GraphQLObjectType({
name: 'NestedType',
fields: {
Expand Down Expand Up @@ -151,6 +163,7 @@ const TestType = new GraphQLObjectType({
fieldWithNestedInputObject: fieldWithInputArg({
type: TestNestedInputObject,
}),
fieldWithJSONScalarInput: fieldWithInputArg({ type: TestJSONScalar }),
list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }),
nested: {
type: NestedType,
Expand Down Expand Up @@ -859,6 +872,94 @@ describe('Execute: Handles inputs', () => {
});
});

// Note: the below is non-specified custom graphql-js behavior.
describe('Handles custom scalars with embedded variables', () => {
it('allows custom scalars', () => {
const result = executeQuery(`
{
fieldWithJSONScalarInput(input: { a: "foo", b: ["bar"], c: "baz" })
}
`);

expectJSON(result).toDeepEqual({
data: {
fieldWithJSONScalarInput: '{ a: "foo", b: ["bar"], c: "baz" }',
},
});
});

it('allows custom scalars with non-embedded variables', () => {
const result = executeQuery(
`
query ($input: JSONScalar) {
fieldWithJSONScalarInput(input: $input)
}
`,
{ input: { a: 'foo', b: ['bar'], c: 'baz' } },
);

expectJSON(result).toDeepEqual({
data: {
fieldWithJSONScalarInput: '{ a: "foo", b: ["bar"], c: "baz" }',
},
});
});

it('allows custom scalars with embedded operation variables', () => {
const result = executeQuery(
`
query ($input: String) {
fieldWithJSONScalarInput(input: { a: $input, b: ["bar"], c: "baz" })
}
`,
{ input: 'foo' },
);

expectJSON(result).toDeepEqual({
data: {
fieldWithJSONScalarInput: '{ a: "foo", b: ["bar"], c: "baz" }',
},
});
});

it('allows custom scalars with embedded fragment variables', () => {
const result = executeQueryWithFragmentArguments(`
{
...JSONFragment(input: "foo")
}
fragment JSONFragment($input: String) on TestType {
fieldWithJSONScalarInput(input: { a: $input, b: ["bar"], c: "baz" })
}
`);

expectJSON(result).toDeepEqual({
data: {
fieldWithJSONScalarInput: '{ a: "foo", b: ["bar"], c: "baz" }',
},
});
});

it('allows custom scalars with embedded nested fragment variables', () => {
const result = executeQueryWithFragmentArguments(`
{
...JSONFragment(input1: "foo")
}
fragment JSONFragment($input1: String) on TestType {
...JSONNestedFragment(input2: $input1)
}
fragment JSONNestedFragment($input2: String) on TestType {
fieldWithJSONScalarInput(input: { a: $input2, b: ["bar"], c: "baz" })
}
`);

expectJSON(result).toDeepEqual({
data: {
fieldWithJSONScalarInput: '{ a: "foo", b: ["bar"], c: "baz" }',
},
});
});
});

describe('Handles lists and nullability', () => {
it('allows lists to be null', () => {
const doc = `
Expand Down
34 changes: 23 additions & 11 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AccumulatorMap } from '../jsutils/AccumulatorMap.js';
import type { ObjMap } from '../jsutils/ObjMap.js';
import type { ObjMap, ReadOnlyObjMap } from '../jsutils/ObjMap.js';

import type {
ConstValueNode,
DirectiveNode,
FieldNode,
FragmentDefinitionNode,
Expand All @@ -25,7 +26,7 @@ import { typeFromAST } from '../utilities/typeFromAST.js';
import type { GraphQLVariableSignature } from './getVariableSignature.js';
import type { VariableValues } from './values.js';
import {
experimentalGetArgumentValues,
getArgumentValues,
getDirectiveValues,
getFragmentVariableValues,
} from './values.js';
Expand All @@ -35,10 +36,21 @@ export interface DeferUsage {
parentDeferUsage: DeferUsage | undefined;
}

export interface FragmentVariableValues {
readonly sources: ReadOnlyObjMap<FragmentVariableValueSource>;
readonly coerced: ReadOnlyObjMap<unknown>;
}

interface FragmentVariableValueSource {
readonly signature: GraphQLVariableSignature;
readonly value?: ConstValueNode;
readonly fragmentVariableValues?: FragmentVariableValues;
}

export interface FieldDetails {
node: FieldNode;
deferUsage?: DeferUsage | undefined;
fragmentVariableValues?: VariableValues | undefined;
fragmentVariableValues?: FragmentVariableValues | undefined;
}

export type FieldDetailsList = ReadonlyArray<FieldDetails>;
Expand Down Expand Up @@ -168,7 +180,7 @@ function collectFieldsImpl(
groupedFieldSet: AccumulatorMap<string, FieldDetails>,
newDeferUsages: Array<DeferUsage>,
deferUsage?: DeferUsage,
fragmentVariableValues?: VariableValues,
fragmentVariableValues?: FragmentVariableValues,
): void {
const {
schema,
Expand Down Expand Up @@ -273,7 +285,7 @@ function collectFieldsImpl(
);

const fragmentVariableSignatures = fragment.variableSignatures;
let newFragmentVariableValues: VariableValues | undefined;
let newFragmentVariableValues: FragmentVariableValues | undefined;
if (fragmentVariableSignatures) {
newFragmentVariableValues = getFragmentVariableValues(
selection,
Expand Down Expand Up @@ -318,7 +330,7 @@ function collectFieldsImpl(
*/
function getDeferUsage(
variableValues: VariableValues,
fragmentVariableValues: VariableValues | undefined,
fragmentVariableValues: FragmentVariableValues | undefined,
node: FragmentSpreadNode | InlineFragmentNode,
parentDeferUsage: DeferUsage | undefined,
): DeferUsage | undefined {
Expand Down Expand Up @@ -351,7 +363,7 @@ function shouldIncludeNode(
context: CollectFieldsContext,
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
variableValues: VariableValues,
fragmentVariableValues: VariableValues | undefined,
fragmentVariableValues: FragmentVariableValues | undefined,
): boolean {
const skipDirectiveNode = node.directives?.find(
(directive) => directive.name.value === GraphQLSkipDirective.name,
Expand All @@ -361,9 +373,9 @@ function shouldIncludeNode(
return false;
}
const skip = skipDirectiveNode
? experimentalGetArgumentValues(
? getArgumentValues(
GraphQLSkipDirective,
skipDirectiveNode,
GraphQLSkipDirective.args,
variableValues,
fragmentVariableValues,
context.hideSuggestions,
Expand All @@ -381,9 +393,9 @@ function shouldIncludeNode(
return false;
}
const include = includeDirectiveNode
? experimentalGetArgumentValues(
? getArgumentValues(
GraphQLIncludeDirective,
includeDirectiveNode,
GraphQLIncludeDirective.args,
variableValues,
fragmentVariableValues,
context.hideSuggestions,
Expand Down
6 changes: 3 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import type {
import { DeferredFragmentRecord } from './types.js';
import type { VariableValues } from './values.js';
import {
experimentalGetArgumentValues,
getArgumentValues,
getDirectiveValues,
getVariableValues,
Expand Down Expand Up @@ -877,9 +876,9 @@ function executeField(
// Build a JS object of arguments from the field.arguments AST, using the
// variables scope to fulfill any variable references.
// TODO: find a way to memoize, in case this field is within a List type.
const args = experimentalGetArgumentValues(
const args = getArgumentValues(
fieldDef,
fieldDetailsList[0].node,
fieldDef.args,
variableValues,
fieldDetailsList[0].fragmentVariableValues,
hideSuggestions,
Expand Down Expand Up @@ -2298,6 +2297,7 @@ function executeSubscription(
fieldDef,
fieldNodes[0],
variableValues,
fieldDetailsList[0].fragmentVariableValues,
hideSuggestions,
);

Expand Down
Loading
Loading