1
- import React , { useMemo } from 'react'
2
- import copyToClipboard from 'copy-to-clipboard '
1
+ import React , { useMemo , useState } from 'react'
2
+ import { useExplorerPlugin } from '@graphiql/plugin-explorer '
3
3
import {
4
4
GraphiQL ,
5
- Fetcher ,
5
+ GraphiQLInterface ,
6
6
GraphiQLProps ,
7
- FetcherParams ,
8
- FetcherOpts ,
7
+ GraphiQLProvider ,
9
8
} from 'graphiql'
9
+ import { Fetcher , FetcherParams , FetcherOpts } from '@graphiql/toolkit'
10
10
import {
11
11
LoadFromUrlOptions ,
12
12
SubscriptionProtocol ,
13
13
UrlLoader ,
14
14
} from '@graphql-tools/url-loader'
15
- import { DocumentNode , GraphQLSchema , Kind , parse } from 'graphql '
16
- import GraphiQLExplorer from 'graphiql-explorer '
15
+ import { useUrlSearchParams } from 'use-url-search-params '
16
+ import { DocumentNode , Kind , parse } from 'graphql '
17
17
import 'graphiql/graphiql.css'
18
+ import '@graphiql/plugin-explorer/dist/style.css'
18
19
import './styles.css'
19
- import './dark-mode.css'
20
- import { YogaLogo } from './YogaLogo'
21
- import { useUrlSearchParams } from 'use-url-search-params'
22
20
import 'json-bigint-patch'
21
+ import { YogaLogo } from './YogaLogo'
23
22
24
23
const getOperationWithFragments = (
25
24
document : DocumentNode ,
@@ -44,18 +43,14 @@ const getOperationWithFragments = (
44
43
45
44
export type YogaGraphiQLProps = Omit <
46
45
GraphiQLProps ,
47
- | 'ref'
48
46
| 'fetcher'
49
- | 'headerEditorEnabled'
50
- | 'defaultVariableEditorOpen'
51
- | 'docExplorerOpen'
47
+ | 'isHeadersEditorEnabled'
48
+ | 'defaultEditorToolsVisibility'
52
49
| 'onToggleDocs'
53
- | 'tabs'
54
50
| 'toolbar'
55
51
| 'onSchemaChange'
56
52
| 'query'
57
53
| 'onEditQuery'
58
- | 'beforeTopBarContent'
59
54
> &
60
55
Partial < Omit < LoadFromUrlOptions , 'headers' > > & {
61
56
title ?: string
@@ -67,44 +62,48 @@ export type YogaGraphiQLProps = Omit<
67
62
68
63
export function YogaGraphiQL ( props : YogaGraphiQLProps ) : React . ReactElement {
69
64
const initialQuery = /* GraphQL */ `#
70
- # Welcome to ${ props . title || 'Yoga GraphiQL' }
71
- #
72
- # ${
65
+ # Welcome to ${ props . title || 'Yoga GraphiQL' }
66
+ #
67
+ # ${
73
68
props . title || 'Yoga GraphiQL'
74
69
} is an in-browser tool for writing, validating, and
75
- # testing GraphQL queries.
76
- #
77
- # Type queries into this side of the screen, and you will see intelligent
78
- # typeaheads aware of the current GraphQL type schema and live syntax and
79
- # validation errors highlighted within the text.
80
- #
81
- # GraphQL queries typically start with a "{" character. Lines that start
82
- # with a # are ignored.
83
- #
84
- # An example GraphQL query might look like:
85
- #
86
- # {
87
- # field(arg: "value") {
88
- # subField
89
- # }
90
- # }
91
- #
92
- # Keyboard shortcuts:
93
- #
94
- # Prettify Query: Shift-Ctrl-P (or press the prettify button above)
95
- #
96
- # Merge Query: Shift-Ctrl-M (or press the merge button above)
97
- #
98
- # Run Query: Ctrl-Enter (or press the play button above)
99
- #
100
- # Auto Complete: Ctrl-Space (or just start typing)
101
- #
102
- `
70
+ # testing GraphQL queries.
71
+ #
72
+ # Type queries into this side of the screen, and you will see intelligent
73
+ # typeaheads aware of the current GraphQL type schema and live syntax and
74
+ # validation errors highlighted within the text.
75
+ #
76
+ # GraphQL queries typically start with a "{" character. Lines that start
77
+ # with a # are ignored.
78
+ #
79
+ # An example GraphQL query might look like:
80
+ #
81
+ # {
82
+ # field(arg: "value") {
83
+ # subField
84
+ # }
85
+ # }
86
+ #
87
+ # Keyboard shortcuts:
88
+ #
89
+ # Prettify Query: Shift-Ctrl-P (or press the prettify button above)
90
+ #
91
+ # Merge Query: Shift-Ctrl-M (or press the merge button above)
92
+ #
93
+ # Run Query: Ctrl-Enter (or press the play button above)
94
+ #
95
+ # Auto Complete: Ctrl-Space (or just start typing)
96
+ #
97
+ `
98
+
103
99
const endpoint = new URL (
104
100
props . endpoint ?? location . pathname ,
105
101
location . href ,
106
102
) . toString ( )
107
- const graphiqlRef = React . useRef < GraphiQL | null > ( null )
103
+
104
+ const type = {
105
+ query : String ,
106
+ }
108
107
109
108
const urlLoader = useMemo ( ( ) => new UrlLoader ( ) , [ ] )
110
109
@@ -134,102 +133,35 @@ export function YogaGraphiQL(props: YogaGraphiQLProps): React.ReactElement {
134
133
}
135
134
} , [ urlLoader , endpoint ] )
136
135
137
- const [ showExplorer , setShowExplorer ] = React . useState ( false )
138
- const [ schema , setSchema ] = React . useState < GraphQLSchema | null > ( null )
139
- const types = {
140
- query : String ,
141
- }
142
136
const [ params , setParams ] = useUrlSearchParams (
143
137
{
144
138
query : props . defaultQuery || initialQuery ,
145
139
} ,
146
- types ,
140
+ type ,
147
141
false ,
148
142
)
149
- const [ showDocs , setShowDocs ] = React . useState ( false )
143
+
144
+ const [ query , setQuery ] = useState ( params . query ?. toString ( ) )
145
+ const explorerPlugin = useExplorerPlugin ( {
146
+ query,
147
+ onEdit : setQuery ,
148
+ } )
150
149
151
150
return (
152
151
< div className = "graphiql-container" >
153
- { schema && params ?. query != null ? (
154
- < GraphiQLExplorer
155
- schema = { schema }
156
- query = { params . query . toString ( ) }
157
- onEdit = { ( query : string ) => {
158
- setParams ( {
159
- query,
160
- } )
161
- } }
162
- explorerIsOpen = { showExplorer }
163
- onToggleExplorer = { ( ) => setShowExplorer ( ( isOpen ) => ! isOpen ) }
164
- colors = { {
165
- keyword : '#ff6d5d' ,
166
- def : '#9f51d9' , // OperationName, FragmentName
167
- property : '#0083c7' , // FieldName
168
- qualifier : '#008aed' , // FieldAlias
169
- attribute : '#00b8ff' , // ArgumentName and ObjectFieldName
170
- number : '#97b1aa' , // type number
171
- string : '#00917d' , // type String
172
- string2 : '#146574' , // Enum
173
- builtin : '#da8aff' , // Boolean
174
- variable : '#ff6d5d' ,
175
- atom : '#ff9f4c' , // Type
176
- } }
177
- />
178
- ) : null }
179
- { params ?. query != null ? (
180
- < GraphiQL
181
- ref = { graphiqlRef }
182
- fetcher = { fetcher }
183
- headerEditorEnabled = { true }
184
- defaultVariableEditorOpen = { true }
185
- docExplorerOpen = { showDocs }
186
- onToggleDocs = { ( ) => setShowDocs ( ( isOpen ) => ! isOpen ) }
187
- tabs
188
- toolbar = { {
189
- additionalContent : (
190
- < >
191
- < button
192
- className = "toolbar-button"
193
- onClick = { ( ) => {
194
- const state = graphiqlRef . current ?. state
195
-
196
- copyToClipboard (
197
- urlLoader . prepareGETUrl ( {
198
- baseUrl : window . location . href ,
199
- query : state ?. query || '' ,
200
- variables : state ?. variables ,
201
- operationName : state ?. operationName ,
202
- } ) ,
203
- )
204
- } }
205
- >
206
- Copy Link
207
- </ button >
208
- </ >
209
- ) ,
210
- } }
211
- onSchemaChange = { ( schema ) => {
212
- setSchema ( schema )
213
- } }
214
- query = { params . query . toString ( ) }
152
+ < GraphiQLProvider
153
+ plugins = { [ explorerPlugin ] }
154
+ query = { query }
155
+ fetcher = { fetcher }
156
+ >
157
+ < GraphiQLInterface
158
+ isHeadersEditorEnabled
159
+ defaultEditorToolsVisibility
215
160
onEditQuery = { ( query ) =>
216
161
setParams ( {
217
162
query,
218
163
} )
219
164
}
220
- beforeTopBarContent = {
221
- schema ? (
222
- showExplorer ? null : (
223
- < button
224
- className = "docExplorerShow docExplorerShowReverse"
225
- onClick = { ( ) => setShowExplorer ( ( isOpen ) => ! isOpen ) }
226
- >
227
- Explorer
228
- </ button >
229
- )
230
- ) : null
231
- }
232
- { ...props }
233
165
>
234
166
< GraphiQL . Logo >
235
167
< div style = { { display : 'flex' , alignItems : 'center' } } >
@@ -249,8 +181,8 @@ export function YogaGraphiQL(props: YogaGraphiQLProps): React.ReactElement {
249
181
</ span >
250
182
</ div >
251
183
</ GraphiQL . Logo >
252
- </ GraphiQL >
253
- ) : null }
184
+ </ GraphiQLInterface >
185
+ </ GraphiQLProvider >
254
186
</ div >
255
187
)
256
188
}
0 commit comments