Skip to content

Commit e0db95b

Browse files
authored
fix: updated documentstore to use schema hash (#7433)
Previously, when users provided their own `documentStore`, Apollo Server used a random prefix per schema in order to guarantee there was no shared state from one schema to the next. Now Apollo Server uses a hash of the schema, which enables the provided document store to be shared if you choose to do so.
1 parent 92be5ac commit e0db95b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

.changeset/wet-berries-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@apollo/server': patch
3+
---
4+
5+
Previously, when users provided their own `documentStore`, Apollo Server used a random prefix per schema in order to guarantee there was no shared state from one schema to the next. Now Apollo Server uses a hash of the schema, which enables the provided document store to be shared if you choose to do so.

packages/server/src/ApolloServer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type GraphQLSchema,
1212
type ParseOptions,
1313
print,
14+
printSchema,
1415
type TypedQueryDocumentNode,
1516
type ValidationContext,
1617
type ValidationRule,
@@ -22,7 +23,6 @@ import {
2223
} from '@apollo/utils.keyvaluecache';
2324
import loglevel from 'loglevel';
2425
import Negotiator from 'negotiator';
25-
import * as uuid from 'uuid';
2626
import { newCachePolicy } from './cachePolicy.js';
2727
import { determineApolloConfig } from './determineApolloConfig.js';
2828
import {
@@ -63,6 +63,7 @@ import { newHTTPGraphQLHead, prettyJSONStringify } from './runHttpQuery.js';
6363
import { SchemaManager } from './utils/schemaManager.js';
6464
import { isDefined } from './utils/isDefined.js';
6565
import { UnreachableCaseError } from './utils/UnreachableCaseError.js';
66+
import { computeCoreSchemaHash } from './utils/computeCoreSchemaHash.js';
6667
import type { WithRequired } from '@apollo/utils.withrequired';
6768
import type { ApolloServerOptionsWithStaticSchema } from './externalTypes/constructor.js';
6869
import type { GatewayExecutor } from '@apollo/server-gateway-interface';
@@ -728,13 +729,15 @@ export class ApolloServer<in out TContext extends BaseContext = BaseContext> {
728729
// same DocumentStore for different schemas because that might make us
729730
// treat invalid operations as valid. If we're using the default
730731
// DocumentStore, then we just create it from scratch each time we get a
731-
// new schema. If we're using a user-provided DocumentStore, then we use a
732-
// random prefix each time we get a new schema.
732+
// new schema. If we're using a user-provided DocumentStore, then we use
733+
// the schema hash as a prefix.
733734
documentStore:
734735
providedDocumentStore === undefined
735736
? new InMemoryLRUCache<DocumentNode>()
736737
: providedDocumentStore,
737-
documentStoreKeyPrefix: providedDocumentStore ? `${uuid.v4()}:` : '',
738+
documentStoreKeyPrefix: providedDocumentStore
739+
? `${computeCoreSchemaHash(printSchema(schema))}:`
740+
: '',
738741
};
739742
}
740743

packages/server/src/__tests__/documentStore.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import gql from 'graphql-tag';
44
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
55
import { ApolloServer } from '..';
66
import { jest, describe, it, expect } from '@jest/globals';
7+
import { makeExecutableSchema } from '@graphql-tools/schema';
8+
import { printSchema } from 'graphql/index';
9+
import { computeCoreSchemaHash } from '../utils/computeCoreSchemaHash';
710

811
const typeDefs = gql`
912
type Query {
@@ -81,11 +84,16 @@ describe('ApolloServer documentStore', () => {
8184
const keys = documentStore.keys();
8285
expect(keys).toHaveLength(1);
8386
const theKey = keys[0];
84-
const [uuid, hash] = theKey.split(':');
85-
expect(typeof uuid).toBe('string');
86-
expect(hash).toEqual(operations.simple.hash);
8787

88-
const result = await documentStore.get(`${uuid}:${hash}`);
88+
const schema = makeExecutableSchema({ typeDefs, resolvers });
89+
const expectedSchemaHash = computeCoreSchemaHash(printSchema(schema));
90+
91+
const [schemaHash, documentHash] = theKey.split(':');
92+
expect(typeof schemaHash).toBe('string');
93+
expect(schemaHash).toEqual(expectedSchemaHash);
94+
expect(documentHash).toEqual(operations.simple.hash);
95+
96+
const result = await documentStore.get(`${schemaHash}:${documentHash}`);
8997
expect(result).toMatchObject(documentNodeMatcher);
9098

9199
await server.executeOperation(operations.simple.op);

0 commit comments

Comments
 (0)