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

Make queryPlanStoreKey a hash of both the query and Operation name #2310

Merged
8 changes: 8 additions & 0 deletions .changeset/fluffy-toes-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@apollo/gateway": patch
---

Previously the `queryPlanStoreKey` was a hash of the query concatenated with an unhashed `operationName` if it was present. This resulted in variable length cache keys that could become unnecessarily long, occupying additional space in the query plan cache.

This change incorporates the `operationName` _into_ the hash itself (if `operationName` is present).

6 changes: 4 additions & 2 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Logger } from '@apollo/utils.logger';
import LRUCache from 'lru-cache';
import {
GraphQLSchema,
VariableDefinitionNode,
VariableDefinitionNode
} from 'graphql';
import { buildOperationContext, OperationContext } from './operationContext';
import {
Expand Down Expand Up @@ -750,7 +750,9 @@ export class ApolloGateway implements GatewayInterface {
async (span) => {
try {
const { request, document, queryHash } = requestContext;
const queryPlanStoreKey = queryHash + (request.operationName || '');
const queryPlanStoreKey = request.operationName ?
createHash('sha256').update(queryHash).update(request.operationName).digest('hex')
: queryHash;
const operationContext = buildOperationContext({
schema: this.schema!,
operationDocument: document,
Expand Down