Skip to content

Commit

Permalink
fix: fix fetchError persisting across different fetchers (#2653)
Browse files Browse the repository at this point in the history
* fix: fix fetchError persisting across different fetchers

* Update packages/graphiql-react/src/schema.tsx

Co-authored-by: Thomas Heyenbrock <[email protected]>

* review comments and added a changeset

Co-authored-by: Thomas Heyenbrock <[email protected]>
  • Loading branch information
dylanowen and thomasheyenbrock authored Sep 9, 2022
1 parent ad9488f commit 39b4668
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-flies-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphiql': patch
'@graphiql/react': patch
---

Fix `fetchError` not being cleared when a new `fetcher` is used
1 change: 1 addition & 0 deletions packages/graphiql-react/src/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
}

setIsFetching(true);
setFetchError(null);

let result = await fetch;

Expand Down
25 changes: 25 additions & 0 deletions packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ describe('GraphiQL', () => {
expect(secondCalled).toEqual(true);
});

it('should refresh schema with new fetcher after a fetchError', async () => {
function firstFetcher() {
return Promise.reject('Schema Error');
}
function secondFetcher() {
return Promise.resolve(simpleIntrospection);
}

// Use a bad fetcher for our initial render
const { rerender, container } = render(<GraphiQL fetcher={firstFetcher} />);
await wait();

expect(
container.querySelector('.doc-explorer-contents .error-container'),
).toBeTruthy();

// Re-render with valid fetcher
rerender(<GraphiQL fetcher={secondFetcher} />);
await wait();

expect(
container.querySelector('.doc-explorer-contents .error-container'),
).not.toBeTruthy();
});

it('should not throw error if schema missing and query provided', () => {
expect(() =>
render(<GraphiQL fetcher={noOpFetcher} query="{}" />),
Expand Down

0 comments on commit 39b4668

Please sign in to comment.