@@ -17,7 +17,7 @@ import {
17
17
} from 'graphql' ;
18
18
import { Dispatch , FC , ReactElement , ReactNode , useEffect } from 'react' ;
19
19
import { createStore } from 'zustand' ;
20
- import { useEditorStore } from './editor' ;
20
+ import { editorStore } from './editor/context ' ;
21
21
import type { SchemaReference } from 'codemirror-graphql/utils/SchemaReference' ;
22
22
import { createBoundedUseStore } from './utility' ;
23
23
@@ -52,17 +52,16 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
52
52
set ( { schemaReference } ) ;
53
53
} ,
54
54
requestCounter : 0 ,
55
- currentHeaders : '' ,
55
+ shouldIntrospect : true ,
56
56
/**
57
57
* Fetch the schema
58
58
*/
59
- introspect ( ) {
59
+ async introspect ( ) {
60
60
const {
61
- schema,
62
61
requestCounter,
63
62
fetcher,
64
- currentHeaders,
65
63
onSchemaChange,
64
+ shouldIntrospect,
66
65
...rest
67
66
} = get ( ) ;
68
67
@@ -71,20 +70,15 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
71
70
* prop is passed an introspection result, we do continue but skip the
72
71
* introspection request.
73
72
*/
74
- if ( isSchema ( schema ) || schema === null ) {
73
+ if ( ! shouldIntrospect ) {
75
74
return ;
76
75
}
77
76
const counter = requestCounter + 1 ;
78
77
set ( { requestCounter : counter } ) ;
79
78
80
- const maybeIntrospectionData = schema ;
81
-
82
- async function fetchIntrospectionData ( ) {
83
- if ( maybeIntrospectionData ) {
84
- // No need to introspect if we already have the data
85
- return maybeIntrospectionData ;
86
- }
87
-
79
+ try {
80
+ const { headerEditor } = editorStore . getState ( ) ;
81
+ const currentHeaders = headerEditor ?. getValue ( ) ;
88
82
const parsedHeaders = parseHeaderString ( currentHeaders ) ;
89
83
if ( ! parsedHeaders . isValidJSON ) {
90
84
set ( { fetchError : 'Introspection failed as headers are invalid.' } ) ;
@@ -148,48 +142,38 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
148
142
}
149
143
150
144
set ( { isFetching : false } ) ;
151
-
145
+ let introspectionData : IntrospectionQuery | undefined ;
152
146
if ( result ?. data && '__schema' in result . data ) {
153
- return result . data as IntrospectionQuery ;
147
+ introspectionData = result . data as IntrospectionQuery ;
148
+ } else {
149
+ // handle as if it were an error if the fetcher response is not a string or response.data is not present
150
+ const responseString =
151
+ typeof result === 'string' ? result : formatResult ( result ) ;
152
+ set ( { fetchError : responseString } ) ;
154
153
}
155
-
156
- // handle as if it were an error if the fetcher response is not a string or response.data is not present
157
- const responseString =
158
- typeof result === 'string' ? result : formatResult ( result ) ;
159
- set ( { fetchError : responseString } ) ;
160
- }
161
-
162
- fetchIntrospectionData ( )
163
- . then ( introspectionData => {
164
- /**
165
- * Don't continue if another introspection request has been started in
166
- * the meantime or if there is no introspection data.
167
- */
168
- if ( counter !== get ( ) . requestCounter || ! introspectionData ) {
169
- return ;
170
- }
171
-
172
- try {
173
- const newSchema = buildClientSchema ( introspectionData ) ;
174
- set ( { schema : newSchema } ) ;
175
- onSchemaChange ?.( newSchema ) ;
176
- } catch ( error ) {
177
- set ( { fetchError : formatError ( error ) } ) ;
178
- }
179
- } )
180
- . catch ( error => {
181
- /**
182
- * Don't continue if another introspection request has been started in
183
- * the meantime.
184
- */
185
- if ( counter !== get ( ) . requestCounter ) {
186
- return ;
187
- }
188
- set ( {
189
- fetchError : formatError ( error ) ,
190
- isFetching : false ,
191
- } ) ;
154
+ /**
155
+ * Don't continue if another introspection request has been started in
156
+ * the meantime or if there is no introspection data.
157
+ */
158
+ if ( counter !== get ( ) . requestCounter || ! introspectionData ) {
159
+ return ;
160
+ }
161
+ const newSchema = buildClientSchema ( introspectionData ) ;
162
+ set ( { schema : newSchema } ) ;
163
+ onSchemaChange ?.( newSchema ) ;
164
+ } catch ( error ) {
165
+ /**
166
+ * Don't continue if another introspection request has been started in
167
+ * the meantime.
168
+ */
169
+ if ( counter !== get ( ) . requestCounter ) {
170
+ return ;
171
+ }
172
+ set ( {
173
+ fetchError : formatError ( error ) ,
174
+ isFetching : false ,
192
175
} ) ;
176
+ }
193
177
} ,
194
178
} ) ) ;
195
179
@@ -207,7 +191,7 @@ export type SchemaContextType = {
207
191
* it will be validated, unless this is explicitly skipped using the
208
192
* `dangerouslyAssumeSchemaIsValid` prop.
209
193
*/
210
- introspect ( ) : void ;
194
+ introspect ( ) : Promise < void > ;
211
195
/**
212
196
* If there currently is an introspection request in-flight.
213
197
*/
@@ -235,10 +219,9 @@ export type SchemaContextType = {
235
219
*/
236
220
requestCounter : number ;
237
221
/**
238
- * Keep a ref to the current headers.
239
- * @default ''
222
+ * `false` when `schema` is provided via `props` as `GraphQLSchema | null`
240
223
*/
241
- currentHeaders : string ;
224
+ shouldIntrospect : boolean ;
242
225
} ;
243
226
244
227
type SchemaContextProviderProps = {
@@ -307,8 +290,6 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
307
290
'The `SchemaContextProvider` component requires a `fetcher` function to be passed as prop.' ,
308
291
) ;
309
292
}
310
- const headerEditor = useEditorStore ( store => store . headerEditor ) ;
311
-
312
293
/**
313
294
* Synchronize prop changes with state
314
295
*/
@@ -324,6 +305,7 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
324
305
fetcher,
325
306
onSchemaChange,
326
307
schema : newSchema ,
308
+ shouldIntrospect : ! isSchema ( schema ) && schema !== null ,
327
309
inputValueDeprecation,
328
310
introspectionQueryName,
329
311
schemaDescription,
@@ -333,11 +315,10 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
333
315
* override this change.
334
316
*/
335
317
requestCounter : requestCounter + 1 ,
336
- currentHeaders : headerEditor ?. getValue ( ) ,
337
318
} ) ) ;
338
319
339
320
// Trigger introspection
340
- schemaStore . getState ( ) . introspect ( ) ;
321
+ void schemaStore . getState ( ) . introspect ( ) ;
341
322
} , [
342
323
schema ,
343
324
dangerouslyAssumeSchemaIsValid ,
@@ -346,7 +327,6 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
346
327
inputValueDeprecation ,
347
328
introspectionQueryName ,
348
329
schemaDescription ,
349
- headerEditor ,
350
330
] ) ;
351
331
352
332
/**
@@ -355,7 +335,7 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
355
335
useEffect ( ( ) => {
356
336
function triggerIntrospection ( event : KeyboardEvent ) {
357
337
if ( event . ctrlKey && event . key === 'R' ) {
358
- schemaStore . getState ( ) . introspect ( ) ;
338
+ void schemaStore . getState ( ) . introspect ( ) ;
359
339
}
360
340
}
361
341
0 commit comments