Skip to content

Commit a034acb

Browse files
committed
do not send variables with value undefined
graphql-js converts these to null See graphql/graphql-js#2533
1 parent 21ff648 commit a034acb

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

src/delegate/createRequest.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ export function createRequest({
113113
sourceSchema,
114114
def.type as NamedTypeNode,
115115
) as GraphQLInputType;
116-
newVariables[varName] = serializeInputValue(
116+
const serializedValue = serializeInputValue(
117117
varType,
118118
variableValues[varName],
119119
);
120+
if (serializedValue !== undefined) {
121+
newVariables[varName] = serializedValue;
122+
}
120123
});
121124
}
122125

src/delegate/transforms/FilterToSchema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ function filterToSchema(
142142
});
143143

144144
const newVariables = usedVariables.reduce((acc, variableName) => {
145-
acc[variableName] = variables[variableName];
145+
const variableValue = variables[variableName];
146+
if (variableValue !== undefined) {
147+
acc[variableName] = variableValue;
148+
}
146149
return acc;
147150
}, {});
148151

src/stitch/typeFromAST.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function makeValues(
188188
[node.name.value]: {
189189
type: createStub(node.type, 'input'),
190190
defaultValue:
191-
node.defaultValue != null
191+
node.defaultValue !== undefined
192192
? valueFromASTUntyped(node.defaultValue)
193193
: undefined,
194194
description: getDescription(node, backcompatOptions),

src/test/alternateStitchSchemas.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,72 @@ describe('transform object fields', () => {
426426
});
427427
});
428428

429+
describe('optional arguments', () => {
430+
const schema = makeExecutableSchema({
431+
typeDefs: `
432+
enum Arg {
433+
possibleArg
434+
}
435+
type Query {
436+
test(arg: Arg): Boolean
437+
}
438+
`,
439+
resolvers: {
440+
Query: {
441+
test: (_root, args, _context) => args.arg === undefined
442+
}
443+
}
444+
});
445+
446+
const stitchedSchema = stitchSchemas({
447+
schemas: [schema],
448+
});
449+
450+
it('work with schema stitching', async () => {
451+
const query = `
452+
{
453+
test
454+
}
455+
`;
456+
457+
const originalResult = await graphql(schema, query);
458+
expect(originalResult.data.test).toEqual(true);
459+
460+
const stitchedResult = await graphql(stitchedSchema, query);
461+
expect(stitchedResult.data.test).toEqual(true);
462+
});
463+
464+
it('work with schema stitching when using variables', async () => {
465+
const query = `
466+
query test($arg: Arg) {
467+
test(arg: $arg)
468+
}
469+
`;
470+
471+
const originalResult = await graphql(schema, query);
472+
expect(originalResult.data.test).toEqual(true);
473+
474+
const stitchedResult = await graphql(stitchedSchema, query);
475+
expect(stitchedResult.data.test).toEqual(true);
476+
});
477+
478+
// See https://github.com/graphql/graphql-js/issues/2533
479+
it('may not work as expected when explicitly passing in an undefined value', async () => {
480+
const query = `
481+
query test($arg: Arg) {
482+
test(arg: $arg)
483+
}
484+
`;
485+
486+
const originalResult = await graphql(schema, query, {}, {}, { arg: undefined });
487+
expect(originalResult.data.test).toEqual(false);
488+
489+
const stitchedResult = await graphql(stitchedSchema, query, {}, {}, { arg: undefined });
490+
expect(stitchedResult.data.test).toEqual(false);
491+
});
492+
});
493+
494+
429495
describe('default values', () => {
430496
test('should work to add a default value even when renaming root fields', async () => {
431497
const transformedPropertySchema = transformSchema(propertySchema, [

src/utils/updateArgument.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@ export function updateArgument(
4747
type: astFromType(argType),
4848
};
4949

50-
variableValues[varName] = newArg;
50+
if (newArg === undefined) {
51+
delete variableValues[varName];
52+
} else {
53+
variableValues[varName] = newArg;
54+
}
5155
}

0 commit comments

Comments
 (0)