Skip to content

Commit 5e76a4f

Browse files
authored
fix query builder updated only first selected field in query editor due recent enabled react-compiler (#3837)
* a * a
1 parent 2a9a77c commit 5e76a4f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

.changeset/long-parents-grow.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphiql/react': patch
3+
'@graphiql/plugin-explorer': patch
4+
---
5+
6+
fix query builder updated only first selected field in query editor due recent enabled react-compiler

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { EditorChange, EditorConfiguration } from 'codemirror';
33
import type { SchemaReference } from 'codemirror-graphql/utils/SchemaReference';
44
import copyToClipboard from 'copy-to-clipboard';
55
import { parse, print } from 'graphql';
6-
import { useEffect, useRef, useState } from 'react';
6+
// 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
7+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
78

89
import { useExplorerContext } from '../explorer';
910
import { usePluginContext } from '../plugin';
@@ -344,9 +345,8 @@ export function useAutoCompleteLeafs({
344345

345346
// https://react.dev/learn/you-might-not-need-an-effect
346347

347-
export const useEditorState = (
348-
editor: 'query' | 'variable' | 'header',
349-
): [string, (val: string) => void] => {
348+
export const useEditorState = (editor: 'query' | 'variable' | 'header') => {
349+
'use no memo'; // eslint-disable-line react-compiler/react-compiler -- TODO: check why query builder update only 1st field https://github.com/graphql/graphiql/issues/3836
350350
const context = useEditorContext({
351351
nonNull: true,
352352
});
@@ -358,8 +358,14 @@ export const useEditorState = (
358358
valueString = editorValue;
359359
}
360360

361-
const handleEditorValue = (value: string) => editorInstance?.setValue(value);
362-
return [valueString, handleEditorValue];
361+
const handleEditorValue = useCallback(
362+
(value: string) => editorInstance?.setValue(value),
363+
[editorInstance],
364+
);
365+
return useMemo<[string, (val: string) => void]>(
366+
() => [valueString, handleEditorValue],
367+
[valueString, handleEditorValue],
368+
);
363369
};
364370

365371
/**

0 commit comments

Comments
 (0)