Skip to content

Commit

Permalink
Merge pull request #3084 from apollographql/jackson/fix-operationName…
Browse files Browse the repository at this point in the history
…-bug

Fix causing multiple operations to sometimes be incorrectly executed
  • Loading branch information
James Baxley authored Jul 25, 2019
2 parents 3fb9208 + 20d9a00 commit 7660727
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.' },
],
});
});
9 changes: 5 additions & 4 deletions packages/apollo-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,15 @@ export class ApolloGateway implements GraphQLService {
>,
): Promise<GraphQLExecutionResult> => {
const { request, document, queryHash } = requestContext;
const queryPlanStoreKey = queryHash + (request.operationName || '');
const operationContext = buildOperationContext(
this.schema!,
document,
request.operationName,
);
let queryPlan;
if (this.queryPlanStore) {
queryPlan = await this.queryPlanStore.get(queryHash);
queryPlan = await this.queryPlanStore.get(queryPlanStoreKey);
}

if (!queryPlan) {
Expand All @@ -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));
}
}

Expand Down

0 comments on commit 7660727

Please sign in to comment.