|
2 | 2 |
|
3 | 3 | A utility for generating a full-featured `fetcher` for GraphiQL including |
4 | 4 | `@stream`, `@defer` `IncrementalDelivery`and `multipart` and subscriptions using |
5 | | -`graphql-ws` or the legacy websockets protocol. |
| 5 | +GraphQL over SSE, `graphql-ws` or the legacy websockets protocol. |
6 | 6 |
|
7 | | -Under the hood, it uses [`graphql-ws`](https://www.npmjs.com/package/graphql-ws) |
8 | | -client and [`meros`](https://www.npmjs.com/package/meros) which act as client |
| 7 | +Under the hood, it uses [`graphql-sse`](https://www.npmjs.com/package/graphql-sse), |
| 8 | +[`graphql-ws`](https://www.npmjs.com/package/graphql-ws) and |
| 9 | +[`meros`](https://www.npmjs.com/package/meros) which act as client |
9 | 10 | reference implementations of the |
10 | 11 | [GraphQL over HTTP Working Group Spec](https://github.com/graphql/graphql-over-http) |
11 | 12 | specification, and the most popular transport spec proposals. |
@@ -45,6 +46,54 @@ const root = createRoot(document.getElementById('graphiql')); |
45 | 46 | root.render(<App />); |
46 | 47 | ``` |
47 | 48 |
|
| 49 | +### Adding GraphQL over SSE subscriptions |
| 50 | + |
| 51 | +First you'll need to install `graphql-sse` as a peer dependency: |
| 52 | + |
| 53 | +```bash |
| 54 | +npm install graphql-sse |
| 55 | +``` |
| 56 | + |
| 57 | +When loading GraphiQL from an ESM CDN with an import map, make sure the optional |
| 58 | +`graphql-sse` peer dependency is also mapped if you use `sseUrl`: |
| 59 | + |
| 60 | +```html |
| 61 | +<script type="importmap"> |
| 62 | + { |
| 63 | + "imports": { |
| 64 | + "@graphiql/toolkit": "https://esm.sh/@graphiql/toolkit", |
| 65 | + "graphql": "https://esm.sh/graphql", |
| 66 | + "graphql-sse": "https://esm.sh/graphql-sse?external=graphql" |
| 67 | + } |
| 68 | + } |
| 69 | +</script> |
| 70 | +``` |
| 71 | + |
| 72 | +Just by providing the `sseUrl`, you can generate a `graphql-sse` client. This |
| 73 | +client supports HTTP/Multipart Incremental Delivery for `@defer` and `@stream`, |
| 74 | +_and_ subscriptions over Server-Sent Events. |
| 75 | + |
| 76 | +```jsx |
| 77 | +import * as React from 'react'; |
| 78 | +import { createRoot } from 'react-dom/client'; |
| 79 | +import { GraphiQL } from 'graphiql'; |
| 80 | +import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
| 81 | + |
| 82 | +const url = 'https://my-schema.com/graphql'; |
| 83 | + |
| 84 | +const sseUrl = 'https://my-schema.com/graphql/stream'; |
| 85 | + |
| 86 | +const fetcher = createGraphiQLFetcher({ url, sseUrl }); |
| 87 | + |
| 88 | +export const App = () => <GraphiQL fetcher={fetcher} />; |
| 89 | + |
| 90 | +const root = createRoot(document.getElementById('graphiql')); |
| 91 | +root.render(<App />); |
| 92 | +``` |
| 93 | + |
| 94 | +You can further customize the `graphql-sse` implementation by creating a custom |
| 95 | +client instance and providing it as the `sseClient` parameter. |
| 96 | + |
48 | 97 | ### Adding `graphql-ws` websockets subscriptions |
49 | 98 |
|
50 | 99 | First you'll need to install `graphql-ws` as a peer dependency: |
@@ -90,6 +139,24 @@ This generates a `graphql-ws` client using the provided url. Note that a server |
90 | 139 | must be compatible with the new `graphql-ws` subscriptions spec for this to |
91 | 140 | work. |
92 | 141 |
|
| 142 | +### `sseUrl` |
| 143 | + |
| 144 | +This generates a `graphql-sse` client using the provided url. Note that a server |
| 145 | +must be compatible with the GraphQL over SSE protocol for this to work. When |
| 146 | +`sseUrl` or `sseClient` is provided, GraphiQL uses SSE for subscriptions instead |
| 147 | +of websockets. |
| 148 | + |
| 149 | +### `sseClient` |
| 150 | + |
| 151 | +Provide your own GraphQL over SSE subscriptions client. Using this option |
| 152 | +bypasses `sseUrl`. In theory, this could be any client using any transport, as |
| 153 | +long as it matches the `graphql-sse` client signature. |
| 154 | + |
| 155 | +### `sseClientOptions` |
| 156 | + |
| 157 | +Provide additional options used when creating a `graphql-sse` client from |
| 158 | +`sseUrl`, for example `singleConnection`. |
| 159 | + |
93 | 160 | ### `wsClient` |
94 | 161 |
|
95 | 162 | Provide your own subscriptions client. Using this option bypasses |
@@ -129,6 +196,35 @@ Pass a custom fetch implementation such as `isomorphic-fetch`. |
129 | 196 |
|
130 | 197 | ## Customization Examples |
131 | 198 |
|
| 199 | +### Custom `sseClient` Example using `graphql-sse` |
| 200 | + |
| 201 | +This example passes a `graphql-sse` client to the `sseClient` option: |
| 202 | + |
| 203 | +```jsx |
| 204 | +import * as React from 'react'; |
| 205 | +import { createRoot } from 'react-dom/client'; |
| 206 | +import { GraphiQL } from 'graphiql'; |
| 207 | +import { createClient } from 'graphql-sse'; |
| 208 | +import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
| 209 | + |
| 210 | +const url = 'https://my-schema.com/graphql'; |
| 211 | + |
| 212 | +const sseUrl = 'https://my-schema.com/graphql/stream'; |
| 213 | + |
| 214 | +const fetcher = createGraphiQLFetcher({ |
| 215 | + url, |
| 216 | + sseClient: createClient({ |
| 217 | + url: sseUrl, |
| 218 | + singleConnection: true, |
| 219 | + }), |
| 220 | +}); |
| 221 | + |
| 222 | +export const App = () => <GraphiQL fetcher={fetcher} />; |
| 223 | + |
| 224 | +const root = createRoot(document.getElementById('graphiql')); |
| 225 | +root.render(<App />); |
| 226 | +``` |
| 227 | + |
132 | 228 | ### Custom `wsClient` Example using `graphql-ws` |
133 | 229 |
|
134 | 230 | This example passes a `graphql-ws` client to the `wsClient` option: |
|
0 commit comments