-
Notifications
You must be signed in to change notification settings - Fork 565
/
YogaGraphiQL.tsx
188 lines (177 loc) · 4.72 KB
/
YogaGraphiQL.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
import React, { useMemo, useState } from 'react'
import { useExplorerPlugin } from '@graphiql/plugin-explorer'
import {
GraphiQL,
GraphiQLInterface,
GraphiQLProps,
GraphiQLProvider,
} from 'graphiql'
import { Fetcher, FetcherParams, FetcherOpts } from '@graphiql/toolkit'
import {
LoadFromUrlOptions,
SubscriptionProtocol,
UrlLoader,
} from '@graphql-tools/url-loader'
import { useUrlSearchParams } from 'use-url-search-params'
import { DocumentNode, Kind, parse } from 'graphql'
import 'graphiql/graphiql.css'
import '@graphiql/plugin-explorer/dist/style.css'
import './styles.css'
import 'json-bigint-patch'
import { YogaLogo } from './YogaLogo'
const getOperationWithFragments = (
document: DocumentNode,
operationName?: string,
): DocumentNode => {
const definitions = document.definitions.filter((definition) => {
if (
definition.kind === Kind.OPERATION_DEFINITION &&
operationName &&
definition.name?.value !== operationName
) {
return false
}
return true
})
return {
kind: Kind.DOCUMENT,
definitions,
}
}
export type YogaGraphiQLProps = Omit<
GraphiQLProps,
| 'fetcher'
| 'isHeadersEditorEnabled'
| 'defaultEditorToolsVisibility'
| 'onToggleDocs'
| 'toolbar'
| 'onSchemaChange'
| 'query'
| 'onEditQuery'
> &
Partial<Omit<LoadFromUrlOptions, 'headers'>> & {
title?: string
/**
* Extra headers you always want to pass with users' headers input
*/
additionalHeaders?: LoadFromUrlOptions['headers']
}
export function YogaGraphiQL(props: YogaGraphiQLProps): React.ReactElement {
const initialQuery = /* GraphQL */ `#
# Welcome to ${props.title || 'Yoga GraphiQL'}
#
# ${
props.title || 'Yoga GraphiQL'
} is an in-browser tool for writing, validating, and
# testing GraphQL queries.
#
# Type queries into this side of the screen, and you will see intelligent
# typeaheads aware of the current GraphQL type schema and live syntax and
# validation errors highlighted within the text.
#
# GraphQL queries typically start with a "{" character. Lines that start
# with a # are ignored.
#
# An example GraphQL query might look like:
#
# {
# field(arg: "value") {
# subField
# }
# }
#
# Keyboard shortcuts:
#
# Prettify Query: Shift-Ctrl-P (or press the prettify button above)
#
# Merge Query: Shift-Ctrl-M (or press the merge button above)
#
# Run Query: Ctrl-Enter (or press the play button above)
#
# Auto Complete: Ctrl-Space (or just start typing)
#
`
const endpoint = new URL(
props.endpoint ?? location.pathname,
location.href,
).toString()
const type = {
query: String,
}
const urlLoader = useMemo(() => new UrlLoader(), [])
const fetcher: Fetcher = useMemo(() => {
const executor = urlLoader.getExecutorAsync(endpoint, {
subscriptionsProtocol: SubscriptionProtocol.SSE,
credentials: 'same-origin',
specifiedByUrl: true,
directiveIsRepeatable: true,
schemaDescription: true,
...props,
headers: props.additionalHeaders || {},
})
return function fetcher(graphQLParams: FetcherParams, opts?: FetcherOpts) {
const document = getOperationWithFragments(
parse(graphQLParams.query),
graphQLParams.operationName ?? undefined,
)
return executor({
document,
operationName: graphQLParams.operationName ?? undefined,
variables: graphQLParams.variables,
extensions: {
headers: opts?.headers,
},
})
}
}, [urlLoader, endpoint])
const [params, setParams] = useUrlSearchParams(
{
query: props.defaultQuery || initialQuery,
},
type,
false,
)
const [query, setQuery] = useState(params.query?.toString())
const explorerPlugin = useExplorerPlugin({
query,
onEdit: setQuery,
})
return (
<div className="graphiql-container">
<GraphiQLProvider
plugins={[explorerPlugin]}
query={query}
fetcher={fetcher}
>
<GraphiQLInterface
isHeadersEditorEnabled
defaultEditorToolsVisibility
onEditQuery={(query) =>
setParams({
query,
})
}
>
<GraphiQL.Logo>
<div style={{ display: 'flex', alignItems: 'center' }}>
<div style={{ width: 40, display: 'flex' }}>
<YogaLogo />
</div>
<span>
{props?.title ? (
props.title
) : (
<>
Yoga Graph
<em>i</em>
QL
</>
)}
</span>
</div>
</GraphiQL.Logo>
</GraphiQLInterface>
</GraphiQLProvider>
</div>
)
}