Skip to content

Commit 117627b

Browse files
authored
feat(@graphiql/react): migrate React context to zustand, replace usePluginContext with usePluginStore hook (#3945)
* upd * upd * upd * upd * upd * upd * upd * upd * upd * upd * upd * changeset * upd * upd * fix tests * fix example * fix tests * upd * upd * upd * upd * upd * less git lines * fix tests * fix tests * lint * fix build * fix build * fix cspell * Update packages/graphiql-react/src/schema.ts
1 parent 7275472 commit 117627b

18 files changed

Lines changed: 186 additions & 117 deletions

File tree

.changeset/good-geese-joke.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphiql/plugin-doc-explorer': patch
3+
'@graphiql/plugin-explorer': patch
4+
'@graphiql/plugin-history': patch
5+
'@graphiql/react': minor
6+
'graphiql': patch
7+
---
8+
9+
feat(@graphiql/react): migrate React context to zustand, replace `usePluginContext` with `usePluginStore` hook
10+

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module.exports = {
3131
'**/CHANGELOG.md',
3232
'functions/*',
3333
'packages/vscode-graphql-syntax/tests/__fixtures__/*',
34+
// symlinks
35+
'packages/graphiql-plugin-doc-explorer/__mocks__/zustand.ts',
36+
'packages/graphiql-plugin-history/__mocks__/zustand.ts',
3437
],
3538
overrides: [
3639
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../graphiql/__mocks__/zustand.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
'use no memo';
22

33
import '@testing-library/jest-dom';
4+
5+
vi.mock('zustand'); // to make it works like Jest (auto-mocking)

packages/graphiql-plugin-doc-explorer/src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
DocsFilledIcon,
33
DocsIcon,
44
GraphiQLPlugin,
5-
usePluginContext,
5+
usePluginStore,
66
} from '@graphiql/react';
77
import { DocExplorer } from './components';
88

@@ -25,8 +25,8 @@ export type {
2525
export const DOC_EXPLORER_PLUGIN: GraphiQLPlugin = {
2626
title: 'Documentation Explorer',
2727
icon: function Icon() {
28-
const pluginContext = usePluginContext();
29-
return pluginContext?.visiblePlugin === DOC_EXPLORER_PLUGIN ? (
28+
const { visiblePlugin } = usePluginStore();
29+
return visiblePlugin === DOC_EXPLORER_PLUGIN ? (
3030
<DocsFilledIcon />
3131
) : (
3232
<DocsIcon />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../graphiql/__mocks__/zustand.ts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use no memo';
2+
3+
import '@testing-library/jest-dom';
4+
5+
vi.mock('zustand'); // to make it works like Jest (auto-mocking)

packages/graphiql-plugin-history/vitest.config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export default defineConfig({
66
test: {
77
globals: true,
88
environment: 'jsdom',
9+
setupFiles: ['./setup-files.ts'],
910
},
1011
});

packages/graphiql-react/src/editor/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import copyToClipboard from 'copy-to-clipboard';
1010
import { parse, print } from 'graphql';
1111
// eslint-disable-next-line @typescript-eslint/no-restricted-imports -- TODO: check why query builder update only 1st field https://github.com/graphql/graphiql/issues/3836
1212
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
13-
import { usePluginContext } from '../plugin';
13+
import { usePluginStore } from '../plugin';
1414
import { useSchemaStore } from '../schema';
1515
import { useStorage } from '../storage';
1616
import { debounce } from '../utility';
@@ -97,7 +97,7 @@ export function useCompletion(
9797
callback?: (reference: SchemaReference) => void,
9898
) {
9999
const { schema, setSchemaReference } = useSchemaStore();
100-
const plugin = usePluginContext();
100+
const plugin = usePluginStore();
101101
useEffect(() => {
102102
if (!editor) {
103103
return;

packages/graphiql-react/src/editor/query-editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { RefObject, useEffect, useRef } from 'react';
1515
import { useExecutionContext } from '../execution';
1616
import { markdown } from '../markdown';
17-
import { usePluginContext } from '../plugin';
17+
import { usePluginStore } from '../plugin';
1818
import { useSchemaStore } from '../schema';
1919
import { useStorage } from '../storage';
2020
import { debounce } from '../utility/debounce';
@@ -145,7 +145,7 @@ export function useQueryEditor(
145145
});
146146
const executionContext = useExecutionContext();
147147
const storage = useStorage();
148-
const plugin = usePluginContext();
148+
const plugin = usePluginStore();
149149
const copy = useCopyQuery({ caller: caller || _useQueryEditor, onCopyQuery });
150150
const merge = useMergeQuery({ caller: caller || _useQueryEditor });
151151
const prettify = usePrettifyEditors({

0 commit comments

Comments
 (0)