-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathGraphiQL.tsx
More file actions
42 lines (38 loc) · 865 Bytes
/
Copy pathGraphiQL.tsx
File metadata and controls
42 lines (38 loc) · 865 Bytes
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
import { GraphiQL as GraphQLPlayground, GraphiQLProviderProps } from "graphiql";
import "graphiql/graphiql.css";
import { useState } from "react";
const defaultQuery = `
query MyQuery {
topStory(category: ALL) {
id
title
likeCount
createdAt
}
}
`;
const fetcher: GraphiQLProviderProps["fetcher"] = async (
graphQLParams,
options
) => {
const data = await fetch("/api", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
...options.headers,
},
body: JSON.stringify(graphQLParams),
});
return data.json().catch(() => data.text());
};
export default function GraphiQL() {
const [query, setQuery] = useState("");
return (
<GraphQLPlayground
query={query || defaultQuery.trim()}
onEditQuery={setQuery}
fetcher={fetcher}
/>
);
}