1 | ## Create Fetcher
|
2 |
|
3 | a utility for generating a full-featured `fetcher` for GraphiQL including `@stream`, `@defer` `IncrementalDelivery`and `multipart` and subscriptions using `graphql-ws` or the legacy websockets protocol
|
4 |
|
5 | under the hood, it uses [`graphql-ws`](https://www.npmjs.com/package/graphql-ws) client and [`meros`](https://www.npmjs.com/package/meros) which act as client reference implementations of the [GraphQL over HTTP Working Group Spec](https://github.com/graphql/graphql-over-http) specification, and the most popular transport spec proposals
|
6 |
|
7 | ### Setup
|
8 |
|
9 | [`graphiql`](https://npmjs.com/package/graphiql) and thus `react` and `react-dom` should already be installed.
|
10 |
|
11 | you'll need to install `@graphiql/toolkit`
|
12 |
|
13 | ```bash
|
14 | npm install --save @graphiql/toolkit
|
15 | ```
|
16 |
|
17 | ```bash
|
18 | yarn add @graphiql/toolkit
|
19 | ```
|
20 |
|
21 | ### Getting Started
|
22 |
|
23 | We have a few flexible options to get you started with the client. It's meant to cover the majority of common use cases with a simple encapsulation.
|
24 |
|
25 | #### Default HTTP/Multipart IncrementalDelivery Usage
|
26 |
|
27 | Here's a simple example. In this case, a websocket client isn't even initialized, only http (with multipart `@stream` and `@defer` Incremental Delivery support of course!).
|
28 |
|
29 | ```ts
|
30 | import * as React from 'react';
|
31 | import ReactDOM from 'react-dom';
|
32 | import { GraphiQL } from 'graphiql';
|
33 | import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
34 |
|
35 | const url = 'https://myschema.com/graphql';
|
36 |
|
37 | const fetcher = createGraphiQLFetcher({ url });
|
38 |
|
39 | export const App = () => <GraphiQL fetcher={fetcher} />;
|
40 |
|
41 | ReactDOM.render(document.getElementByID('graphiql'), <App />);
|
42 | ```
|
43 |
|
44 | #### Adding `graphql-ws` websockets subscriptions
|
45 |
|
46 | first you'll need to install `graphql-ws` as a peer dependency:
|
47 |
|
48 | ```bash
|
49 | npm install --save graphql-ws
|
50 | ```
|
51 |
|
52 | ```bash
|
53 | yarn add graphql-ws
|
54 | ```
|
55 |
|
56 | Just by providing the `subscriptionUrl`, you can also generate a `graphql-ws` client. This client now supports both HTTP/Multipart Incremental Delivery for `@defer` and `@stream`, _and_ websockets subscriptions
|
57 |
|
58 | ```ts
|
59 | import * as React from 'react';
|
60 | import ReactDOM from 'react-dom';
|
61 | import { GraphiQL } from 'graphiql';
|
62 | import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
63 |
|
64 | const url = 'https://myschema.com/graphql';
|
65 |
|
66 | const subscriptionUrl = 'wss://myschema.com/graphql';
|
67 |
|
68 | const fetcher = createGraphiQLFetcher({
|
69 | url,
|
70 | subscriptionUrl,
|
71 | });
|
72 |
|
73 | export const App = () => <GraphiQL fetcher={fetcher} />;
|
74 |
|
75 | ReactDOM.render(document.getElementByID('graphiql'), <App />);
|
76 | ```
|
77 |
|
78 | You can further customize the `graphql-ws` implementation by creating a custom client instance and providing it as the `wsClient` parameter
|
79 |
|
80 | ### Options
|
81 |
|
82 | #### `url` (_required_)
|
83 |
|
84 | This is url used for all `HTTP` requests, and for schema introspection.
|
85 |
|
86 | #### `subscriptionUrl`
|
87 |
|
88 | This generates a `graphql-ws` client using the provided url. Note that a server must be compatible with the new `graphql-ws` subscriptions spec for this to work.
|
89 |
|
90 | #### `wsClient`
|
91 |
|
92 | provide your own subscriptions client. bypasses `subscriptionUrl`. In theory, this could be any client using any transport, as long as it matches `graphql-ws` `Client` signature.
|
93 |
|
94 | #### `legacyWsClient` or `legacyClient`
|
95 |
|
96 | provide a legacy subscriptions client using `subscriptions-transport-ws` protocol. bypasses `subscriptionUrl`. In theory, this could be any client using any transport, as long as it matches `subscriptions-transport-ws` `Client` signature.
|
97 |
|
98 | #### `headers`
|
99 |
|
100 | Pass headers to any and all requests
|
101 |
|
102 | #### `fetch`
|
103 |
|
104 | Pass a custom fetch implementation such as `isomorphic-feth`
|
105 |
|
106 | ### Customization Examples
|
107 |
|
108 | #### Custom `wsClient` Example using `graphql-ws`
|
109 |
|
110 | Just by providing the `wsClient`
|
111 |
|
112 | ```ts
|
113 | import * as React from 'react';
|
114 | import ReactDOM from 'react-dom';
|
115 | import { GraphiQL } from 'graphiql';
|
116 | import { createClient } from 'graphql-ws';
|
117 | import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
118 |
|
119 | const url = 'https://myschema.com/graphql';
|
120 |
|
121 | const subscriptionUrl = 'wss://myschema.com/graphql';
|
122 |
|
123 | const fetcher = createGraphiQLFetcher({
|
124 | url,
|
125 | wsClient: createClient({
|
126 | url: subscriptionUrl,
|
127 | keepAlive: 2000,
|
128 | }),
|
129 | });
|
130 |
|
131 | export const App = () => <GraphiQL fetcher={fetcher} />;
|
132 |
|
133 | ReactDOM.render(document.getElementByID('graphiql'), <App />);
|
134 | ```
|
135 |
|
136 | #### Custom `legacyClient` Example
|
137 |
|
138 | (not reccomended)
|
139 |
|
140 | By providing the `legacyClient` you can support a `subscriptions-transport-ws` client implementation, or equivalent
|
141 |
|
142 | ```ts
|
143 | import * as React from 'react';
|
144 | import ReactDOM from 'react-dom';
|
145 | import { GraphiQL } from 'graphiql';
|
146 | import { SubscriptionClient } from 'subscriptions-transport-ws';
|
147 | import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
148 |
|
149 | const url = 'https://myschema.com/graphql';
|
150 |
|
151 | const subscriptionUrl = 'wss://myschema.com/graphql';
|
152 |
|
153 | const fetcher = createGraphiQLFetcher({
|
154 | url,
|
155 | legacyWsClient: new SubscriptionsClient(subscriptionUrl),
|
156 | });
|
157 |
|
158 | export const App = () => <GraphiQL fetcher={fetcher} />;
|
159 |
|
160 | ReactDOM.render(document.getElementByID('graphiql'), <App />);
|
161 | ```
|
162 |
|
163 | you will need to install the client seperately:
|
164 |
|
165 | ```bash
|
166 | yarn add subscriptions-transport-ws
|
167 | ```
|
168 |
|
169 | ```bash
|
170 | npm install --save subscriptions-transport-ws
|
171 | ```
|
172 |
|
173 | and instantiate a client instance following their readme, and pass it as `legacyWsClient`.
|
174 |
|
175 | #### Custom `fetcher` Example
|
176 |
|
177 | For SSR, we might want to use something like `isomorphic-fetch`
|
178 |
|
179 | ```ts
|
180 | import * as React from 'react';
|
181 | import ReactDOM from 'react-dom';
|
182 | import { GraphiQL } from 'graphiql';
|
183 | import { fetch } from 'isomorphic-fetch';
|
184 | import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
185 |
|
186 | const url = 'https://myschema.com/graphql';
|
187 |
|
188 | const fetcher = createGraphiQLFetcher({
|
189 | url,
|
190 | fetch,
|
191 | });
|
192 |
|
193 | export const App = () => <GraphiQL fetcher={fetcher} />;
|
194 |
|
195 | ReactDOM.render(document.getElementByID('graphiql'), <App />);
|
196 | ```
|
197 |
|
198 | ## Credits
|
199 |
|
200 | This is originally inspired by `graphql-subscriptions-fetcher` created by @Urigo
|