Skip to content

Commit ece277d

Browse files
authored
fix: throw an error if client.cache isn't provided in ssrMode (#124)
* fix: make cache required in ssr mode * test: test that cache is required in ssrMode
1 parent 8a20545 commit ece277d

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const client = new GraphQLClient(config)
118118

119119
- `url` (**Required**): The url to your GraphQL server
120120
- `ssrMode`: Boolean - set to `true` when using on the server for server-side rendering; defaults to `false`
121-
- `cache`: Object with the following methods:
121+
- `cache` (**Required** if `ssrMode` is `true`, otherwise optional): Object with the following methods:
122122
- `cache.get(key)`
123123
- `cache.set(key, data)`
124124
- `cache.delete(key)`

Diff for: packages/graphql-hooks-ssr/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ app.get('/', async (req, reply) => {
2626
// Step 1: Create the client inside the request handler
2727
const client = new GraphQLClient({
2828
url: 'https://domain.com/graphql',
29-
cache: memCache(),
29+
cache: memCache() // NOTE: a cache is required for SSR,
3030
fetch
3131
})
3232

Diff for: packages/graphql-hooks-ssr/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ const ReactDOMServer = require('react-dom/server')
22

33
async function getInitialState(opts) {
44
const { App, client, render = ReactDOMServer.renderToStaticMarkup } = opts
5+
6+
if (!client.cache) {
7+
throw new Error(
8+
'A cache implementation must be provided for SSR, please pass one to `GraphQLClient` via `options`.'
9+
)
10+
}
11+
512
// ensure ssrMode is set:
613
client.ssrMode = true
714
render(App)

Diff for: packages/graphql-hooks-ssr/index.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,23 @@ describe('getInitialState', () => {
2727
expect(result).toEqual({ foo: 'bar' })
2828
expect(resolvedPromises).toBe(2)
2929
})
30+
31+
it("throws if a cache hasn't been provided", async () => {
32+
const MockApp = () => 'hello world'
33+
34+
const mockClient = {
35+
ssrPromises: []
36+
}
37+
38+
expect(
39+
getInitialState({
40+
App: MockApp,
41+
client: mockClient
42+
})
43+
).rejects.toEqual(
44+
new Error(
45+
'A cache implementation must be provided for SSR, please pass one to `GraphQLClient` via `options`.'
46+
)
47+
)
48+
})
3049
})

Diff for: packages/graphql-hooks/src/GraphQLClient.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class GraphQLClient {
1515
)
1616
}
1717

18+
if (config.ssrMode && !config.cache) {
19+
throw new Error('GraphQLClient: config.cache is required when in ssrMode')
20+
}
21+
1822
this.cache = config.cache
1923
this.headers = config.headers || {}
2024
this.ssrMode = config.ssrMode

Diff for: packages/graphql-hooks/test/unit/GraphQLClient.test.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ describe('GraphQLClient', () => {
3737
global.fetch = oldFetch
3838
})
3939

40+
it('throws if config.ssrMode is true and no config.cache is provided', () => {
41+
expect(() => {
42+
new GraphQLClient({
43+
...validConfig,
44+
ssrMode: true
45+
})
46+
}).toThrow('GraphQLClient: config.cache is required when in ssrMode')
47+
})
48+
4049
it('assigns config.cache to an instance property', () => {
4150
const cache = { get: 'get', set: 'set' }
4251
const client = new GraphQLClient({ ...validConfig, cache })
@@ -49,8 +58,12 @@ describe('GraphQLClient', () => {
4958
expect(client.headers).toBe(headers)
5059
})
5160

52-
it('assigns config.ssrMode to an instance property', () => {
53-
const client = new GraphQLClient({ ...validConfig, ssrMode: true })
61+
it('assigns config.ssrMode to an instance property if config.cache is provided', () => {
62+
const client = new GraphQLClient({
63+
...validConfig,
64+
ssrMode: true,
65+
cache: { get: 'get', set: 'set' }
66+
})
5467
expect(client.ssrMode).toBe(true)
5568
})
5669

0 commit comments

Comments
 (0)