Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jan 15, 2025
1 parent a491d4c commit 0a17f00
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 25 deletions.
7 changes: 3 additions & 4 deletions e2e/cache-control/__snapshots__/cache-control.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Cache Control composes with Apollo: apollo 1`] = `
exports[`Cache Control Composition With apollo composed correctly: apollo 1`] = `
"schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(url: "https://specs.apollo.dev/join/v0.3", for: EXECUTION)
Expand Down Expand Up @@ -84,7 +84,7 @@ type Query
}"
`;

exports[`Cache Control composes with Mesh: mesh 1`] = `
exports[`Cache Control Composition With mesh composed correctly: mesh 1`] = `
"
schema
@link(url: "https://specs.apollo.dev/link/v1.0")
Expand Down Expand Up @@ -196,6 +196,5 @@ enum CacheControlScope @join__type(graph: AUTHORS) @join__type(graph: BOOKS) {
PUBLIC @join__enumValue(graph: AUTHORS) @join__enumValue(graph: BOOKS)
PRIVATE @join__enumValue(graph: AUTHORS) @join__enumValue(graph: BOOKS)
}
"
"
`;
80 changes: 65 additions & 15 deletions e2e/cache-control/cache-control.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,75 @@
import { createTenv, type Service } from '@e2e/tenv';
import { authors, books } from './services/data';

describe('Cache Control', () => {
const env = createTenv(__dirname);
let services: Service[];
beforeAll(async () => {
beforeEach(async () => {
const books = await env.service('books');
const authors = await env.service('authors');
services = [books, authors];
services = [authors, books];
});
it('composes with Mesh', async () => {
const { result } = await env.compose({
services,
maskServicePorts: true,
const composition = {
mesh(maskServicePorts: boolean) {
return env.compose({
services,
maskServicePorts,
output: 'graphql',
});
},
apollo(maskServicePorts: boolean) {
return env.composeWithApollo({
services,
maskServicePorts,
});
},
};
for (const [name, compose] of Object.entries(composition)) {
describe(`Composition With ${name}`, () => {
it('composed correctly', async () => {
const { result } = await compose(true);
expect(result).toMatchSnapshot(name);
});
const cacheMethod = ['HTTP_CACHE', 'RESPONSE_CACHE'];
for (const method of cacheMethod) {
it('caches correctly with ' + method, async () => {
const { output } = await compose(false);
const gw = await env.serve({
supergraph: output,
env: {
[method]: 'true',
},
});
async function makeQuery() {
const res = await gw.execute({
query: /* GraphQL */ `
query TestQuery {
authors {
id
name
}
books {
id
title
}
}
`,
});
expect(res).toEqual({
data: {
authors: authors.map(({ id, name }) => ({ id, name })),
books: books.map(({ id, title }) => ({ id, title })),
},
});
}
await makeQuery();
expect(services[0].getStd('both')).toContain('TestQuery: 1');
expect(services[1].getStd('both')).toContain('TestQuery: 1');
await makeQuery();
expect(services[0].getStd('both')).not.toContain('TestQuery: 2');
expect(services[1].getStd('both')).not.toContain('TestQuery: 2');
});
}
});
expect(result).toMatchSnapshot('mesh');
});
it('composes with Apollo', async () => {
const { result } = await env.composeWithApollo({
services,
maskServicePorts: true,
});
expect(result).toMatchSnapshot('apollo');
});
}
});
18 changes: 13 additions & 5 deletions e2e/cache-control/gateway.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { defineConfig, useHttpCache } from '@graphql-hive/gateway';

export const gatewayConfig = defineConfig({
const config: ReturnType<typeof defineConfig> = {
cache: {
type: 'localforage',
},
responseCaching: {
};

if (process.env.HTTP_CACHE) {
config.plugins = ctx => [useHttpCache(ctx)];
}

if (process.env.RESPONSE_CACHE) {
config.responseCaching = {
session: () => null,
},
plugins: ctx => [useHttpCache(ctx)],
});
};
}

export const gatewayConfig = defineConfig(config);
9 changes: 9 additions & 0 deletions e2e/cache-control/services/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ startStandaloneServer(
},
},
}),
plugins: [
{
async requestDidStart({ request }) {
if (request.operationName) {
console.count(request.operationName);
}
},
},
],
}),
{ listen: { port } },
);
11 changes: 10 additions & 1 deletion e2e/cache-control/services/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ startStandaloneServer(
`),
resolvers: {
Query: {
authors: () => books,
books: () => books,
book: (_, { id }) => books.find(book => book.id === id),
},
Book: {
Expand All @@ -63,6 +63,15 @@ startStandaloneServer(
},
},
}),
plugins: [
{
async requestDidStart({ request }) {
if (request.operationName) {
console.count(request.operationName);
}
},
},
],
}),
{ listen: { port } },
);

0 comments on commit 0a17f00

Please sign in to comment.