Skip to content

Commit

Permalink
do not send variables with value undefined
Browse files Browse the repository at this point in the history
graphql-js converts these to null
See graphql/graphql-js#2533
  • Loading branch information
yaacovCR committed May 8, 2020
1 parent 21ff648 commit a034acb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/delegate/createRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ export function createRequest({
sourceSchema,
def.type as NamedTypeNode,
) as GraphQLInputType;
newVariables[varName] = serializeInputValue(
const serializedValue = serializeInputValue(
varType,
variableValues[varName],
);
if (serializedValue !== undefined) {
newVariables[varName] = serializedValue;
}
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/delegate/transforms/FilterToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ function filterToSchema(
});

const newVariables = usedVariables.reduce((acc, variableName) => {
acc[variableName] = variables[variableName];
const variableValue = variables[variableName];
if (variableValue !== undefined) {
acc[variableName] = variableValue;
}
return acc;
}, {});

Expand Down
2 changes: 1 addition & 1 deletion src/stitch/typeFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function makeValues(
[node.name.value]: {
type: createStub(node.type, 'input'),
defaultValue:
node.defaultValue != null
node.defaultValue !== undefined
? valueFromASTUntyped(node.defaultValue)
: undefined,
description: getDescription(node, backcompatOptions),
Expand Down
66 changes: 66 additions & 0 deletions src/test/alternateStitchSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,72 @@ describe('transform object fields', () => {
});
});

describe('optional arguments', () => {
const schema = makeExecutableSchema({
typeDefs: `
enum Arg {
possibleArg
}
type Query {
test(arg: Arg): Boolean
}
`,
resolvers: {
Query: {
test: (_root, args, _context) => args.arg === undefined
}
}
});

const stitchedSchema = stitchSchemas({
schemas: [schema],
});

it('work with schema stitching', async () => {
const query = `
{
test
}
`;

const originalResult = await graphql(schema, query);
expect(originalResult.data.test).toEqual(true);

const stitchedResult = await graphql(stitchedSchema, query);
expect(stitchedResult.data.test).toEqual(true);
});

it('work with schema stitching when using variables', async () => {
const query = `
query test($arg: Arg) {
test(arg: $arg)
}
`;

const originalResult = await graphql(schema, query);
expect(originalResult.data.test).toEqual(true);

const stitchedResult = await graphql(stitchedSchema, query);
expect(stitchedResult.data.test).toEqual(true);
});

// See https://github.com/graphql/graphql-js/issues/2533
it('may not work as expected when explicitly passing in an undefined value', async () => {
const query = `
query test($arg: Arg) {
test(arg: $arg)
}
`;

const originalResult = await graphql(schema, query, {}, {}, { arg: undefined });
expect(originalResult.data.test).toEqual(false);

const stitchedResult = await graphql(stitchedSchema, query, {}, {}, { arg: undefined });
expect(stitchedResult.data.test).toEqual(false);
});
});


describe('default values', () => {
test('should work to add a default value even when renaming root fields', async () => {
const transformedPropertySchema = transformSchema(propertySchema, [
Expand Down
6 changes: 5 additions & 1 deletion src/utils/updateArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ export function updateArgument(
type: astFromType(argType),
};

variableValues[varName] = newArg;
if (newArg === undefined) {
delete variableValues[varName];
} else {
variableValues[varName] = newArg;
}
}

0 comments on commit a034acb

Please sign in to comment.