Skip to content

feat(@graphiql/react): migrate React context to zustand. Replace useExecutionContext with useExecutionStore hook and replace useEditorContext with useEditorStore hook #3946

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 37 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7c5f11f
use execution store
dimaMachina May 13, 2025
53cb535
upd
dimaMachina May 13, 2025
e52977e
upd
dimaMachina May 13, 2025
fcfd519
upd
dimaMachina May 13, 2025
8db47ce
upd
dimaMachina May 13, 2025
7589253
upd
dimaMachina May 13, 2025
9425ff8
upd
dimaMachina May 13, 2025
f0d728d
upd
dimaMachina May 13, 2025
7335522
upd
dimaMachina May 13, 2025
6ac226e
upd
dimaMachina May 13, 2025
7bf7e80
upd
dimaMachina May 13, 2025
b28cc14
upd
dimaMachina May 13, 2025
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
df6bc60
Merge branch 'main' into v4-6
dimaMachina May 14, 2025
b17adfe
Update index.ts
dimaMachina May 15, 2025
04503d4
Update index.ts
dimaMachina May 15, 2025
3412b7f
Update .changeset/five-cars-roll.md
dimaMachina May 20, 2025
190130b
Merge branch 'main' into v4-6
dimaMachina May 20, 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
12 changes: 12 additions & 0 deletions .changeset/five-cars-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@graphiql/plugin-doc-explorer': patch
'@graphiql/plugin-explorer': patch
'@graphiql/plugin-history': patch
'@graphiql/react': minor
'graphiql': minor
---

feat(@graphiql/react): migrate React context to zustand:
- replace `useExecutionContext` with `useExecutionStore` hook
- replace `useEditorContext` with `useEditorStore` hook
- replace `useAutoCompleteLeafs` hook with `getAutoCompleteLeafs` function
2 changes: 1 addition & 1 deletion packages/graphiql-plugin-doc-explorer/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
} from 'graphql';
import { FC, ReactElement, ReactNode, useEffect } from 'react';
import {
createBoundedUseStore,
SchemaContextType,
useSchemaStore,
createBoundedUseStore,
} from '@graphiql/react';
import { createStore } from 'zustand';

Expand Down
8 changes: 4 additions & 4 deletions packages/graphiql-plugin-explorer/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { CSSProperties, FC, useCallback } from 'react';
import {
GraphiQLPlugin,
useEditorContext,
useExecutionContext,
useEditorStore,
useExecutionStore,
useSchemaStore,
useOperationsEditorState,
useOptimisticState,
Expand Down Expand Up @@ -62,9 +62,9 @@ export type GraphiQLExplorerPluginProps = Omit<
>;

const ExplorerPlugin: FC<GraphiQLExplorerPluginProps> = props => {
const { setOperationName } = useEditorContext({ nonNull: true });
const { setOperationName } = useEditorStore();
const { schema } = useSchemaStore();
const { run } = useExecutionContext({ nonNull: true });
const { run } = useExecutionStore();

// handle running the current operation from the plugin
const handleRunOperation = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ComponentProps } from 'react';
import { formatQuery, HistoryItem } from '../components';
import { HistoryContextProvider } from '../context';
import {
useEditorContext,
useEditorStore,
Tooltip,
StorageContextProvider,
} from '@graphiql/react';
Expand All @@ -16,15 +16,15 @@ vi.mock('@graphiql/react', async () => {
const mockedSetHeaderEditor = vi.fn();
return {
...originalModule,
useEditorContext() {
useEditorStore() {
return {
queryEditor: { setValue: mockedSetQueryEditor },
variableEditor: { setValue: mockedSetVariableEditor },
headerEditor: { setValue: mockedSetHeaderEditor },
tabs: [],
};
},
useExecutionContext() {
useExecutionStore() {
return {};
},
};
Expand Down Expand Up @@ -78,12 +78,10 @@ function getMockProps(
}

describe('QueryHistoryItem', () => {
const mockedSetQueryEditor = useEditorContext()!.queryEditor!
.setValue as Mock;
const mockedSetVariableEditor = useEditorContext()!.variableEditor!
.setValue as Mock;
const mockedSetHeaderEditor = useEditorContext()!.headerEditor!
.setValue as Mock;
const store = useEditorStore();
const mockedSetQueryEditor = store.queryEditor!.setValue as Mock;
const mockedSetVariableEditor = store.variableEditor!.setValue as Mock;
const mockedSetHeaderEditor = store.headerEditor!.setValue as Mock;
beforeEach(() => {
mockedSetQueryEditor.mockClear();
mockedSetVariableEditor.mockClear();
Expand Down
7 changes: 2 additions & 5 deletions packages/graphiql-plugin-history/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StarFilledIcon,
StarIcon,
TrashIcon,
useEditorContext,
useEditorStore,
Button,
Tooltip,
UnStyledButton,
Expand Down Expand Up @@ -112,10 +112,7 @@ type QueryHistoryItemProps = {
export const HistoryItem: FC<QueryHistoryItemProps> = props => {
const { editLabel, toggleFavorite, deleteFromHistory, setActive } =
useHistoryActions();
const { headerEditor, queryEditor, variableEditor } = useEditorContext({
nonNull: true,
caller: HistoryItem,
});
const { headerEditor, queryEditor, variableEditor } = useEditorStore();
const inputRef = useRef<HTMLInputElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
const [isEditable, setIsEditable] = useState(false);
Expand Down
8 changes: 4 additions & 4 deletions packages/graphiql-plugin-history/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FC, ReactElement, ReactNode, useEffect } from 'react';
import { createStore } from 'zustand';
import { HistoryStore, QueryStoreItem } from '@graphiql/toolkit';
import {
useExecutionContext,
useEditorContext,
useExecutionStore,
useEditorStore,
useStorage,
createBoundedUseStore,
} from '@graphiql/react';
Expand Down Expand Up @@ -119,8 +119,8 @@ export const HistoryContextProvider: FC<HistoryContextProviderProps> = ({
maxHistoryLength = 20,
children,
}) => {
const { isFetching } = useExecutionContext({ nonNull: true });
const { tabs, activeTabIndex } = useEditorContext({ nonNull: true });
const { isFetching } = useExecutionStore();
const { tabs, activeTabIndex } = useEditorStore();
const activeTab = tabs[activeTabIndex];
const storage = useStorage();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, useEffect } from 'react';
import { clsx } from 'clsx';
import { useEditorContext } from '../context';
import { useEditorStore } from '../context';
import { useHeaderEditor, UseHeaderEditorArgs } from '../header-editor';
import '../style/codemirror.css';
import '../style/fold.css';
Expand All @@ -18,11 +18,8 @@ export const HeaderEditor: FC<HeaderEditorProps> = ({
isHidden,
...hookArgs
}) => {
const { headerEditor } = useEditorContext({
nonNull: true,
caller: HeaderEditor,
});
const ref = useHeaderEditor(hookArgs, HeaderEditor);
const headerEditor = useEditorStore(store => store.headerEditor);
const ref = useHeaderEditor(hookArgs);

useEffect(() => {
if (!isHidden) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../style/info.css';
import '../style/editor.css';

export const ResponseEditor: FC<UseResponseEditorArgs> = props => {
const ref = useResponseEditor(props, ResponseEditor);
const ref = useResponseEditor(props);
return (
<section
className="result-window"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC, useEffect } from 'react';
import { clsx } from 'clsx';

import { useEditorContext } from '../context';
import { useEditorStore } from '../context';
import { useVariableEditor, UseVariableEditorArgs } from '../variable-editor';

import '../style/codemirror.css';
Expand All @@ -22,11 +22,8 @@ export const VariableEditor: FC<VariableEditorProps> = ({
isHidden,
...hookArgs
}) => {
const { variableEditor } = useEditorContext({
nonNull: true,
caller: VariableEditor,
});
const ref = useVariableEditor(hookArgs, VariableEditor);
const variableEditor = useEditorStore(store => store.variableEditor);
const ref = useVariableEditor(hookArgs);

useEffect(() => {
if (!isHidden) {
Expand Down
Loading
Loading