packages/graphiql-toolkit/docs/create-fetcher.md
createGraphiQLFetcherA utility for generating a full-featured fetcher for GraphiQL including
@stream, @defer IncrementalDeliveryand multipart and subscriptions using
graphql-ws or the legacy websockets protocol.
Under the hood, it uses graphql-ws
client and meros which act as client
reference implementations of the
GraphQL over HTTP Working Group Spec
specification, and the most popular transport spec proposals.
You can install @graphiql/toolkit using your favorite package manager:
npm install @graphiql/toolkit
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.
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!).
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { GraphiQL } from 'graphiql';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
const url = 'https://my-schema.com/graphql';
const fetcher = createGraphiQLFetcher({ url });
export const App = () => <GraphiQL fetcher={fetcher} />;
const root = createRoot(document.getElementById('graphiql'));
root.render(<App />);
graphql-ws websockets subscriptionsFirst you'll need to install graphql-ws as a peer dependency:
npm install graphql-ws
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.
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { GraphiQL } from 'graphiql';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
const url = 'https://my-schema.com/graphql';
const subscriptionUrl = 'wss://my-schema.com/graphql';
const fetcher = createGraphiQLFetcher({ url, subscriptionUrl });
export const App = () => <GraphiQL fetcher={fetcher} />;
const root = createRoot(document.getElementById('graphiql'));
root.render(<App />);
You can further customize the graphql-ws implementation by creating a custom
client instance and providing it as the wsClient parameter.
url (required)This is url used for all HTTP requests, and for schema introspection.
subscriptionUrlThis 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.
wsClientProvide your own subscriptions client. Using this option bypasses
subscriptionUrl. In theory, this could be any client using any transport, as
long as it matches graphql-ws Client signature.
wsConnectionParamsProvide your initial connection params.
const fetcher = createGraphiQLFetcher({
url: 'https://localhost:3000',
subscriptionUrl: 'https://localhost:3001',
wsConnectionParams: { Authorization: 'token 1234' },
});
const App = () => {
return <GraphiQL fetcher={fetcher} />;
};
legacyWsClient or legacyClientProvide a legacy subscriptions client using subscriptions-transport-ws
protocol. Using this option bypasses subscriptionUrl. In theory, this could be
any client using any transport, as long as it matches
subscriptions-transport-ws Client signature.
headersSpecify headers that will be passed to all requests.
fetchPass a custom fetch implementation such as isomorphic-fetch.
wsClient Example using graphql-wsThis example passes a graphql-ws client to the wsClient option:
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { GraphiQL } from 'graphiql';
import { createClient } from 'graphql-ws';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
const url = 'https://my-schema.com/graphql';
const subscriptionUrl = 'wss://my-schema.com/graphql';
const fetcher = createGraphiQLFetcher({
url,
wsClient: createClient({
url: subscriptionUrl,
keepAlive: 2000,
}),
});
export const App = () => <GraphiQL fetcher={fetcher} />;
const root = createRoot(document.getElementById('graphiql'));
root.render(<App />);
legacyClient Example(not recommended)
By providing the legacyClient you can support a subscriptions-transport-ws
client implementation, or equivalent:
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { GraphiQL } from 'graphiql';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
const url = 'https://my-schema.com/graphql';
const subscriptionUrl = 'wss://my-schema.com/graphql';
const fetcher = createGraphiQLFetcher({
url,
legacyWsClient: new SubscriptionClient(subscriptionUrl),
});
export const App = () => <GraphiQL fetcher={fetcher} />;
const root = createRoot(document.getElementById('graphiql'));
root.render(<App />);
Note that you will need to install the client separately:
npm install subscriptions-transport-ws
fetcher ExampleFor SSR, we might want to use something like isomorphic-fetch:
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { GraphiQL } from 'graphiql';
import { fetch } from 'isomorphic-fetch';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
const url = 'https://my-schema.com/graphql';
const fetcher = createGraphiQLFetcher({ url, fetch });
export const App = () => <GraphiQL fetcher={fetcher} />;
const root = createRoot(document.getElementById('graphiql'));
root.render(<App />);
This is originally inspired by graphql-subscriptions-fetcher created by @Urigo