Skip to content

Commit 6d631e2

Browse files
authored
fix headers are not set in the refetch of introspection query (#3963)
* upd * upd * upd * upd * upd * upd * fix
1 parent 71755b7 commit 6d631e2

File tree

3 files changed

+51
-65
lines changed

3 files changed

+51
-65
lines changed

.changeset/gentle-weeks-allow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphiql/react': patch
3+
'graphiql': patch
4+
---
5+
6+
fix headers are not set in the refetch of introspection query

packages/graphiql-plugin-doc-explorer/src/components/__tests__/doc-explorer.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ function makeSchema(fieldName = 'field') {
3030

3131
const defaultSchemaContext = {
3232
...schemaStore.getInitialState(),
33-
introspect() {},
33+
async introspect() {},
3434
schema: makeSchema(),
3535
};
3636

3737
const withErrorSchemaContext = {
3838
...schemaStore.getInitialState(),
3939
fetchError: 'Error fetching schema',
40-
introspect() {},
40+
async introspect() {},
4141
schema: new GraphQLSchema({ description: 'GraphQL Schema for testing' }),
4242
};
4343

packages/graphiql-react/src/schema.ts

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from 'graphql';
1818
import { Dispatch, FC, ReactElement, ReactNode, useEffect } from 'react';
1919
import { createStore } from 'zustand';
20-
import { useEditorStore } from './editor';
20+
import { editorStore } from './editor/context';
2121
import type { SchemaReference } from 'codemirror-graphql/utils/SchemaReference';
2222
import { createBoundedUseStore } from './utility';
2323

@@ -52,17 +52,16 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
5252
set({ schemaReference });
5353
},
5454
requestCounter: 0,
55-
currentHeaders: '',
55+
shouldIntrospect: true,
5656
/**
5757
* Fetch the schema
5858
*/
59-
introspect() {
59+
async introspect() {
6060
const {
61-
schema,
6261
requestCounter,
6362
fetcher,
64-
currentHeaders,
6563
onSchemaChange,
64+
shouldIntrospect,
6665
...rest
6766
} = get();
6867

@@ -71,20 +70,15 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
7170
* prop is passed an introspection result, we do continue but skip the
7271
* introspection request.
7372
*/
74-
if (isSchema(schema) || schema === null) {
73+
if (!shouldIntrospect) {
7574
return;
7675
}
7776
const counter = requestCounter + 1;
7877
set({ requestCounter: counter });
7978

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();
8882
const parsedHeaders = parseHeaderString(currentHeaders);
8983
if (!parsedHeaders.isValidJSON) {
9084
set({ fetchError: 'Introspection failed as headers are invalid.' });
@@ -148,48 +142,38 @@ export const schemaStore = createStore<SchemaStore>((set, get) => ({
148142
}
149143

150144
set({ isFetching: false });
151-
145+
let introspectionData: IntrospectionQuery | undefined;
152146
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 });
154153
}
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,
192175
});
176+
}
193177
},
194178
}));
195179

@@ -207,7 +191,7 @@ export type SchemaContextType = {
207191
* it will be validated, unless this is explicitly skipped using the
208192
* `dangerouslyAssumeSchemaIsValid` prop.
209193
*/
210-
introspect(): void;
194+
introspect(): Promise<void>;
211195
/**
212196
* If there currently is an introspection request in-flight.
213197
*/
@@ -235,10 +219,9 @@ export type SchemaContextType = {
235219
*/
236220
requestCounter: number;
237221
/**
238-
* Keep a ref to the current headers.
239-
* @default ''
222+
* `false` when `schema` is provided via `props` as `GraphQLSchema | null`
240223
*/
241-
currentHeaders: string;
224+
shouldIntrospect: boolean;
242225
};
243226

244227
type SchemaContextProviderProps = {
@@ -307,8 +290,6 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
307290
'The `SchemaContextProvider` component requires a `fetcher` function to be passed as prop.',
308291
);
309292
}
310-
const headerEditor = useEditorStore(store => store.headerEditor);
311-
312293
/**
313294
* Synchronize prop changes with state
314295
*/
@@ -324,6 +305,7 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
324305
fetcher,
325306
onSchemaChange,
326307
schema: newSchema,
308+
shouldIntrospect: !isSchema(schema) && schema !== null,
327309
inputValueDeprecation,
328310
introspectionQueryName,
329311
schemaDescription,
@@ -333,11 +315,10 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
333315
* override this change.
334316
*/
335317
requestCounter: requestCounter + 1,
336-
currentHeaders: headerEditor?.getValue(),
337318
}));
338319

339320
// Trigger introspection
340-
schemaStore.getState().introspect();
321+
void schemaStore.getState().introspect();
341322
}, [
342323
schema,
343324
dangerouslyAssumeSchemaIsValid,
@@ -346,7 +327,6 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
346327
inputValueDeprecation,
347328
introspectionQueryName,
348329
schemaDescription,
349-
headerEditor,
350330
]);
351331

352332
/**
@@ -355,7 +335,7 @@ export const SchemaContextProvider: FC<SchemaContextProviderProps> = ({
355335
useEffect(() => {
356336
function triggerIntrospection(event: KeyboardEvent) {
357337
if (event.ctrlKey && event.key === 'R') {
358-
schemaStore.getState().introspect();
338+
void schemaStore.getState().introspect();
359339
}
360340
}
361341

0 commit comments

Comments
 (0)