-
Notifications
You must be signed in to change notification settings - Fork 10
/
GraphiQL.tsx
293 lines (263 loc) · 7.85 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
import React from "react";
import { GraphiQL as DefaultGraphiQL } 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 { ToolbarButton } from "graphiql/dist/components/ToolbarButton";
import "./custom-graphiql.css";
import {
FetcherParams,
FetcherResult,
} from "graphiql/dist/components/GraphiQL";
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: "ws://localhost:4000/graphql",
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: "http://localhost:4000/graphql",
searchParams,
onNext: (value) => {
sink.next(JSON.parse(value));
},
onError: sink.error,
onComplete: sink.complete,
});
return () => subscription.unsubscribe();
});
}
const patches = await fetch("http://localhost:4000/graphql", {
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)
);
const defaultQuery = /* GraphQL */ `
#
# Simple ping query, nothing special here.
#
query PingQuery {
ping
}
#
# Simple ping mutation, nothing special here.
#
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
}
#
# Here things get interesting ;)
# Let's learn a bit about defer!
#
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
}
}
}
#
# There is also the @stream directive which is handy for lists that take a long time to retrieve.
#
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)
}
#
# This one is highly experimental and there is no RFC ongoing.
# The live directive specifies that the client wants to be notified in case any data the query selects has changed
# Check out https://github.com/n1ru4l/graphql-live-query for more information.
query LiveTestQuery @live {
# this field returns a list of greetings whose items are shuffled
greetings
}
`;
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}
/>
</>
),
}}
/>
</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>
);
};