1 | ---
|
2 | title: apollo-link-schema
|
3 | description: Assists with mocking and server-side rendering
|
4 | ---
|
5 |
|
6 | ## Installation
|
7 |
|
8 | `npm install apollo-link-schema --save`
|
9 |
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ### Server Side Rendering
|
14 |
|
15 | When performing SSR _on the same server_ you can use this library to avoid making network calls.
|
16 |
|
17 | ```js
|
18 | import { ApolloClient } from "apollo-client";
|
19 | import { InMemoryCache } from "apollo-cache-inmemory";
|
20 | import { SchemaLink } from "apollo-link-schema";
|
21 |
|
22 | import schema from './path/to/your/schema';
|
23 |
|
24 | const graphqlClient = new ApolloClient({
|
25 | ssr: true,
|
26 | cache: new InMemoryCache(),
|
27 | link: new SchemaLink({ schema })
|
28 | });
|
29 | ```
|
30 |
|
31 | ### Mocking
|
32 | ```js
|
33 | import { ApolloClient } from "apollo-client";
|
34 | import { InMemoryCache } from "apollo-cache-inmemory";
|
35 | import { SchemaLink } from "apollo-link-schema";
|
36 | import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
|
37 |
|
38 | const typeDefs = `
|
39 | Query {
|
40 | ...
|
41 | }
|
42 | `;
|
43 |
|
44 | const mocks = {
|
45 | Query: () => ...,
|
46 | Mutation: () => ...
|
47 | };
|
48 |
|
49 | const schema = makeExecutableSchema({ typeDefs });
|
50 | addMockFunctionsToSchema({
|
51 | schema,
|
52 | mocks
|
53 | });
|
54 |
|
55 | const apolloCache = new InMemoryCache(window.__APOLLO_STATE__);
|
56 |
|
57 | const graphqlClient = new ApolloClient({
|
58 | cache: apolloCache,
|
59 | link: new SchemaLink({ schema })
|
60 | });
|
61 | ```
|
62 |
|
63 | ### Options
|
64 |
|
65 | The `SchemaLink` constructor an be called with an object that has the following properties:
|
66 |
|
67 | * `schema`: an executable graphql schema
|
68 | * `rootValue`: the root value that is used (e.g. the user)
|
69 | * `context`: the context that is used (e.g. an object that stores all the data-fetching connectors)
|