|
| 1 | +import { ApolloServer } from '@apollo/server'; |
| 2 | +import cacheDirective from '@src/directives/cache'; |
| 3 | +import { buildSchema } from './util'; |
| 4 | +import assert from 'assert'; |
| 5 | + |
| 6 | +jest.useFakeTimers(); |
| 7 | +jest.spyOn(global, 'setTimeout'); |
| 8 | + |
| 9 | +const cache = new Map<string, string>() |
| 10 | + |
| 11 | +const cacheCallback = { |
| 12 | + has: jest.fn(key => cache.has(key)), |
| 13 | + get: jest.fn(key => cache.get(key)), |
| 14 | + delete: jest.fn(key => cache.delete(key)), |
| 15 | + set: jest.fn((key: string, value: string) => { |
| 16 | + cache.set(key, value) |
| 17 | + }), |
| 18 | +} |
| 19 | + |
| 20 | +const { cacheDirectiveTypeDefs, cacheDirectiveTransformer } = cacheDirective('cache', cacheCallback) |
| 21 | + |
| 22 | +describe('@cache directive', () => { |
| 23 | + let testServer: ApolloServer; |
| 24 | + |
| 25 | + const resolvers = { |
| 26 | + Query: { |
| 27 | + user: () => ({ |
| 28 | + age: 28 |
| 29 | + }) |
| 30 | + }, |
| 31 | + }; |
| 32 | + |
| 33 | + const testQuery = ` |
| 34 | + query ExampleQuery { |
| 35 | + user { |
| 36 | + age |
| 37 | + } |
| 38 | + } |
| 39 | + ` |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + if (testServer) { |
| 43 | + await testServer.stop() |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + it('will cache a field value before returning a response with correct ttl', async () => { |
| 48 | + const ttl = 8000 |
| 49 | + const schema = buildSchema({ |
| 50 | + typeDefs: [ |
| 51 | + `type User { |
| 52 | + age: Int @cache(key: "user_age", ttl: ${ttl}) |
| 53 | + } |
| 54 | + |
| 55 | + type Query { |
| 56 | + user: User |
| 57 | + } |
| 58 | + `, |
| 59 | + cacheDirectiveTypeDefs, |
| 60 | + ], |
| 61 | + resolvers, |
| 62 | + transformers: [cacheDirectiveTransformer], |
| 63 | + }) |
| 64 | + |
| 65 | + testServer = new ApolloServer({ schema }) |
| 66 | + |
| 67 | + const response = await testServer.executeOperation<{ user: { age: number } }>({ |
| 68 | + query: testQuery |
| 69 | + }) |
| 70 | + |
| 71 | + assert(response.body.kind === 'single'); |
| 72 | + expect(response.body.singleResult.errors).toBeUndefined(); |
| 73 | + expect(response.body.singleResult.data.user.age).toEqual(28); |
| 74 | + expect(setTimeout).toHaveBeenCalledTimes(1); |
| 75 | + expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), ttl); |
| 76 | + expect(cacheCallback.set).toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it('will delete a cache field value if the ttl has long expired', async () => { |
| 80 | + const ttl = 3000 |
| 81 | + const key = 'user_age' |
| 82 | + const schema = buildSchema({ |
| 83 | + typeDefs: [ |
| 84 | + `type User { |
| 85 | + age: Int @cache(key: "${key}", ttl: ${ttl}) |
| 86 | + } |
| 87 | + |
| 88 | + type Query { |
| 89 | + user: User |
| 90 | + } |
| 91 | + `, |
| 92 | + cacheDirectiveTypeDefs, |
| 93 | + ], |
| 94 | + resolvers, |
| 95 | + transformers: [cacheDirectiveTransformer], |
| 96 | + }) |
| 97 | + |
| 98 | + testServer = new ApolloServer({ schema }) |
| 99 | + |
| 100 | + const response = await testServer.executeOperation<{ user: { age: number } }>({ |
| 101 | + query: testQuery |
| 102 | + }) |
| 103 | + |
| 104 | + assert(response.body.kind === 'single'); |
| 105 | + expect(response.body.singleResult.errors).toBeUndefined(); |
| 106 | + expect(response.body.singleResult.data.user.age).toEqual(28); |
| 107 | + |
| 108 | + expect(cacheCallback.set).toHaveBeenCalled() |
| 109 | + expect(cacheCallback.set).toHaveBeenCalledWith(key, JSON.stringify(response.body.singleResult.data.user.age)) |
| 110 | + |
| 111 | + jest.advanceTimersByTime(ttl + 5000) |
| 112 | + |
| 113 | + expect(cacheCallback.delete).toHaveBeenCalled() |
| 114 | + expect(cacheCallback.delete).toHaveBeenCalledWith(key) |
| 115 | + }) |
| 116 | +}) |
0 commit comments