Skip to content

graphiql 5: allow multiple independant instances on the same page #3990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 262 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
262 commits
Select commit Hold shift + click to select a range
7a09053
upd
dimaMachina May 13, 2025
dba736e
upd
dimaMachina May 13, 2025
d16a30c
upd
dimaMachina May 13, 2025
58cb9eb
default query
dimaMachina May 13, 2025
518ed77
persist headers
dimaMachina May 13, 2025
0662a03
upd
dimaMachina May 13, 2025
cf1a268
upd
dimaMachina May 13, 2025
839d938
upd
dimaMachina May 13, 2025
f912245
upd
dimaMachina May 13, 2025
0abb0ec
upd
dimaMachina May 13, 2025
e8403f6
upd
dimaMachina May 13, 2025
5824175
upd
dimaMachina May 13, 2025
418b805
upd
dimaMachina May 13, 2025
7b9bc0b
upd
dimaMachina May 13, 2025
fec041b
upd
dimaMachina May 13, 2025
9afd50c
reduce rerenders
dimaMachina May 13, 2025
50b7ef3
add logs
dimaMachina May 13, 2025
82f6c09
try
dimaMachina May 13, 2025
7d9cd26
cleanup
dimaMachina May 13, 2025
30d298f
cspell
dimaMachina May 13, 2025
2e261ca
upd
dimaMachina May 14, 2025
7119c6a
upd
dimaMachina May 14, 2025
31241e2
upd
dimaMachina May 14, 2025
037485d
upd
dimaMachina May 14, 2025
13b008b
upd
dimaMachina May 14, 2025
93b1492
upd
dimaMachina May 14, 2025
5e572fd
upd
dimaMachina May 14, 2025
3648b78
upd
dimaMachina May 14, 2025
bc43eb3
upd
dimaMachina May 14, 2025
0595458
upd
dimaMachina May 14, 2025
24c2373
upd
dimaMachina May 14, 2025
384e7b0
upd
dimaMachina May 14, 2025
7a9224c
upd
dimaMachina May 14, 2025
727fd1a
upd
dimaMachina May 14, 2025
3ba26c5
upd
dimaMachina May 14, 2025
77303a1
upd
dimaMachina May 14, 2025
c1f402d
upd
dimaMachina May 14, 2025
7ca8fa1
upd
dimaMachina May 14, 2025
12cf600
upd
dimaMachina May 14, 2025
b0193b2
upd
dimaMachina May 14, 2025
576b52d
upd
dimaMachina May 14, 2025
e149cd3
upd
dimaMachina May 14, 2025
6a61215
upd
dimaMachina May 14, 2025
567cca6
upd
dimaMachina May 14, 2025
db445ec
upd
dimaMachina May 14, 2025
88a5be2
upd
dimaMachina May 14, 2025
9649e6d
upd
dimaMachina May 14, 2025
ced804c
upd
dimaMachina May 14, 2025
74c059f
upd
dimaMachina May 14, 2025
3580e94
upd
dimaMachina May 14, 2025
661df4b
upd
dimaMachina May 14, 2025
787d47c
upd
dimaMachina May 14, 2025
9a5e29f
upd
dimaMachina May 14, 2025
95e5e85
upd
dimaMachina May 14, 2025
27554e2
upd
dimaMachina May 14, 2025
454cb97
upd
dimaMachina May 14, 2025
ca53861
upd
dimaMachina May 14, 2025
5d07602
upd
dimaMachina May 14, 2025
a7ceaa8
upd
dimaMachina May 14, 2025
2bc1a06
upd
dimaMachina May 14, 2025
04747d3
upd
dimaMachina May 14, 2025
ef835fb
upd
dimaMachina May 14, 2025
6bcc55d
upd
dimaMachina May 14, 2025
6682497
upd
dimaMachina May 14, 2025
1fe7eaf
upd
dimaMachina May 14, 2025
7e85d71
upd
dimaMachina May 14, 2025
695832c
upd
dimaMachina May 14, 2025
c062b23
upd
dimaMachina May 14, 2025
9b3c029
upd
dimaMachina May 14, 2025
43e83d0
upd
dimaMachina May 14, 2025
efa078a
upd
dimaMachina May 14, 2025
a7d21a2
upd
dimaMachina May 14, 2025
df6bc60
Merge branch 'main' into v4-6
dimaMachina May 14, 2025
2723780
Merge branch 'v4-6' into v4-9
dimaMachina May 14, 2025
5207987
fixes
dimaMachina May 14, 2025
ed99173
fixes
dimaMachina May 14, 2025
0747d7e
fixes
dimaMachina May 14, 2025
f553781
fixes
dimaMachina May 14, 2025
39242be
fixes
dimaMachina May 14, 2025
2ecda33
upd
dimaMachina May 15, 2025
ad4a8a9
upd
dimaMachina May 15, 2025
0e8823f
upd
dimaMachina May 15, 2025
a83cf69
upd
dimaMachina May 15, 2025
62ea76e
upd
dimaMachina May 15, 2025
796aa20
upd
dimaMachina May 15, 2025
ce31c60
upd
dimaMachina May 15, 2025
0274e87
upd
dimaMachina May 15, 2025
65790ea
upd
dimaMachina May 15, 2025
78fc130
upd
dimaMachina May 15, 2025
6a14d7e
upd
dimaMachina May 15, 2025
b1ff040
upd
dimaMachina May 15, 2025
e42dd2d
upd
dimaMachina May 15, 2025
4be721b
upd
dimaMachina May 15, 2025
19bf02f
upd
dimaMachina May 15, 2025
b08df3c
upd
dimaMachina May 15, 2025
f43edcc
upd
dimaMachina May 15, 2025
9222dbf
upd
dimaMachina May 15, 2025
22693e7
upd
dimaMachina May 15, 2025
c1b09c8
upd
dimaMachina May 15, 2025
e8008ae
upd
dimaMachina May 15, 2025
00da1c8
upd
dimaMachina May 15, 2025
a6dda07
upd
dimaMachina May 15, 2025
8f92b6a
upd
dimaMachina May 15, 2025
f899b18
upd
dimaMachina May 15, 2025
e2ce749
upd
dimaMachina May 15, 2025
b7969bc
upd
dimaMachina May 15, 2025
1aab847
upd
dimaMachina May 15, 2025
980c83a
upd
dimaMachina May 15, 2025
1882e20
upd
dimaMachina May 15, 2025
fa90c0c
upd
dimaMachina May 15, 2025
036ebde
upd
dimaMachina May 15, 2025
3290391
upd
dimaMachina May 15, 2025
b78624a
upd
dimaMachina May 15, 2025
005091e
upd
dimaMachina May 15, 2025
154cb8b
upd
dimaMachina May 15, 2025
f799696
upd
dimaMachina May 15, 2025
9bb265f
upd
dimaMachina May 15, 2025
8a894da
upd
dimaMachina May 15, 2025
83825c3
upd
dimaMachina May 15, 2025
e78fa9a
upd
dimaMachina May 15, 2025
a785297
upd
dimaMachina May 15, 2025
d2f0d36
upd
dimaMachina May 15, 2025
e9c0567
upd
dimaMachina May 15, 2025
aa701c5
upd
dimaMachina May 15, 2025
639bc4c
upd
dimaMachina May 15, 2025
6f44cd8
upd
dimaMachina May 15, 2025
e4eb50c
prettier
dimaMachina May 15, 2025
14be107
upd
dimaMachina May 15, 2025
388495d
move plugins check to store
dimaMachina May 15, 2025
bc2c655
upd
dimaMachina May 15, 2025
7e937c3
upd
dimaMachina May 15, 2025
25ba8cc
refactor
dimaMachina May 15, 2025
319cb36
refactor
dimaMachina May 15, 2025
0623c2d
upd
dimaMachina May 15, 2025
f8cecc2
upd
dimaMachina May 15, 2025
7320d0d
upd
dimaMachina May 15, 2025
fe17c0b
upd
dimaMachina May 15, 2025
df29d87
upd
dimaMachina May 15, 2025
e63113b
upd
dimaMachina May 15, 2025
537a8f9
lint fix
dimaMachina May 15, 2025
5c1eec5
Merge branch 'main' into v4-9
dimaMachina May 20, 2025
c94a092
Merge branch 'v4-9' into v4-10
dimaMachina May 20, 2025
3f701ba
all in one
dimaMachina May 26, 2025
2e4ff5b
yarn lokc
dimaMachina May 26, 2025
d75740c
fix
dimaMachina May 26, 2025
c0ed68d
yarn.lock
dimaMachina May 26, 2025
768ab60
update babel.config.js
dimaMachina May 26, 2025
a2765fb
fix tests
dimaMachina May 26, 2025
c08fcdb
update examples
dimaMachina May 26, 2025
deedf19
yarn.lock
dimaMachina May 26, 2025
ce303b7
upd
dimaMachina May 26, 2025
1b0ebc8
upd
dimaMachina May 26, 2025
1179c7b
upd
dimaMachina May 26, 2025
9c1bb09
upd
dimaMachina May 26, 2025
008f4d3
upd
dimaMachina May 26, 2025
7a1105a
upd
dimaMachina May 26, 2025
60bdb85
upd
dimaMachina May 26, 2025
e5cb2b6
upd
dimaMachina May 26, 2025
d980a98
upd
dimaMachina May 26, 2025
75f7641
upd
dimaMachina May 26, 2025
25cbdd3
upd
dimaMachina May 26, 2025
ca296ab
upd
dimaMachina May 26, 2025
3ead625
upd
dimaMachina May 26, 2025
6b0c6e8
upd
dimaMachina May 26, 2025
f705612
upd
dimaMachina May 26, 2025
ccbf762
upd
dimaMachina May 26, 2025
a007466
upd
dimaMachina May 27, 2025
a47540e
upd
dimaMachina May 27, 2025
5b9665b
upd
dimaMachina May 30, 2025
b83dba5
upd
dimaMachina May 30, 2025
129d3dd
upd
dimaMachina May 30, 2025
b10b1af
upd
dimaMachina May 30, 2025
ebbb63b
upd
dimaMachina May 30, 2025
31a4ca8
pick
dimaMachina May 31, 2025
d4c3f1f
provider
dimaMachina May 31, 2025
d2a01cd
autocomplete leafs
dimaMachina May 31, 2025
d7a733b
editors
dimaMachina May 31, 2025
7b76a31
query editor
dimaMachina May 31, 2025
196d506
upd
dimaMachina May 31, 2025
71e24ef
upd
dimaMachina May 31, 2025
32eae07
upd
dimaMachina May 31, 2025
f96f7cc
useSynchronizeValue
dimaMachina May 31, 2025
497de53
synchronizeActiveTabValues
dimaMachina May 31, 2025
f58f37b
synchronizeActiveTabValues and setEditorValues
dimaMachina May 31, 2025
3c7cc93
storeTabs
dimaMachina May 31, 2025
fc434d6
actions
dimaMachina May 31, 2025
d8ea848
setOperationFacts
dimaMachina May 31, 2025
96a101c
cleanup
dimaMachina May 31, 2025
dff85b6
move editor logic to provider
dimaMachina May 31, 2025
a8e3ce5
add InnerGraphiQLProvider
dimaMachina May 31, 2025
8844ce0
move editor logic
dimaMachina May 31, 2025
759912e
move more
dimaMachina May 31, 2025
33ca9ee
move schema
dimaMachina May 31, 2025
9996e99
refactor plugins and graphiql
dimaMachina May 31, 2025
b1d5eb0
upd
dimaMachina May 31, 2025
346482e
upd
dimaMachina May 31, 2025
1343e96
swap isIntrospecting and isFetching
dimaMachina May 31, 2025
fe33c88
cleanupDisposables
dimaMachina May 31, 2025
11d6ea9
lint
dimaMachina May 31, 2025
df85f9a
add test-d typecheck test
dimaMachina May 31, 2025
d90e3a4
gitignore
dimaMachina May 31, 2025
b83d876
Merge branch 'graphiql-5' into monaco4-slices
dimaMachina Jun 5, 2025
e27c076
run yarn
dimaMachina Jun 5, 2025
bca83ff
fix graphiql build
dimaMachina Jun 5, 2025
b97ac72
use cn
dimaMachina Jun 5, 2025
1352370
fix history tests
dimaMachina Jun 5, 2025
81c564f
fix tests
dimaMachina Jun 6, 2025
606f790
types check pass
dimaMachina Jun 6, 2025
116cf8f
run ayrn
dimaMachina Jun 6, 2025
3a0bf02
run ayrn
dimaMachina Jun 6, 2025
8a0acbc
tests pass
dimaMachina Jun 6, 2025
87317fd
upd
dimaMachina Jun 6, 2025
fc2acdc
Delete .changeset/five-cars-roll.md
dimaMachina Jun 6, 2025
fece9b4
Update packages/graphiql-plugin-doc-explorer/src/components/__tests__…
dimaMachina Jun 6, 2025
d753b59
fix run at cursor action
dimaMachina Jun 6, 2025
0ac9f55
add run action for response editor
dimaMachina Jun 6, 2025
4411566
fix copy query action
dimaMachina Jun 6, 2025
68d92e8
copy query
dimaMachina Jun 6, 2025
aed4a7f
fix prettify editors action
dimaMachina Jun 6, 2025
3369d08
fix merge query action
dimaMachina Jun 6, 2025
119e7c4
polish
dimaMachina Jun 6, 2025
d736ec1
move `getAutoCompleteLeafs`
dimaMachina Jun 6, 2025
d5ab266
formatShortcutForOS
dimaMachina Jun 6, 2025
efe1dd7
formatShortcutForOS
dimaMachina Jun 6, 2025
fa27fa3
formatShortcutForOS
dimaMachina Jun 6, 2025
96f0c17
remove on click reference from variable editor
dimaMachina Jun 6, 2025
bf93b8b
store on click reference in ref
dimaMachina Jun 6, 2025
f98ecbe
polish
dimaMachina Jun 6, 2025
ba15482
add changeset and allow override all default GraphiQL plugins
dimaMachina Jun 6, 2025
53ef0f1
upd
dimaMachina Jun 6, 2025
b4d6987
better autocomplete for editor theme
dimaMachina Jun 6, 2025
601d0bd
try fix tests
dimaMachina Jun 6, 2025
17d6d23
Merge branch 'graphiql-5' into monaco4-slices
dimaMachina Jun 6, 2025
dfea9e5
merge conflicts and fix ClipboardItem is not defined
dimaMachina Jun 6, 2025
9948a32
fix vitest and e2e
dimaMachina Jun 6, 2025
45950e5
Update .changeset/beige-months-care.md
dimaMachina Jun 6, 2025
b5727cb
upd setup-files
dimaMachina Jun 6, 2025
c7be443
upd monaco-editor mocks
dimaMachina Jun 6, 2025
4eef6b7
fix lint
dimaMachina Jun 6, 2025
7d5b98d
should works now!
dimaMachina Jun 6, 2025
7995d95
polish
dimaMachina Jun 6, 2025
cff0ea7
polish
dimaMachina Jun 6, 2025
71d8919
polish
dimaMachina Jun 6, 2025
bc6567c
build pass on next 15.4
dimaMachina Jun 6, 2025
5564bec
remove unneeded actions
dimaMachina Jun 6, 2025
f617daa
Merge branch 'graphiql-5' into monaco4-slices
dimaMachina Jun 6, 2025
b268371
merge conflicts
dimaMachina Jun 6, 2025
4954310
Update .changeset/beige-months-care.md
dimaMachina Jun 6, 2025
2fb2cd4
Update .changeset/beige-months-care.md
dimaMachina Jun 6, 2025
f376691
rm next.js example because netlify fails
dimaMachina Jun 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/beige-months-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@graphiql/plugin-doc-explorer': minor
'@graphiql/plugin-explorer': major
'@graphiql/plugin-history': minor
'@graphiql/react': minor
'graphiql': major
---

- allow multiple independent instances of GraphiQL on the same page
- store `onClickReference` in query editor in React `ref`
- remove `onClickReference` from variable editor
- fix shortcut text per OS for run query in execute query button's tooltip and in default query
- allow override all default GraphiQL plugins
- adjust operation argument color to be purple from GraphiQL v2 on dark/light theme
20 changes: 17 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ module.exports = {
'functions/*',
'packages/vscode-graphql-syntax/tests/__fixtures__/*',
// symlinks
'packages/graphiql-react/__mocks__/monaco-editor.ts',
'packages/graphiql-plugin-doc-explorer/__mocks__/zustand.ts',
'packages/graphiql-plugin-doc-explorer/__mocks__/monaco-editor.ts',
'packages/graphiql-plugin-history/__mocks__/zustand.ts',
'packages/graphiql-plugin-history/__mocks__/monaco-editor.ts',
],
overrides: [
{
Expand Down Expand Up @@ -147,6 +150,10 @@ module.exports = {
property: 'getComputedStyle',
message: 'Use `getComputedStyle` instead',
},
{
object: 'self',
message: 'Use `globalThis` instead',
},
],
'no-return-assign': 'error',
'no-return-await': 'error',
Expand Down Expand Up @@ -367,6 +374,7 @@ module.exports = {
'unicorn/no-length-as-slice-end': 'error',
'unicorn/prefer-string-replace-all': 'error',
'unicorn/prefer-array-some': 'error',
// '@typescript-eslint/prefer-for-of': 'error', TODO
'unicorn/no-hex-escape': 'off', // TODO: enable
// doesn't catch a lot of cases; we use ESLint builtin `no-restricted-syntax` to forbid `.keyCode`
'unicorn/prefer-keyboard-event-key': 'off',
Expand All @@ -379,6 +387,13 @@ module.exports = {
'import-x/no-named-as-default-member': 'off',
},
},
{
files: ['packages/{monaco-graphql,graphiql*}/**/*.{ts,tsx,mts,cts}'],
excludedFiles: ['packages/graphiql-toolkit/**/*.{ts,tsx}'],
rules: {
'@typescript-eslint/no-unnecessary-condition': 'error',
},
},
{
// Rules that requires type information
files: ['**/*.{ts,tsx,mts,cts}'],
Expand Down Expand Up @@ -515,9 +530,7 @@ module.exports = {
rules: {
'@typescript-eslint/no-restricted-imports': [
'error',
...RESTRICTED_IMPORTS
// TODO: enable when monaco-editor will be migrated over codemirror
.filter(({ name }) => name !== 'monaco-editor'),
...RESTRICTED_IMPORTS,
{
name: 'react',
importNames: ['memo', 'useCallback', 'useMemo'],
Expand Down Expand Up @@ -572,6 +585,7 @@ module.exports = {
'react-hooks/rules-of-hooks': 'off',
'sonarjs/no-dead-store': 'off',
'@typescript-eslint/no-restricted-imports': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions examples/graphiql-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"private": true,
"scripts": {
"types:check": "tsc --noEmit",
"dev": "next --turbopack",
"build": "next build --turbopack",
"dev": "next",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"graphiql": "^5.0.0-rc.0",
"next": "15.3.3",
"next": "15.4.0",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
Expand Down
8 changes: 4 additions & 4 deletions examples/monaco-graphql-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"type": "module",
"scripts": {
"types:check": "tsc --noEmit",
"dev": "next --turbopack",
"build": "next build --turbopack",
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@graphiql/toolkit": "^0.11.3",
"graphql": "^16.9.0",
"jsonc-parser": "^3.2.0",
"monaco-editor": "^0.39.0",
"monaco-editor": "^0.52.2",
"monaco-graphql": "^1.7.0",
"next": "15.3.3",
"next": "15.4.0",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/monaco-graphql-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"graphql-language-service": "^5.4.0",
"json-schema": "^0.4.0",
"jsonc-parser": "^3.2.0",
"monaco-editor": "^0.47.0",
"monaco-editor": "^0.52.2",
"monaco-graphql": "^1.7.0",
"prettier": "3.3.2"
},
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"examples/monaco-graphql-nextjs",
"examples/monaco-graphql-react-vite",
"examples/graphiql-vite",
"examples/graphiql-nextjs",
"examples/graphiql-webpack"
]
},
Expand Down Expand Up @@ -128,7 +127,6 @@
"wsrun": "^5.2.4"
},
"resolutions": {
"monaco-editor": "0.47.0",
"@babel/traverse": "^7.23.2",
"vscode-languageserver-types": "3.17.3",
"markdown-it": "14.1.0",
Expand Down
36 changes: 3 additions & 33 deletions packages/graphiql-plugin-doc-explorer/setup-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,6 @@

import '@testing-library/jest-dom';

vi.mock('zustand'); // to make it works like Jest (auto-mocking)

/**
* Fixes TypeError: document.queryCommandSupported is not a function
*/
if (!navigator.clipboard) {
Object.defineProperty(navigator, 'clipboard', {
writable: false,
value: {
write: async () => null,
},
});
}

/**
* Fixes TypeError: mainWindow.matchMedia is not a function
* @see https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
*/
if (!window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
writable: false,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}
// to make it works like Jest (auto-mocking)
vi.mock('zustand');
vi.mock('monaco-editor');
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { act, render } from '@testing-library/react';
import { Mock } from 'vitest';
import { useGraphiQL as $useGraphiQL } from '@graphiql/react';
import { render } from '@testing-library/react';
import { GraphQLInt, GraphQLObjectType, GraphQLSchema } from 'graphql';
import { FC, useEffect } from 'react';
import {
Expand All @@ -7,7 +9,17 @@ import {
useDocExplorerActions,
} from '../../context';
import { DocExplorer } from '../doc-explorer';
import { schemaStore } from '../../../../graphiql-react/dist/stores/schema';

const useGraphiQL = $useGraphiQL as Mock;

vi.mock('@graphiql/react', async () => {
const originalModule =
await vi.importActual<typeof import('@graphiql/react')>('@graphiql/react');
return {
...originalModule,
useGraphiQL: vi.fn(),
};
});

function makeSchema(fieldName = 'field') {
return new GraphQLSchema({
Expand All @@ -29,15 +41,16 @@ function makeSchema(fieldName = 'field') {
}

const defaultSchemaContext = {
...schemaStore.getInitialState(),
async introspect() {},
fetchError: null,
introspect() {},
isFetching: false,
schema: makeSchema(),
validationErrors: [],
};

const withErrorSchemaContext = {
...schemaStore.getInitialState(),
...defaultSchemaContext,
fetchError: 'Error fetching schema',
async introspect() {},
schema: new GraphQLSchema({ description: 'GraphQL Schema for testing' }),
};

Expand All @@ -50,21 +63,29 @@ const DocExplorerWithContext: FC = () => {
};

describe('DocExplorer', () => {
beforeEach(() => {
vi.resetModules();
});

it('renders spinner when the schema is loading', () => {
schemaStore.setState({ isFetching: true });
useGraphiQL.mockImplementation(cb =>
cb({ ...defaultSchemaContext, isFetching: true }),
);
const { container } = render(<DocExplorerWithContext />);
const spinner = container.querySelectorAll('.graphiql-spinner');
expect(spinner).toHaveLength(1);
});
it('renders with null schema', () => {
schemaStore.setState({ ...defaultSchemaContext, schema: null });
useGraphiQL.mockImplementation(cb =>
cb({ ...defaultSchemaContext, schema: null }),
);
const { container } = render(<DocExplorerWithContext />);
const error = container.querySelectorAll('.graphiql-doc-explorer-error');
expect(error).toHaveLength(1);
expect(error[0]).toHaveTextContent('No GraphQL schema available');
});
it('renders with schema', () => {
schemaStore.setState(defaultSchemaContext);
useGraphiQL.mockImplementation(cb => cb(defaultSchemaContext));
const { container } = render(<DocExplorerWithContext />);
const error = container.querySelectorAll('.graphiql-doc-explorer-error');
expect(error).toHaveLength(0);
Expand All @@ -73,14 +94,11 @@ describe('DocExplorer', () => {
).toHaveTextContent('GraphQL Schema for testing');
});
it('renders correctly with schema error', () => {
schemaStore.setState(withErrorSchemaContext);
useGraphiQL.mockImplementation(cb => cb(withErrorSchemaContext));
const { rerender, container } = render(<DocExplorerWithContext />);
const error = container.querySelector('.graphiql-doc-explorer-error');
expect(error).toHaveTextContent('Error fetching schema');

act(() => {
schemaStore.setState(defaultSchemaContext);
});
useGraphiQL.mockImplementation(cb => cb(defaultSchemaContext));
rerender(<DocExplorerWithContext />);
const errors = container.querySelectorAll('.graphiql-doc-explorer-error');
expect(errors).toHaveLength(0);
Expand All @@ -104,45 +122,38 @@ describe('DocExplorer', () => {
};

// Initial render, set initial state
schemaStore.setState({
...defaultSchemaContext,
schema: initialSchema,
});
useGraphiQL.mockImplementation(cb =>
cb({ ...defaultSchemaContext, schema: initialSchema }),
);
const { container, rerender } = render(
<DocExplorerStore>
<SetInitialStack />
</DocExplorerStore>,
);

// First proper render of doc explorer
act(() => {
schemaStore.setState({
...defaultSchemaContext,
schema: initialSchema,
});
});
rerender(
<DocExplorerStore>
<DocExplorer />
</DocExplorerStore>,
);

const [title] = container.querySelectorAll('.graphiql-doc-explorer-title');
const title = container.querySelector('.graphiql-doc-explorer-title')!;
expect(title.textContent).toEqual('field');

// Second render of doc explorer, this time with a new schema, with _same_ field name
act(() => {
schemaStore.setState({
useGraphiQL.mockImplementation(cb =>
cb({
...defaultSchemaContext,
schema: makeSchema(), // <<< New, but equivalent, schema
});
});
}),
);
rerender(
<DocExplorerStore>
<DocExplorer />
</DocExplorerStore>,
);
const [title2] = container.querySelectorAll('.graphiql-doc-explorer-title');
const title2 = container.querySelector('.graphiql-doc-explorer-title')!;
// Because `Query.field` still exists in the new schema, we can still render it
expect(title2.textContent).toEqual('field');
});
Expand All @@ -166,23 +177,16 @@ describe('DocExplorer', () => {
};

// Initial render, set initial state
schemaStore.setState({
...defaultSchemaContext,
schema: initialSchema,
});
useGraphiQL.mockImplementation(cb =>
cb({ ...defaultSchemaContext, schema: initialSchema }),
);
const { container, rerender } = render(
<DocExplorerStore>
<SetInitialStack />
</DocExplorerStore>,
);

// First proper render of doc explorer
act(() => {
schemaStore.setState({
...defaultSchemaContext,
schema: initialSchema,
});
});
rerender(
<DocExplorerStore>
<DocExplorer />
Expand All @@ -193,12 +197,12 @@ describe('DocExplorer', () => {
expect(title.textContent).toEqual('field');

// Second render of doc explorer, this time with a new schema, with a different field name
act(() => {
schemaStore.setState({
useGraphiQL.mockImplementation(cb =>
cb({
...defaultSchemaContext,
schema: makeSchema('field2'), // <<< New schema with a new field name
});
});
}),
);
rerender(
<DocExplorerStore>
<DocExplorer />
Expand Down
Loading
Loading