Skip to content

Commit c609d0f

Browse files
committed
Fix GraphQL schema getting nuked on codemirror language refresh
1 parent 7eb3f12 commit c609d0f

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

src-web/components/core/Editor/Editor.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import { settingsAtom } from '@yaakapp-internal/models';
1212
import type { EditorLanguage, TemplateFunction } from '@yaakapp-internal/plugins';
1313
import { parseTemplate } from '@yaakapp-internal/templates';
1414
import classNames from 'classnames';
15+
import type { GraphQLSchema } from 'graphql';
1516
import { useAtomValue } from 'jotai';
1617
import { md5 } from 'js-md5';
17-
import type { MutableRefObject, ReactNode } from 'react';
18+
import type { ReactNode, RefObject } from 'react';
1819
import {
1920
Children,
2021
cloneElement,
@@ -77,6 +78,7 @@ export interface EditorProps {
7778
hideGutter?: boolean;
7879
id?: string;
7980
language?: EditorLanguage | 'pairs' | 'url';
81+
graphQLSchema?: GraphQLSchema | null;
8082
onBlur?: () => void;
8183
onChange?: (value: string) => void;
8284
onFocus?: () => void;
@@ -115,6 +117,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
115117
format,
116118
heightMode,
117119
hideGutter,
120+
graphQLSchema,
118121
language,
119122
onBlur,
120123
onChange,
@@ -374,6 +377,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
374377
onClickVariable,
375378
onClickMissingVariable,
376379
onClickPathParameter,
380+
graphQLSchema: graphQLSchema ?? null,
377381
});
378382
view.dispatch({ effects: languageCompartment.reconfigure(ext) });
379383
}, [
@@ -386,6 +390,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
386390
onClickPathParameter,
387391
completionOptions,
388392
useTemplating,
393+
graphQLSchema,
389394
]);
390395

391396
// Initialize the editor when ref mounts
@@ -408,6 +413,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
408413
onClickVariable,
409414
onClickMissingVariable,
410415
onClickPathParameter,
416+
graphQLSchema: graphQLSchema ?? null,
411417
});
412418
const extensions = [
413419
languageCompartment.of(langExt),
@@ -595,12 +601,12 @@ function getExtensions({
595601
}: Pick<EditorProps, 'singleLine' | 'readOnly' | 'hideGutter'> & {
596602
stateKey: EditorProps['stateKey'];
597603
container: HTMLDivElement | null;
598-
onChange: MutableRefObject<EditorProps['onChange']>;
599-
onPaste: MutableRefObject<EditorProps['onPaste']>;
600-
onPasteOverwrite: MutableRefObject<EditorProps['onPasteOverwrite']>;
601-
onFocus: MutableRefObject<EditorProps['onFocus']>;
602-
onBlur: MutableRefObject<EditorProps['onBlur']>;
603-
onKeyDown: MutableRefObject<EditorProps['onKeyDown']>;
604+
onChange: RefObject<EditorProps['onChange']>;
605+
onPaste: RefObject<EditorProps['onPaste']>;
606+
onPasteOverwrite: RefObject<EditorProps['onPasteOverwrite']>;
607+
onFocus: RefObject<EditorProps['onFocus']>;
608+
onBlur: RefObject<EditorProps['onBlur']>;
609+
onKeyDown: RefObject<EditorProps['onKeyDown']>;
604610
}) {
605611
// TODO: Ensure tooltips render inside the dialog if we are in one.
606612
const parent =

src-web/components/core/Editor/extensions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
import { tags as t } from '@lezer/highlight';
3838
import type { EnvironmentVariable } from '@yaakapp-internal/models';
3939
import { graphql } from 'cm6-graphql';
40+
import type { GraphQLSchema } from 'graphql';
4041
import { activeRequestIdAtom } from '../../../hooks/useActiveRequestId';
4142
import { jotaiStore } from '../../../lib/jotai';
4243
import { renderMarkdown } from '../../../lib/markdown';
@@ -106,13 +107,15 @@ export function getLanguageExtension({
106107
onClickMissingVariable,
107108
onClickPathParameter,
108109
completionOptions,
110+
graphQLSchema,
109111
}: {
110112
useTemplating: boolean;
111113
environmentVariables: EnvironmentVariable[];
112114
onClickVariable: (option: EnvironmentVariable, tagValue: string, startPos: number) => void;
113115
onClickMissingVariable: (name: string, tagValue: string, startPos: number) => void;
114116
onClickPathParameter: (name: string) => void;
115117
completionOptions: TwigCompletionOption[];
118+
graphQLSchema: GraphQLSchema | null;
116119
} & Pick<EditorProps, 'language' | 'autocomplete'>) {
117120
const extraExtensions: Extension[] = [];
118121

@@ -128,7 +131,7 @@ export function getLanguageExtension({
128131
// GraphQL is a special exception
129132
if (language === 'graphql') {
130133
return [
131-
graphql(undefined, {
134+
graphql(graphQLSchema ?? undefined, {
132135
async onCompletionInfoRender(gqlCompletionItem): Promise<Node | null> {
133136
if (!gqlCompletionItem.documentation) return null;
134137
const innerHTML = await renderMarkdown(gqlCompletionItem.documentation);

src-web/components/graphql/GraphQLEditor.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { EditorView } from '@codemirror/view';
22
import type { HttpRequest } from '@yaakapp-internal/models';
3-
import { updateSchema } from 'cm6-graphql';
43

54
import { formatSdl } from 'format-graphql';
65
import { useAtom } from 'jotai';
7-
import { useEffect, useMemo, useRef } from 'react';
6+
import { useMemo, useRef } from 'react';
87
import { useLocalStorage } from 'react-use';
98
import { useIntrospectGraphQL } from '../../hooks/useIntrospectGraphQL';
109
import { useStateWithDeps } from '../../hooks/useStateWithDeps';
@@ -63,12 +62,6 @@ export function GraphQLEditor({ request, onChange, baseRequest, ...extraEditorPr
6362
onChange(newBody);
6463
};
6564

66-
// Refetch the schema when the URL changes
67-
useEffect(() => {
68-
if (editorViewRef.current == null) return;
69-
updateSchema(editorViewRef.current, schema ?? undefined);
70-
}, [schema]);
71-
7265
const actions = useMemo<EditorProps['actions']>(
7366
() => [
7467
<div key="actions" className="flex flex-row !opacity-100 !shadow">
@@ -201,6 +194,7 @@ export function GraphQLEditor({ request, onChange, baseRequest, ...extraEditorPr
201194
<Editor
202195
language="graphql"
203196
heightMode="auto"
197+
graphQLSchema={schema}
204198
format={formatSdl}
205199
defaultValue={currentBody.query}
206200
onChange={handleChangeQuery}

src-web/hooks/useFilterResponse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function useFilterResponse({
2121
filter,
2222
})) as FilterResponse;
2323

24+
if (result.error) {
25+
console.log("Failed to filter response:", result.error);
26+
}
27+
2428
return result;
2529
},
2630
});

0 commit comments

Comments
 (0)