Skip to content

[1] various refactor after zustand migration #3949

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 99 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 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
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
5c1eec5
Merge branch 'main' into v4-9
dimaMachina May 20, 2025
bbe615f
Merge branch 'graphiql-5' into v4-9
dimaMachina Jun 1, 2025
d15dc9d
Delete .changeset/five-cars-roll.md
dimaMachina Jun 1, 2025
94bcf22
Apply suggestions from code review
dimaMachina Jun 3, 2025
f5627c4
Apply suggestions from code review
dimaMachina Jun 3, 2025
69dba43
Update .changeset/chilly-sloths-heal.md
dimaMachina Jun 3, 2025
4b2cb7b
Update .changeset/chilly-sloths-heal.md
dimaMachina Jun 3, 2025
277c17e
Update .changeset/chilly-sloths-heal.md
dimaMachina Jun 3, 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
27 changes: 27 additions & 0 deletions .changeset/chilly-sloths-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'@graphiql/plugin-doc-explorer': patch
'@graphiql/plugin-explorer': patch
'graphql-language-service': patch
'@graphiql/plugin-history': patch
'codemirror-graphql': patch
'@graphiql/react': minor
'graphiql': patch
---

- replace `onCopyQuery` hook with `copyQuery` function
- replace `onMergeQuery` hook with `mergeQuery` function
- replace `onPrettifyEditors` hook with `prettifyEditors` function
- remove `fetcher` prop from `SchemaContextProvider` and `schemaStore` and add `fetcher` to `executionStore`
- add `onCopyQuery` and `onPrettifyQuery` props to `EditorContextProvider`
- remove exports (use `GraphiQLProvider`)
- `EditorContextProvider`
- `ExecutionContextProvider`
- `PluginContextProvider`
- `SchemaContextProvider`
- `StorageContextProvider`
- `ExecutionContextType`
- `PluginContextType`
- feat(@graphiql/react): migrate React context to zustand:
- replace `useExecutionContext` with `useExecutionStore` hook
- replace `useEditorContext` with `useEditorStore` hook
- prefer `getComputedStyle` over `window.getComputedStyle`
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ module.exports = {
property: 'navigator',
message: 'Use `navigator` instead',
},
{
object: 'window',
property: 'getComputedStyle',
message: 'Use `getComputedStyle` instead',
},
],
'no-return-assign': 'error',
'no-return-await': 'error',
Expand Down
12 changes: 7 additions & 5 deletions packages/codemirror-graphql/src/utils/info-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,19 @@ function showPopup(cm: CodeMirror.Editor, box: DOMRect, info: HTMLDivElement) {
document.body.append(popup);

const popupBox = popup.getBoundingClientRect();
const popupStyle = window.getComputedStyle(popup);
const { marginLeft, marginRight, marginBottom, marginTop } =
getComputedStyle(popup);

const popupWidth =
popupBox.right -
popupBox.left +
parseFloat(popupStyle.marginLeft) +
parseFloat(popupStyle.marginRight);
parseFloat(marginLeft) +
parseFloat(marginRight);
const popupHeight =
popupBox.bottom -
popupBox.top +
parseFloat(popupStyle.marginTop) +
parseFloat(popupStyle.marginBottom);
parseFloat(marginTop) +
parseFloat(marginBottom);

let topPos = box.bottom;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ type FieldMatch = {

export function useSearchResults() {
const explorerNavStack = useDocExplorer();
const { schema } = useSchemaStore();
const schema = useSchemaStore(store => store.schema);

const navItem = explorerNavStack.at(-1)!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const EnumValue: FC<{ value: GraphQLEnumValue }> = ({ value }) => {
};

const PossibleTypes: FC<{ type: GraphQLNamedType }> = ({ type }) => {
const { schema } = useSchemaStore();
const schema = useSchemaStore(store => store.schema);
if (!schema || !isAbstractType(type)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql-plugin-doc-explorer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type {
export const DOC_EXPLORER_PLUGIN: GraphiQLPlugin = {
title: 'Documentation Explorer',
icon: function Icon() {
const { visiblePlugin } = usePluginStore();
const visiblePlugin = usePluginStore(store => store.visiblePlugin);
return visiblePlugin === DOC_EXPLORER_PLUGIN ? (
<DocsFilledIcon />
) : (
Expand Down
6 changes: 3 additions & 3 deletions packages/graphiql-plugin-explorer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export type GraphiQLExplorerPluginProps = Omit<
>;

const ExplorerPlugin: FC<GraphiQLExplorerPluginProps> = props => {
const { setOperationName } = useEditorStore();
const { schema } = useSchemaStore();
const { run } = useExecutionStore();
const setOperationName = useEditorStore(store => store.setOperationName);
const schema = useSchemaStore(store => store.schema);
const run = useExecutionStore(store => store.run);

// handle running the current operation from the plugin
const handleRunOperation = useCallback(
Expand Down
65 changes: 27 additions & 38 deletions packages/graphiql-plugin-history/src/__tests__/components.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import type { Mock } from 'vitest';
import { fireEvent, render } from '@testing-library/react';
import type { ComponentProps } from 'react';
import { formatQuery, HistoryItem } from '../components';
import { HistoryContextProvider } from '../context';
import {
useEditorStore,
Tooltip,
StorageContextProvider,
} from '@graphiql/react';

vi.mock('@graphiql/react', async () => {
const originalModule = await vi.importActual('@graphiql/react');
const mockedSetQueryEditor = vi.fn();
const mockedSetVariableEditor = vi.fn();
const mockedSetHeaderEditor = vi.fn();
return {
...originalModule,
useEditorStore() {
return {
queryEditor: { setValue: mockedSetQueryEditor },
variableEditor: { setValue: mockedSetVariableEditor },
headerEditor: { setValue: mockedSetHeaderEditor },
tabs: [],
};
},
useExecutionStore() {
return {};
},
};
});
import { Tooltip, GraphiQLProvider } from '@graphiql/react';
import { editorStore } from '../../../graphiql-react/dist/editor/context';

const mockQuery = /* GraphQL */ `
query Test($string: String) {
Expand All @@ -49,11 +24,11 @@ type QueryHistoryItemProps = ComponentProps<typeof HistoryItem>;
const QueryHistoryItemWithContext: typeof HistoryItem = props => {
return (
<Tooltip.Provider>
<StorageContextProvider>
<GraphiQLProvider fetcher={vi.fn()}>
<HistoryContextProvider>
<HistoryItem {...props} />
</HistoryContextProvider>
</StorageContextProvider>
</GraphiQLProvider>
</Tooltip.Provider>
);
};
Expand All @@ -78,15 +53,6 @@ function getMockProps(
}

describe('QueryHistoryItem', () => {
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();
mockedSetHeaderEditor.mockClear();
});
it('renders operationName if label is not provided', () => {
const otherMockProps = { item: { operationName: mockOperationName } };
const props = getMockProps(otherMockProps);
Expand All @@ -108,6 +74,29 @@ describe('QueryHistoryItem', () => {
});

it('selects the item when history label button is clicked', () => {
const mockedSetQueryEditor = vi.fn();
const mockedSetVariableEditor = vi.fn();
const mockedSetHeaderEditor = vi.fn();
type MonacoEditorWithOperationFacts = NonNullable<
ReturnType<typeof editorStore.getInitialState>['queryEditor']
>;
type MonacoEditor = NonNullable<
ReturnType<typeof editorStore.getInitialState>['variableEditor']
>;

editorStore.setState({
queryEditor: {
setValue: mockedSetQueryEditor,
} as unknown as MonacoEditorWithOperationFacts,
variableEditor: {
setValue: mockedSetVariableEditor,
} as unknown as MonacoEditor,
headerEditor: {
setValue: mockedSetHeaderEditor,
getValue: () => '',
} as unknown as MonacoEditor,
});

const otherMockProps = { item: { operationName: mockOperationName } };
const mockProps = getMockProps(otherMockProps);
const { container } = render(
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql-plugin-history/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const HistoryContextProvider: FC<HistoryContextProviderProps> = ({
maxHistoryLength = 20,
children,
}) => {
const { isFetching } = useExecutionStore();
const isFetching = useExecutionStore(store => store.isFetching);
const { tabs, activeTabIndex } = useEditorStore();
const activeTab = tabs[activeTabIndex];
const storage = useStorage();
Expand Down
23 changes: 17 additions & 6 deletions packages/graphiql-react/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export const KEY_MAP = Object.freeze({
prettify: ['Shift-Ctrl-P'],
mergeFragments: ['Shift-Ctrl-M'],
runQuery: ['Ctrl-Enter', 'Cmd-Enter'],
autoComplete: ['Ctrl-Space'],
copyQuery: ['Shift-Ctrl-C'],
refetchSchema: ['Shift-Ctrl-R'],
searchInEditor: ['Ctrl-F'],
searchInDocs: ['Ctrl-K'],
} as const);

export const DEFAULT_QUERY = `# Welcome to GraphiQL
#
# GraphiQL is an in-browser tool for writing, validating, and
# testing GraphQL queries.
# GraphiQL is an in-browser tool for writing, validating, and testing
# GraphQL queries.
#
# Type queries into this side of the screen, and you will see intelligent
# typeaheads aware of the current GraphQL type schema and live syntax and
Expand All @@ -20,13 +31,13 @@ export const DEFAULT_QUERY = `# Welcome to GraphiQL
#
# Keyboard shortcuts:
#
# Prettify query: Shift-Ctrl-P (or press the prettify button)
# Prettify query: ${KEY_MAP.prettify[0]} (or press the prettify button)
#
# Merge fragments: Shift-Ctrl-M (or press the merge button)
# Merge fragments: ${KEY_MAP.mergeFragments[0]} (or press the merge button)
#
# Run Query: Ctrl-Enter (or press the play button)
# Run Query: ${KEY_MAP.runQuery[0]} (or press the play button)
#
# Auto Complete: Ctrl-Space (or just start typing)
# Auto Complete: ${KEY_MAP.autoComplete[0]} (or just start typing)
#

`;
Loading
Loading