From a64b62075f4f90ab679102df5e5e3cdae6925aa8 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Tue, 12 Mar 2019 17:08:40 -0700 Subject: [PATCH] Basic end-to-end test Unit tests of all the pluginn options forthcoming --- .../src/__tests__/collectCacheControlHints.ts | 7 +- packages/apollo-cache-control/src/index.ts | 9 ++- .../src/ApolloServer.ts | 76 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/packages/apollo-cache-control/src/__tests__/collectCacheControlHints.ts b/packages/apollo-cache-control/src/__tests__/collectCacheControlHints.ts index 1730e5c32c1..e69e8bc448e 100644 --- a/packages/apollo-cache-control/src/__tests__/collectCacheControlHints.ts +++ b/packages/apollo-cache-control/src/__tests__/collectCacheControlHints.ts @@ -17,7 +17,12 @@ export async function collectCacheControlHints( ): Promise { enableGraphQLExtensions(schema); - const cacheControlExtension = new CacheControlExtension(options); + // Because this test helper looks at the formatted extensions, we always want + // to include them. + const cacheControlExtension = new CacheControlExtension({ + ...options, + stripFormattedExtensions: false, + }); const response = await graphql({ schema, diff --git a/packages/apollo-cache-control/src/index.ts b/packages/apollo-cache-control/src/index.ts index 9debb2d7cd1..56a5552d61c 100644 --- a/packages/apollo-cache-control/src/index.ts +++ b/packages/apollo-cache-control/src/index.ts @@ -119,7 +119,14 @@ export class CacheControlExtension } format(): [string, CacheControlFormat] | undefined { - if (this.options.stripFormattedExtensions) return; + // We should have to explicitly ask leave the formatted extension in, or + // pass the old-school `cacheControl: true` (as interpreted by + // apollo-server-core/ApolloServer), in order to include the + // engineproxy-aimed extensions. Specifically, we want users of + // apollo-server-plugin-full-query-cache to be able to specify + // `cacheControl: {defaultMaxAge: 600}` without accidentally turning on the + // extension formatting. + if (this.options.stripFormattedExtensions !== false) return; return [ 'cacheControl', diff --git a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts index 950fca7a336..c53cb9ea5c7 100644 --- a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts +++ b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts @@ -38,6 +38,9 @@ import { } from 'apollo-server-core'; import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions'; import { TracingFormat } from 'apollo-tracing'; +import ApolloServerPluginFullQueryCache from 'apollo-server-plugin-full-query-cache'; + +import { mockDate, unmockDate, advanceTimeBy } from '__mocks__/date'; export function createServerInfo( server: AS, @@ -1381,5 +1384,78 @@ export function testApolloServer( expect(resolverDuration).not.toBeGreaterThan(tracing.duration); }); }); + + describe('Full Query Caching', () => { + const typeDefs = gql` + type Query { + cachedField: String @cacheControl(maxAge: 10) + } + `; + + let resolverCallCount = 0; + const resolvers = { + Query: { + cachedField: () => { + resolverCallCount++; + return 'value'; + }, + }, + }; + + beforeAll(() => { + mockDate(); + }); + + afterAll(() => { + unmockDate(); + }); + + it('basic caching', async () => { + const { url: uri } = await createApolloServer({ + typeDefs, + resolvers, + plugins: [ApolloServerPluginFullQueryCache()], + }); + + const apolloFetch = createApolloFetch({ uri }); + + const fetch = async () => { + const result = await apolloFetch({ + query: `{ cachedField }`, + }); + expect(result.data.cachedField).toBe('value'); + }; + + await fetch(); + expect(resolverCallCount).toBe(1); + + await fetch(); + expect(resolverCallCount).toBe(1); + + advanceTimeBy(5 * 1000); + + await fetch(); + expect(resolverCallCount).toBe(1); + + advanceTimeBy(6 * 1000); + await fetch(); + expect(resolverCallCount).toBe(2); + + await fetch(); + expect(resolverCallCount).toBe(2); + + const textChangedASTUnchangedResult = await apolloFetch({ + query: `{ cachedField }`, + }); + expect(textChangedASTUnchangedResult.data.cachedField).toBe('value'); + expect(resolverCallCount).toBe(2); + + const slightlyDifferentQueryResult = await apolloFetch({ + query: `{alias: cachedField}`, + }); + expect(slightlyDifferentQueryResult.data.alias).toBe('value'); + expect(resolverCallCount).toBe(3); + }); + }); }); }