UNPKG

1.62 kBMarkdownView Raw
1---
2title: apollo-link-schema
3description: 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
15When performing SSR _on the same server_ you can use this library to avoid making network calls.
16
17```js
18import { ApolloClient } from "apollo-client";
19import { InMemoryCache } from "apollo-cache-inmemory";
20import { SchemaLink } from "apollo-link-schema";
21
22import schema from './path/to/your/schema';
23
24const graphqlClient = new ApolloClient({
25 ssr: true,
26 cache: new InMemoryCache(),
27 link: new SchemaLink({ schema })
28});
29```
30
31### Mocking
32```js
33import { ApolloClient } from "apollo-client";
34import { InMemoryCache } from "apollo-cache-inmemory";
35import { SchemaLink } from "apollo-link-schema";
36import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
37
38const typeDefs = `
39 Query {
40 ...
41 }
42`;
43
44const mocks = {
45 Query: () => ...,
46 Mutation: () => ...
47};
48
49const schema = makeExecutableSchema({ typeDefs });
50addMockFunctionsToSchema({
51 schema,
52 mocks
53});
54
55const apolloCache = new InMemoryCache(window.__APOLLO_STATE__);
56
57const graphqlClient = new ApolloClient({
58 cache: apolloCache,
59 link: new SchemaLink({ schema })
60});
61```
62
63### Options
64
65The `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)