@@ -204,32 +204,28 @@ You can also create your own theme in CSS. As a reference, the default
204204` graphiql ` theme definition can be found
205205[ here] ( ../graphiql-react/src/style/codemirror.css ) .
206206
207- ### Usage with React Router and ` ssr: true `
207+ ## Usage with React Router and ` ssr: true `
208208
209209When using GraphiQL with [ React Router’s SSR mode] ( https://reactrouter.com/api/framework-conventions/react-router.config.ts#ssr ) ,
210210you need to mark the GraphiQL component as a [ client module] ( https://reactrouter.com/api/framework-conventions/client-modules )
211211by adding ` .client ` to the file name.
212212
213213``` tsx
214214// graphiql.client.tsx
215- import type { FC } from ' react' ;
216215import { GraphiQL } from ' graphiql' ;
217216import { createGraphiQLFetcher } from ' @graphiql/toolkit' ;
218217
219218const fetcher = createGraphiQLFetcher ({ url: ' https://my.backend/graphql' });
220219
221- export const Route: FC = () => {
222- return <GraphiQL fetcher = { fetcher } />;
223- };
220+ export const graphiql = <GraphiQL fetcher = { fetcher } />;
224221```
225222
226- ``` tsx
227- // route.tsx
223+ ``` ts
224+ // route.ts
228225import type { FC } from ' react' ;
229- import { useEffect , useState } from ' react' ;
230226import type { LinksFunction , MetaFunction } from ' react-router' ;
231227import graphiqlStyles from ' graphiql/style.css?url' ;
232- import { Route as GraphiQL } from ' ./graphiql.client' ;
228+ import { graphiql } from ' ./graphiql.client' ;
233229
234230export const meta: MetaFunction = () => {
235231 return [{ title: ' API Explorer' }];
@@ -240,14 +236,42 @@ export const links: LinksFunction = () => {
240236};
241237
242238const Route: FC = () => {
243- const [mounted, setMounted] = useState (false );
239+ return graphiql ;
240+ };
244241
245- useEffect (() => {
246- setMounted (true );
247- }, []);
242+ export default Route ;
243+ ```
244+
245+ ## Usage with a Custom Storage Namespace
248246
249- return mounted ? <GraphiQL /> : ' Loading...' ;
247+ When multiple GraphiQL instances run on the same origin—such as in different apps or
248+ environments—they can conflict by reading from and writing to the same ` localStorage ` keys. To
249+ prevent this, you can provide a custom ` storage ` object that prefixes all keys with a unique
250+ namespace, isolating each instance’s state and avoiding collisions.
251+
252+ ``` tsx
253+ import type { FC } from ' react' ;
254+ import { GraphiQL } from ' graphiql' ;
255+ import { createGraphiQLFetcher } from ' @graphiql/toolkit' ;
256+
257+ const fetcher = createGraphiQLFetcher ({ url: ' https://my.backend/graphql' });
258+
259+ const NAMESPACE = ' my-namespace' ;
260+
261+ const storage: typeof localStorage = {
262+ ... localStorage ,
263+ getItem(key ) {
264+ return localStorage .getItem (` ${NAMESPACE }:${key } ` );
265+ },
266+ setItem(key , value ) {
267+ return localStorage .setItem (` ${NAMESPACE }:${key } ` , value );
268+ },
269+ removeItem(key ) {
270+ return localStorage .removeItem (` ${NAMESPACE }:${key } ` );
271+ },
250272};
251273
252- export default Route ;
274+ export const App: FC = () => {
275+ return <GraphiQL fetcher = { fetcher } storage = { myStorage } />;
276+ };
253277```
0 commit comments