-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
editor.tsx
205 lines (192 loc) · 6.03 KB
/
editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useEffect } from 'react';
import { getIntrospectionQuery, IntrospectionQuery } from 'graphql';
import { Uri, editor, KeyMod, KeyCode, languages } from 'monaco-editor';
import { initializeMode } from 'monaco-graphql/esm/initializeMode';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
import * as JSONC from 'jsonc-parser';
/**
* Copied from graphql/graphiql/examples/monaco-graphql-vite
*/
function debounce<F extends (...args: any[]) => any>(duration: number, fn: F) {
let timeout: number | null;
return function (this: any, ...args: Parameters<F>) {
if (timeout) {
window.clearTimeout(timeout);
}
timeout = window.setTimeout(() => {
timeout = null;
fn(args);
}, duration);
};
}
const fetcher = createGraphiQLFetcher({
url: 'https://swapi-graphql.netlify.app/.netlify/functions/index/',
});
const defaultVariables =
localStorage.getItem('variables') ??
`
{
// limit will appear here as autocomplete,
// and because the default value is 0, will
// complete as such
}
`;
const getSchema = async () =>
fetcher({
query: getIntrospectionQuery(),
operationName: 'IntrospectionQuery',
});
const getOrCreateModel = (uri: string, value: string) => {
return (
editor.getModel(Uri.file(uri)) ??
editor.createModel(value, uri.split('.').pop(), Uri.file(uri))
);
};
const execOperation = async function () {
const variables = editor.getModel(Uri.file('variables.json'))!.getValue();
const operations = editor.getModel(Uri.file('operation.graphql'))!.getValue();
const resultsModel = editor.getModel(Uri.file('results.json'));
const result = await fetcher({
query: operations,
variables: JSON.stringify(JSONC.parse(variables)),
});
// TODO: this demo only supports a single iteration for http GET/POST,
// no multipart or subscriptions yet.
// @ts-expect-error
const data = await result.next();
resultsModel?.setValue(JSON.stringify(data.value, null, 2));
};
const queryAction = {
id: 'graphql-run',
label: 'Run Operation',
contextMenuOrder: 0,
contextMenuGroupId: 'graphql',
keybindings: [
// eslint-disable-next-line no-bitwise
KeyMod.CtrlCmd | KeyCode.Enter,
],
run: execOperation,
};
// set these early on so that initial variables with comments don't flash an error
languages.json.jsonDefaults.setDiagnosticsOptions({
allowComments: true,
trailingCommas: 'ignore',
});
const createEditor = (
ref: React.MutableRefObject<null>,
options: editor.IStandaloneEditorConstructionOptions,
) => editor.create(ref.current as unknown as HTMLElement, options);
export default function Editor() {
const opsRef = React.useRef(null);
const varsRef = React.useRef(null);
const resultsRef = React.useRef(null);
const [queryEditor, setQueryEditor] =
React.useState<editor.IStandaloneCodeEditor | null>(null);
const [variablesEditor, setVariablesEditor] =
React.useState<editor.IStandaloneCodeEditor | null>(null);
const [resultsViewer, setResultsViewer] =
React.useState<editor.IStandaloneCodeEditor | null>(null);
const [schema, setSchema] = React.useState<unknown | null>(null);
const [loading, setLoading] = React.useState(false);
/**
* Create the models & editors
*/
useEffect(() => {
const queryModel = getOrCreateModel('operation.graphql', '');
const variablesModel = getOrCreateModel('variables.json', defaultVariables);
const resultsModel = getOrCreateModel('results.json', '{}');
if (!queryEditor) {
setQueryEditor(
createEditor(opsRef, {
theme: 'vs-dark',
model: queryModel,
language: 'graphql',
}),
);
}
if (!variablesEditor) {
setVariablesEditor(
createEditor(varsRef, {
theme: 'vs-dark',
model: variablesModel,
}),
);
}
if (!resultsViewer) {
setResultsViewer(
createEditor(resultsRef, {
theme: 'vs-dark',
model: resultsModel,
readOnly: true,
smoothScrolling: true,
}),
);
}
queryModel.onDidChangeContent(
debounce(300, () => {
localStorage.setItem('operations', queryModel.getValue());
}),
);
variablesModel.onDidChangeContent(
debounce(300, () => {
localStorage.setItem('variables', variablesModel.getValue());
}),
);
// eslint-disable-next-line react-hooks/exhaustive-deps -- only run once on mount
}, []);
useEffect(() => {
queryEditor?.addAction(queryAction);
variablesEditor?.addAction(queryAction);
}, [queryEditor, variablesEditor]);
/**
* Handle the initial schema load
*/
useEffect(() => {
if (!schema && !loading) {
setLoading(true);
void getSchema()
.then(data => {
if (!('data' in data)) {
throw new Error(
'this demo does not support subscriptions or http multipart yet',
);
}
initializeMode({
diagnosticSettings: {
validateVariablesJSON: {
[Uri.file('operation.graphql').toString()]: [
Uri.file('variables.json').toString(),
],
},
jsonDiagnosticSettings: {
validate: true,
schemaValidation: 'error',
// set these again, because we are entirely re-setting them here
allowComments: true,
trailingCommas: 'ignore',
},
},
schemas: [
{
introspectionJSON: data.data as unknown as IntrospectionQuery,
uri: 'myschema.graphql',
},
],
});
setSchema(data.data);
})
.then(() => setLoading(false));
}
}, [schema, loading]);
return (
<div id="wrapper">
<div id="left-pane" className="pane">
<div ref={opsRef} className="editor" />
<div ref={varsRef} className="editor" />
</div>
<div id="right-pane" className="pane">
<div ref={resultsRef} className="editor" />
</div>
</div>
);
}