Skip to content

Commit

Permalink
fix query builder updated only first selected field in query editor d…
Browse files Browse the repository at this point in the history
…ue recent enabled react-compiler (#3837)

* a

* a
  • Loading branch information
dimaMachina authored Dec 16, 2024
1 parent 2a9a77c commit 5e76a4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/long-parents-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphiql/react': patch
'@graphiql/plugin-explorer': patch
---

fix query builder updated only first selected field in query editor due recent enabled react-compiler
18 changes: 12 additions & 6 deletions packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { EditorChange, EditorConfiguration } from 'codemirror';
import type { SchemaReference } from 'codemirror-graphql/utils/SchemaReference';
import copyToClipboard from 'copy-to-clipboard';
import { parse, print } from 'graphql';
import { useEffect, useRef, useState } from 'react';
// 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
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

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

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

export const useEditorState = (
editor: 'query' | 'variable' | 'header',
): [string, (val: string) => void] => {
export const useEditorState = (editor: 'query' | 'variable' | 'header') => {
'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
const context = useEditorContext({
nonNull: true,
});
Expand All @@ -358,8 +358,14 @@ export const useEditorState = (
valueString = editorValue;
}

const handleEditorValue = (value: string) => editorInstance?.setValue(value);
return [valueString, handleEditorValue];
const handleEditorValue = useCallback(
(value: string) => editorInstance?.setValue(value),
[editorInstance],
);
return useMemo<[string, (val: string) => void]>(
() => [valueString, handleEditorValue],
[valueString, handleEditorValue],
);
};

/**
Expand Down

0 comments on commit 5e76a4f

Please sign in to comment.