UNPKG

5.83 kBMarkdownView Raw
1## Create Fetcher
2
3a 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
5under 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
11you'll need to install `@graphiql/toolkit`
12
13```bash
14npm install --save @graphiql/toolkit
15```
16
17```bash
18yarn add @graphiql/toolkit
19```
20
21### Getting Started
22
23We 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
27Here'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
30import * as React from 'react';
31import ReactDOM from 'react-dom';
32import { GraphiQL } from 'graphiql';
33import { createGraphiQLFetcher } from '@graphiql/toolkit';
34
35const url = 'https://myschema.com/graphql';
36
37const fetcher = createGraphiQLFetcher({ url });
38
39export const App = () => <GraphiQL fetcher={fetcher} />;
40
41ReactDOM.render(document.getElementByID('graphiql'), <App />);
42```
43
44#### Adding `graphql-ws` websockets subscriptions
45
46first you'll need to install `graphql-ws` as a peer dependency:
47
48```bash
49npm install --save graphql-ws
50```
51
52```bash
53yarn add graphql-ws
54```
55
56Just 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
59import * as React from 'react';
60import ReactDOM from 'react-dom';
61import { GraphiQL } from 'graphiql';
62import { createGraphiQLFetcher } from '@graphiql/toolkit';
63
64const url = 'https://myschema.com/graphql';
65
66const subscriptionUrl = 'wss://myschema.com/graphql';
67
68const fetcher = createGraphiQLFetcher({
69 url,
70 subscriptionUrl,
71});
72
73export const App = () => <GraphiQL fetcher={fetcher} />;
74
75ReactDOM.render(document.getElementByID('graphiql'), <App />);
76```
77
78You 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
84This is url used for all `HTTP` requests, and for schema introspection.
85
86#### `subscriptionUrl`
87
88This 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
92provide 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
96provide 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
100Pass headers to any and all requests
101
102#### `fetch`
103
104Pass a custom fetch implementation such as `isomorphic-feth`
105
106### Customization Examples
107
108#### Custom `wsClient` Example using `graphql-ws`
109
110Just by providing the `wsClient`
111
112```ts
113import * as React from 'react';
114import ReactDOM from 'react-dom';
115import { GraphiQL } from 'graphiql';
116import { createClient } from 'graphql-ws';
117import { createGraphiQLFetcher } from '@graphiql/toolkit';
118
119const url = 'https://myschema.com/graphql';
120
121const subscriptionUrl = 'wss://myschema.com/graphql';
122
123const fetcher = createGraphiQLFetcher({
124 url,
125 wsClient: createClient({
126 url: subscriptionUrl,
127 keepAlive: 2000,
128 }),
129});
130
131export const App = () => <GraphiQL fetcher={fetcher} />;
132
133ReactDOM.render(document.getElementByID('graphiql'), <App />);
134```
135
136#### Custom `legacyClient` Example
137
138(not reccomended)
139
140By providing the `legacyClient` you can support a `subscriptions-transport-ws` client implementation, or equivalent
141
142```ts
143import * as React from 'react';
144import ReactDOM from 'react-dom';
145import { GraphiQL } from 'graphiql';
146import { SubscriptionClient } from 'subscriptions-transport-ws';
147import { createGraphiQLFetcher } from '@graphiql/toolkit';
148
149const url = 'https://myschema.com/graphql';
150
151const subscriptionUrl = 'wss://myschema.com/graphql';
152
153const fetcher = createGraphiQLFetcher({
154 url,
155 legacyWsClient: new SubscriptionsClient(subscriptionUrl),
156});
157
158export const App = () => <GraphiQL fetcher={fetcher} />;
159
160ReactDOM.render(document.getElementByID('graphiql'), <App />);
161```
162
163you will need to install the client seperately:
164
165```bash
166yarn add subscriptions-transport-ws
167```
168
169```bash
170npm install --save subscriptions-transport-ws
171```
172
173and instantiate a client instance following their readme, and pass it as `legacyWsClient`.
174
175#### Custom `fetcher` Example
176
177For SSR, we might want to use something like `isomorphic-fetch`
178
179```ts
180import * as React from 'react';
181import ReactDOM from 'react-dom';
182import { GraphiQL } from 'graphiql';
183import { fetch } from 'isomorphic-fetch';
184import { createGraphiQLFetcher } from '@graphiql/toolkit';
185
186const url = 'https://myschema.com/graphql';
187
188const fetcher = createGraphiQLFetcher({
189 url,
190 fetch,
191});
192
193export const App = () => <GraphiQL fetcher={fetcher} />;
194
195ReactDOM.render(document.getElementByID('graphiql'), <App />);
196```
197
198## Credits
199
200This is originally inspired by `graphql-subscriptions-fetcher` created by @Urigo