From 356af7dd133fe935a4917ee812ae389cd20680b0 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Wed, 24 Jul 2019 17:06:53 -0700 Subject: [PATCH 1/2] Fix causing operation name to sometimes be incorrectly done --- .../__tests__/gateway/queryPlanCache.test.ts | 49 +++++++++++++++++++ packages/apollo-gateway/src/index.ts | 9 ++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/apollo-gateway/src/__tests__/gateway/queryPlanCache.test.ts b/packages/apollo-gateway/src/__tests__/gateway/queryPlanCache.test.ts index 8687af947b7..8fb4223f666 100644 --- a/packages/apollo-gateway/src/__tests__/gateway/queryPlanCache.test.ts +++ b/packages/apollo-gateway/src/__tests__/gateway/queryPlanCache.test.ts @@ -59,3 +59,52 @@ it('caches the query plan for a request', async () => { expect(result.data).toEqual(secondResult.data); expect(planner.buildQueryPlan).toHaveBeenCalledTimes(1); }); + +it('supports multiple operations and operationName', async () => { + const query = `#graphql + query GetUser { + me { + username + } + } + query GetReviews { + topReviews { + body + } + } + `; + + const gateway = new ApolloGateway({ + localServiceList: [accounts, books, inventory, product, reviews], + buildService: service => { + return new LocalGraphQLDataSource(buildFederatedSchema([service])); + }, + }); + + const { schema, executor } = await gateway.load(); + + const server = new ApolloServer({ schema, executor }); + + const { data: userData } = await server.executeOperation({ + query, + operationName: 'GetUser', + }); + + const { data: reviewsData } = await server.executeOperation({ + query, + operationName: 'GetReviews', + }); + + expect(userData).toEqual({ + me: { username: '@ada' }, + }); + expect(reviewsData).toEqual({ + topReviews: [ + { body: 'Love it!' }, + { body: 'Too expensive.' }, + { body: 'Could be better.' }, + { body: 'Prefer something else.' }, + { body: 'Wish I had read this before.' }, + ], + }); +}); diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 9b1c26e7582..2e01a2e1021 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -260,6 +260,7 @@ export class ApolloGateway implements GraphQLService { >, ): Promise => { const { request, document, queryHash } = requestContext; + const queryPlanStoreKey = queryHash + request.operationName || ''; const operationContext = buildOperationContext( this.schema!, document, @@ -267,7 +268,7 @@ export class ApolloGateway implements GraphQLService { ); let queryPlan; if (this.queryPlanStore) { - queryPlan = await this.queryPlanStore.get(queryHash); + queryPlan = await this.queryPlanStore.get(queryPlanStoreKey); } if (!queryPlan) { @@ -285,9 +286,9 @@ export class ApolloGateway implements GraphQLService { // While it shouldn't normally be necessary to wrap this `Promise` in a // `Promise.resolve` invocation, it seems that the underlying cache store // is returning a non-native `Promise` (e.g. Bluebird, etc.). - Promise.resolve(this.queryPlanStore.set(queryHash, queryPlan)).catch( - err => this.logger.warn('Could not store queryPlan', err), - ); + Promise.resolve( + this.queryPlanStore.set(queryPlanStoreKey, queryPlan), + ).catch(err => this.logger.warn('Could not store queryPlan', err)); } } From 20d9a0027fef2e4bd5f3c4179e4e3e7c8620cef8 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Wed, 24 Jul 2019 17:09:43 -0700 Subject: [PATCH 2/2] Fix precedence error --- packages/apollo-gateway/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 2e01a2e1021..62ad858195e 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -260,7 +260,7 @@ export class ApolloGateway implements GraphQLService { >, ): Promise => { const { request, document, queryHash } = requestContext; - const queryPlanStoreKey = queryHash + request.operationName || ''; + const queryPlanStoreKey = queryHash + (request.operationName || ''); const operationContext = buildOperationContext( this.schema!, document,