This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
apolloClient.js
52 lines (47 loc) · 1.89 KB
/
apolloClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* This client is used to prefetch data server side
* (necessary for SSR)
*
* /!\ It must be recreated on every request
*/
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';
import { GraphQLSchema } from '../../modules/graphql.js';
import { createStateLink } from '../../modules/apollo-common';
import { ApolloLink } from 'apollo-link';
// @see https://www.apollographql.com/docs/react/features/server-side-rendering.html#local-queries
// import { createHttpLink } from 'apollo-link-http';
// import fetch from 'node-fetch'
export const createClient = async ({ req, computeContext }) => {
// init
// stateLink will init the client internal state
const cache = new InMemoryCache();
const stateLink = createStateLink({ cache });
// schemaLink will fetch data directly based on the executable schema
const schema = GraphQLSchema.getExecutableSchema();
// this is the resolver context
const context = await computeContext(req);
const schemaLink = new SchemaLink({ schema, context });
const client = new ApolloClient({
ssrMode: true,
link: ApolloLink.from([stateLink, schemaLink]),
// @see https://www.apollographql.com/docs/react/features/server-side-rendering.html#local-queries
// Remember that this is the interface the SSR server will use to connect to the
// API server, so we need to ensure it isn't firewalled, etc
//link: createHttpLink({
// uri: 'http://localhost:3000',
// credentials: 'same-origin',
// headers: {
// // NOTE: this is a Connect req, not an Express req,
// // so req.header is not defined
// // cookie: req.header('Cookie'),
// cookie: req.headers['cookie'],
// },
// // need to explicitely pass fetch server side
// fetch
//}),
cache,
});
return client;
};