Skip to content
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

Deduplicate variable definitions for subqueries #2840

Merged
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
77 changes: 77 additions & 0 deletions packages/apollo-gateway/src/__tests__/executeQueryPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,83 @@ describe('executeQueryPlan', () => {
`);
});

it('should not duplicate variable definitions', async () => {
const query = gql`
query Test($first: Int!) {
first: topReviews(first: $first) {
body
author {
name
}
}
second: topReviews(first: $first) {
body
author {
name
}
}
}
`;

const operationContext = buildOperationContext(schema, query);
const queryPlan = buildQueryPlan(operationContext);

const requestContext = buildRequestContext();
requestContext.request.variables = { first: 3 };

const response = await executeQueryPlan(
queryPlan,
serviceMap,
requestContext,
operationContext,
);

expect(response.data).toMatchInlineSnapshot(`
Object {
"first": Array [
Object {
"author": Object {
"name": "Ada Lovelace",
},
"body": "Love it!",
},
Object {
"author": Object {
"name": "Ada Lovelace",
},
"body": "Too expensive.",
},
Object {
"author": Object {
"name": "Alan Turing",
},
"body": "Could be better.",
},
],
"second": Array [
Object {
"author": Object {
"name": "Ada Lovelace",
},
"body": "Love it!",
},
Object {
"author": Object {
"name": "Ada Lovelace",
},
"body": "Too expensive.",
},
Object {
"author": Object {
"name": "Alan Turing",
},
"body": "Could be better.",
},
],
}
`);
});

it('can execute an introspection query', async () => {
const operationContext = buildOperationContext(
schema,
Expand Down
21 changes: 17 additions & 4 deletions packages/apollo-gateway/src/executeQueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,27 @@ function mapFetchNodeToVariableDefinitions(
node: FetchNode,
): VariableDefinitionNode[] {
const variableUsage = node.variableUsages;
return variableUsage
? variableUsage.map(({ node, type }) => ({
if (!variableUsage) {
return [];
}

const variableMap = variableUsage.reduce((map, { node, type }) => {
const key = `${node.name.value}_${type.toString()}`;

if (!map.has(key)) {
map.set(key, {
kind: Kind.VARIABLE_DEFINITION,
variable: node,
type: astFromType(type),
}))
: [];
});
}

return map;
}, new Map<string, VariableDefinitionNode>());

return Array.from(variableMap.values());
}

function operationForRootFetch(
fetch: FetchNode,
operation: OperationTypeNode = 'query',
Expand Down