-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pluggable documentStore replacing experimental option (#5644)
Co-authored-by: David Glasser <[email protected]> Co-authored-by: Stephen Barlow <[email protected]>
- Loading branch information
1 parent
4efa414
commit b2c37ae
Showing
9 changed files
with
209 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/apollo-server-core/src/__tests__/documentStore.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import gql from 'graphql-tag'; | ||
import type { DocumentNode } from 'graphql'; | ||
|
||
import { ApolloServerBase } from '../ApolloServer'; | ||
import { InMemoryLRUCache } from 'apollo-server-caching'; | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
hello() { | ||
return 'world'; | ||
}, | ||
}, | ||
}; | ||
|
||
// allow us to access internals of the class | ||
class ApolloServerObservable extends ApolloServerBase { | ||
override graphQLServerOptions() { | ||
return super.graphQLServerOptions(); | ||
} | ||
} | ||
|
||
const documentNodeMatcher = { | ||
kind: 'Document', | ||
definitions: expect.any(Array), | ||
loc: { | ||
start: 0, | ||
end: 15, | ||
}, | ||
}; | ||
|
||
const operations = { | ||
simple: { | ||
op: { query: 'query { hello }' }, | ||
hash: 'ec2e01311ab3b02f3d8c8c712f9e579356d332cd007ac4c1ea5df727f482f05f', | ||
}, | ||
}; | ||
|
||
describe('ApolloServerBase documentStore', () => { | ||
it('documentStore - undefined', async () => { | ||
const server = new ApolloServerObservable({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
|
||
await server.start(); | ||
|
||
const options = await server.graphQLServerOptions(); | ||
const embeddedStore = options.documentStore as any; | ||
expect(embeddedStore).toBeInstanceOf(InMemoryLRUCache); | ||
|
||
await server.executeOperation(operations.simple.op); | ||
|
||
expect(await embeddedStore.getTotalSize()).toBe(403); | ||
expect(await embeddedStore.get(operations.simple.hash)).toMatchObject( | ||
documentNodeMatcher, | ||
); | ||
}); | ||
|
||
it('documentStore - custom', async () => { | ||
const documentStore = { | ||
get: async function (key: string) { | ||
return cache[key]; | ||
}, | ||
set: async function (key: string, val: DocumentNode) { | ||
cache[key] = val; | ||
}, | ||
delete: async function () {}, | ||
}; | ||
const cache: Record<string, DocumentNode> = {}; | ||
|
||
const getSpy = jest.spyOn(documentStore, 'get'); | ||
const setSpy = jest.spyOn(documentStore, 'set'); | ||
|
||
const server = new ApolloServerBase({ | ||
typeDefs, | ||
resolvers, | ||
documentStore, | ||
}); | ||
await server.start(); | ||
|
||
await server.executeOperation(operations.simple.op); | ||
|
||
expect(Object.keys(cache)).toEqual([operations.simple.hash]); | ||
expect(cache[operations.simple.hash]).toMatchObject(documentNodeMatcher); | ||
|
||
await server.executeOperation(operations.simple.op); | ||
|
||
expect(Object.keys(cache)).toEqual([operations.simple.hash]); | ||
|
||
expect(getSpy.mock.calls.length).toBe(2); | ||
expect(setSpy.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('documentStore - null', async () => { | ||
const server = new ApolloServerObservable({ | ||
typeDefs, | ||
resolvers, | ||
documentStore: null, | ||
}); | ||
|
||
await server.start(); | ||
|
||
const options = await server.graphQLServerOptions(); | ||
expect(options.documentStore).toBe(null); | ||
|
||
const result = await server.executeOperation(operations.simple.op); | ||
|
||
expect(result.data).toEqual({ hello: 'world' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.