Skip to content

Commit

Permalink
Basic end-to-end test
Browse files Browse the repository at this point in the history
Unit tests of all the pluginn options forthcoming
  • Loading branch information
glasser committed Mar 13, 2019
1 parent 84141bd commit a64b620
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export async function collectCacheControlHints(
): Promise<CacheHint[]> {
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,
Expand Down
9 changes: 8 additions & 1 deletion packages/apollo-cache-control/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ export class CacheControlExtension<TContext = any>
}

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',
Expand Down
76 changes: 76 additions & 0 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AS extends ApolloServerBase>(
server: AS,
Expand Down Expand Up @@ -1381,5 +1384,78 @@ export function testApolloServer<AS extends ApolloServerBase>(
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);
});
});
});
}

0 comments on commit a64b620

Please sign in to comment.