-
Notifications
You must be signed in to change notification settings - Fork 10
/
GraphiQL.tsx
377 lines (346 loc) · 10.7 KB
/
GraphiQL.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import React from "react";
import {
GraphiQL as DefaultGraphiQL,
FetcherParams,
FetcherResult,
} from "graphiql";
import "graphiql/graphiql.css";
import { createClient } from "graphql-ws";
import { meros } from "meros/browser";
import { Subscription as SSESubscription } from "sse-z";
import { io } from "socket.io-client";
import { isLiveQueryOperationDefinitionNode } from "@n1ru4l/graphql-live-query";
import { createSocketIOGraphQLClient } from "@n1ru4l/socket-io-graphql-client";
import {
makeAsyncIterableIteratorFromSink,
isAsyncIterable,
} from "@n1ru4l/push-pull-async-iterable-iterator";
import { parse, getOperationAST } from "graphql";
import type { GraphQLError } from "graphql";
import { ToolbarButton } from "graphiql/dist/components/ToolbarButton";
import "./custom-graphiql.css";
const ioClient = io("http://localhost:4001");
const ioGraphQLClient = createSocketIOGraphQLClient<FetcherResult>(ioClient);
const ioFetcher = (graphQLParams: FetcherParams) =>
ioGraphQLClient.execute({ ...graphQLParams, operation: graphQLParams.query });
const wsClient = createClient({
url: import.meta.env.VITE_WS_URL,
lazy: false,
});
const httpMultipartFetcher = async (
graphQLParams: FetcherParams
): Promise<any> => {
const abortController = new AbortController();
const parsedDocument = parse(graphQLParams.query);
const operationName = graphQLParams.operationName;
const documentNode = getOperationAST(parsedDocument, operationName);
if (
documentNode!.operation === "subscription" ||
isLiveQueryOperationDefinitionNode(documentNode!)
) {
const searchParams: Record<string, string> = {
operationName: graphQLParams.operationName,
query: graphQLParams.query,
};
if (graphQLParams.variables) {
searchParams.variables = JSON.stringify(graphQLParams.variables);
}
return makeAsyncIterableIteratorFromSink<FetcherResult>((sink) => {
const subscription = new SSESubscription({
url: import.meta.env.VITE_GRAPHQL_SERVER_URL,
searchParams,
onNext: (value) => {
sink.next(JSON.parse(value));
},
onError: sink.error,
onComplete: sink.complete,
});
return () => subscription.unsubscribe();
});
}
const patches = await fetch(import.meta.env.VITE_GRAPHQL_SERVER_URL, {
method: "POST",
body: JSON.stringify(graphQLParams),
headers: {
accept: "application/json, multipart/mixed",
"content-type": "application/json",
},
signal: abortController.signal,
}).then((r) => meros<FetcherResult>(r));
if (isAsyncIterable(patches)) {
return multiResponseParser(patches);
}
return patches.json();
};
async function* multiResponseParser<T>(
iterator: AsyncIterableIterator<{
body: T;
json: boolean;
}>
) {
for await (const { body, json } of iterator) {
if (!json) {
throw new Error("failed parsing part as json");
}
yield body;
}
}
const wsFetcher = (graphQLParams: FetcherParams) =>
makeAsyncIterableIteratorFromSink<any>((sink) =>
wsClient.subscribe(graphQLParams, {
...sink,
error: (err) => {
if (err instanceof Error) {
sink.error(err);
} else if (err instanceof CloseEvent) {
sink.error(
new Error(
`Socket closed with event ${err.code} ${err.reason || ""}`.trim()
)
);
} else {
sink.error(
new Error(
(err as GraphQLError[]).map(({ message }) => message).join(", ")
)
);
}
},
})
);
const defaultQuery = /* GraphQL */ `
#
# Query
#
# This is just a simple query - nothing to special here.
# But note that you can execute it via WebSocket, HTTP and Socket.io
#
query PingQuery {
ping
}
#
# Mutation
#
# This is just a simple query - nothing to special here.
# But note that you can execute it via WebSocket, HTTP and Socket.io
#
mutation PingMutation {
ping
}
#
# Subscription backed by a AsyncGenerator function
#
subscription CountTestSubscription {
# This subscription will emit 10 events over the time span of 10 seconds before completing.
count(to: 10)
}
#
# Subscription backed by Node.js EventEmitter
#
subscription RandomHashTestSubscription {
# a new value is published in 1 second intervals
randomHash
}
#
# Query using @defer
#
# defer can be used on fragments in order to defer sending a part of the result to the client,
# if it takes longer than the rest of the resolvers to yield an value.
#
# @defer is useful when a certain resolver on your backend is slow, but not mandatory for showing something meaningful to your users.
# An example for this would be a slow database call or third-party service.
#
query DeferTestQuery {
deferTest {
name
# The defer directive on fragments allows specifying that we are fine with
# getting the data requirement defined in that fragment later (if it is not available immediately)
... on GraphQLDeferTest @defer {
# this field has a sleep(5000) call,
# which means it will take a long time until it will be resolved
deferThisField
}
}
}
#
# Query using stream
#
# stream can be used on fields that return lists.
# The resolver on the backend uses an async generator function for yielding the values.
#
# This allows slow/huge lists of data to be streamed to the client. While data is still coming in the client can already show UI.
# Handy for feed like views.
#
query StreamTestQuery {
#
# With the initialCount argument we can specify the minimum required items that should be sent initially.
# the remaining items will then be streamed once they are ready to be sent over the wire.
streamTest @stream(initialCount: 2)
}
#
# Query using @live
#
# This one is highly experimental and there is no RFC ongoing.
# The live directive specifies that the client wants to always wants to have the latest up to date data streamed.
#
# The implementation tracks the resources a client consumes and re-executes the query operation once one of those got stale/invalidated.
# Check out https://github.com/n1ru4l/graphql-live-query for more information.
#
# This example returns a list that is mutated and invalidated each second.
#
query LiveTestQuery @live {
# this field returns a list of greetings whose items are shuffled
greetings
}
#
# OneOf input types
#
# OneOf input types allow polymorphic input fields. They are currently in the RFC stage. https://github.com/graphql/graphql-spec/pull/825
# The @envelop/extended-validation plugin allows using the feature today! https://github.com/dotansimha/envelop/tree/main/packages/plugins/extended-validation
#
# The input type LogEvent type is marked as a oneOf type. Therefore, the validation of the operation only passes if the input has either a stringEvent or booleanEvent key.
# Providing both or neither would result in a validation error.
#
# Let's provide a string as the input
mutation OneOfStringInputMutation {
logEvent(input: {
stringEvent: "hey"
})
}
# Let's provide a boolean as the input
mutation OneOfBooleanInputMutation {
logEvent(input: {
booleanEvent: true
})
}
# Uncomment and execute this query and you will encounter a validation error.
# mutation OneOfInvalidInputMutation {
# logEvent(input: {
# stringEvent: "hey"
# booleanEvent: true
# })
# }
`
.split(`\n`)
.slice(1)
.map((line) => line.replace(" ", ""))
.join(`\n`);
export const GraphiQL = () => {
const [activeTransportIndex, setActiveTransportIndex] = React.useState(0);
const fetcherOptions: ToolbarDropDownOption[] = React.useMemo(
() => [
{
value: "ws",
label: "GraphQL over WS",
title: "GraphQL over WS",
},
{
value: "http",
label: "GraphQL over HTTP",
title: "GraphQL over HTTP",
},
{
value: "Socket.io",
label: "GraphQL over Socket.io",
title: "GraphQL over Socket.io",
},
],
[]
);
const activeTransport = (
fetcherOptions[activeTransportIndex] ?? fetcherOptions[0]
).value;
const fetcher =
activeTransport === "ws"
? wsFetcher
: activeTransport === "http"
? httpMultipartFetcher
: ioFetcher;
return (
<div style={{ height: "100vh" }}>
<DefaultGraphiQL
defaultQuery={defaultQuery}
fetcher={fetcher}
toolbar={{
additionalContent: (
<>
<div className="toolbar-label">Transport</div>
<ToolbarDropDown
options={fetcherOptions}
activeOptionIndex={activeTransportIndex}
onSelectOption={setActiveTransportIndex}
/>
</>
),
}}
// ensure that the defaultQuery is always used by disabling storage
storage={{
getItem: () => null,
removeItem: () => undefined,
setItem: () => undefined,
length: 0,
}}
/>
</div>
);
};
type ToolbarDropDownOption = {
title: string;
label: string;
value: string;
};
const ToolbarDropDown = (props: {
options: ToolbarDropDownOption[];
activeOptionIndex: number;
placeholder?: string;
onSelectOption: (optionIndex: number) => void;
}): React.ReactElement => {
const [isOpen, setIsOpen] = React.useState(false);
const selectedOption = props.options[props.activeOptionIndex] ?? null;
return (
<div className="toolbar-drop-down">
<ToolbarButton
title={selectedOption?.title ?? props.placeholder ?? "Select value"}
label={selectedOption?.label ?? props.placeholder ?? "Select Value"}
onClick={() => {
setIsOpen((isOpen) => !isOpen);
}}
/>
{isOpen ? (
<ToolbarDropDownMenu>
{props.options.map((item, index) => (
<ToolbarDropDownMenuItem
key={item.value}
item={item}
isActive={index === props.activeOptionIndex}
onClick={() => {
props.onSelectOption(index);
setIsOpen(false);
}}
/>
))}
</ToolbarDropDownMenu>
) : null}
</div>
);
};
const ToolbarDropDownMenu = (props: {
children: React.ReactNode;
}): React.ReactElement => {
return <div className="toolbar-drop-down-menu">{props.children}</div>;
};
const ToolbarDropDownMenuItem = (props: {
item: ToolbarDropDownOption;
isActive: boolean;
onClick: () => void;
}): React.ReactElement => {
return (
<button
className="toolbar-drop-down-menu-item"
title={props.item.title}
onClick={props.onClick}
>
{props.item.label}
</button>
);
};