@@ -3,33 +3,53 @@ import * as React from 'react';
3
3
import { createRoot } from 'react-dom/client' ;
4
4
import { GraphiQL } from 'graphiql' ;
5
5
import { explorerPlugin } from '@graphiql/plugin-explorer' ;
6
- import { snippets } from './snippets' ;
6
+ import { getSnippets } from './snippets' ;
7
7
import { codeExporterPlugin } from '@graphiql/plugin-code-exporter' ;
8
8
import 'graphiql/graphiql.css' ;
9
9
import '@graphiql/plugin-explorer/dist/style.css' ;
10
10
import '@graphiql/plugin-code-exporter/dist/style.css' ;
11
+ import { createGraphiQLFetcher } from '@graphiql/toolkit' ;
12
+ import { useStorageContext } from '@graphiql/react' ;
13
+
14
+ export const STARTING_URL =
15
+ 'https://swapi-graphql.netlify.app/.netlify/functions/index' ;
16
+
17
+ import './index.css' ;
18
+ import { serverSelectPlugin , LAST_URL_KEY } from './select-server-plugin' ;
19
+
20
+ if ( 'serviceWorker' in navigator ) {
21
+ window . addEventListener ( 'load' , ( ) => {
22
+ navigator . serviceWorker
23
+ . register ( '/service-worker.js' )
24
+ . then ( registration => {
25
+ console . log ( 'SW registered: ' , registration ) ;
26
+ } )
27
+ . catch ( registrationError => {
28
+ console . log ( 'SW registration failed: ' , registrationError ) ;
29
+ } ) ;
30
+ } ) ;
31
+ }
11
32
12
33
/**
13
- * A manual fetcher implementation, you should probably
14
- * just use `createGraphiQLFetcher` from `@graphiql/toolkit
34
+ * A manual fetcher implementation example
15
35
* @returns
16
36
*/
17
- const fetcher = async ( graphQLParams , options ) => {
18
- const data = await fetch (
19
- 'https://swapi-graphql.netlify.app/.netlify/functions/index' ,
20
- {
21
- method : 'POST' ,
22
- headers : {
23
- Accept : 'application/json' ,
24
- 'Content-Type' : 'application/json' ,
25
- ...options . headers ,
26
- } ,
27
- body : JSON . stringify ( graphQLParams ) ,
28
- credentials : 'same-origin' ,
29
- } ,
30
- ) ;
31
- return data . json ( ) . catch ( ( ) => data . text ( ) ) ;
32
- } ;
37
+ // const fetcher = async (graphQLParams, options) => {
38
+ // const data = await fetch(
39
+ // STARTING_URL ,
40
+ // {
41
+ // method: 'POST',
42
+ // headers: {
43
+ // Accept: 'application/json',
44
+ // 'Content-Type': 'application/json',
45
+ // ...options.headers,
46
+ // },
47
+ // body: JSON.stringify(graphQLParams),
48
+ // credentials: 'same-origin',
49
+ // },
50
+ // );
51
+ // return data.json().catch(() => data.text());
52
+ // };
33
53
34
54
const style = { height : '100vh' } ;
35
55
/**
@@ -38,15 +58,36 @@ const style = { height: '100vh' };
38
58
* then use the `useMemo` hook
39
59
*/
40
60
const explorer = explorerPlugin ( ) ;
41
- const exporter = codeExporterPlugin ( { snippets } ) ;
42
61
43
62
const App = ( ) => {
63
+ const storage = useStorageContext ( ) ;
64
+
65
+ const lastUrl = storage ?. get ( LAST_URL_KEY ) ;
66
+ const [ currentUrl , setUrl ] = React . useState ( lastUrl ?? STARTING_URL ) ;
67
+ // TODO: a breaking change where we make URL an internal state concern, and then expose hooks
68
+ // so that you can handle/set URL state internally from a plugin
69
+ // fetcher could then pass a dynamic URL config object to the fetcher internally
70
+ const exporter = React . useMemo (
71
+ ( ) =>
72
+ codeExporterPlugin ( { snippets : getSnippets ( { serverUrl : currentUrl } ) } ) ,
73
+ [ currentUrl ] ,
74
+ ) ;
75
+ const fetcher = React . useMemo (
76
+ ( ) => createGraphiQLFetcher ( { url : currentUrl } ) ,
77
+ [ currentUrl ] ,
78
+ ) ;
79
+ const serverSelect = React . useMemo (
80
+ ( ) => serverSelectPlugin ( { url : currentUrl , setUrl } ) ,
81
+ [ currentUrl ] ,
82
+ ) ;
83
+
44
84
return (
45
85
< GraphiQL
46
86
style = { style }
47
87
// eslint-disable-next-line @arthurgeron/react-usememo/require-usememo
48
- plugins = { [ explorer , exporter ] }
88
+ plugins = { [ serverSelect , explorer , exporter ] }
49
89
fetcher = { fetcher }
90
+ shouldPersistHeaders
50
91
/>
51
92
) ;
52
93
} ;
0 commit comments