-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
25 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
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'); | ||
}); | ||
} | ||
}); |
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 |
---|---|---|
@@ -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); |
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