Skip to content

Commit 356af7d

Browse files
author
Jackson Kearl
committed
Fix causing operation name to sometimes be incorrectly done
1 parent 3fb9208 commit 356af7d

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/apollo-gateway/src/__tests__/gateway/queryPlanCache.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,52 @@ it('caches the query plan for a request', async () => {
5959
expect(result.data).toEqual(secondResult.data);
6060
expect(planner.buildQueryPlan).toHaveBeenCalledTimes(1);
6161
});
62+
63+
it('supports multiple operations and operationName', async () => {
64+
const query = `#graphql
65+
query GetUser {
66+
me {
67+
username
68+
}
69+
}
70+
query GetReviews {
71+
topReviews {
72+
body
73+
}
74+
}
75+
`;
76+
77+
const gateway = new ApolloGateway({
78+
localServiceList: [accounts, books, inventory, product, reviews],
79+
buildService: service => {
80+
return new LocalGraphQLDataSource(buildFederatedSchema([service]));
81+
},
82+
});
83+
84+
const { schema, executor } = await gateway.load();
85+
86+
const server = new ApolloServer({ schema, executor });
87+
88+
const { data: userData } = await server.executeOperation({
89+
query,
90+
operationName: 'GetUser',
91+
});
92+
93+
const { data: reviewsData } = await server.executeOperation({
94+
query,
95+
operationName: 'GetReviews',
96+
});
97+
98+
expect(userData).toEqual({
99+
me: { username: '@ada' },
100+
});
101+
expect(reviewsData).toEqual({
102+
topReviews: [
103+
{ body: 'Love it!' },
104+
{ body: 'Too expensive.' },
105+
{ body: 'Could be better.' },
106+
{ body: 'Prefer something else.' },
107+
{ body: 'Wish I had read this before.' },
108+
],
109+
});
110+
});

packages/apollo-gateway/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,15 @@ export class ApolloGateway implements GraphQLService {
260260
>,
261261
): Promise<GraphQLExecutionResult> => {
262262
const { request, document, queryHash } = requestContext;
263+
const queryPlanStoreKey = queryHash + request.operationName || '';
263264
const operationContext = buildOperationContext(
264265
this.schema!,
265266
document,
266267
request.operationName,
267268
);
268269
let queryPlan;
269270
if (this.queryPlanStore) {
270-
queryPlan = await this.queryPlanStore.get(queryHash);
271+
queryPlan = await this.queryPlanStore.get(queryPlanStoreKey);
271272
}
272273

273274
if (!queryPlan) {
@@ -285,9 +286,9 @@ export class ApolloGateway implements GraphQLService {
285286
// While it shouldn't normally be necessary to wrap this `Promise` in a
286287
// `Promise.resolve` invocation, it seems that the underlying cache store
287288
// is returning a non-native `Promise` (e.g. Bluebird, etc.).
288-
Promise.resolve(this.queryPlanStore.set(queryHash, queryPlan)).catch(
289-
err => this.logger.warn('Could not store queryPlan', err),
290-
);
289+
Promise.resolve(
290+
this.queryPlanStore.set(queryPlanStoreKey, queryPlan),
291+
).catch(err => this.logger.warn('Could not store queryPlan', err));
291292
}
292293
}
293294

0 commit comments

Comments
 (0)