-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathapp.jsx
190 lines (162 loc) · 4.8 KB
/
app.jsx
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
// eslint-disable-next-line no-unused-vars
import regeneratorRuntime from "regenerator-runtime"
import * as React from "react"
import { createRoot } from "react-dom/client"
import { GraphiQL } from "graphiql"
import { getIntrospectionQuery } from "graphql"
import { useExplorerPlugin } from "@graphiql/plugin-explorer"
import { useExporterPlugin } from "@graphiql/plugin-code-exporter"
import { snippets } from "./code-exporter/snippets.js"
import { Logo } from "./logo.jsx"
import { fetcher, fetchFragments, locationQuery } from "./utils.js"
import { RefreshDataSourceButton } from "./toolbar.jsx"
import {
FALLBACK_QUERY,
GRAPHIQL_URL,
LOCAL_STORAGE_NAMES,
REFRESH_URL,
} from "./constants.js"
import "graphiql/graphiql.css"
import "@graphiql/plugin-explorer/dist/style.css"
import "@graphiql/plugin-code-exporter/dist/style.css"
import "./app.css"
const parameters = {}
window.location.search
.slice(1)
.split(`&`)
.forEach(function (entry) {
const eq = entry.indexOf(`=`)
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
entry.slice(eq + 1)
)
}
})
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery
updateURL()
}
function onEditVariables(newVariables) {
parameters.variables = newVariables
updateURL()
}
function onEditHeaders(newHeaders) {
parameters.headers = newHeaders
updateURL()
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName
updateURL()
}
function onTabChange(tabsState) {
const activeTab = tabsState.tabs[tabsState.activeTabIndex]
parameters.query = activeTab.query
parameters.variables = activeTab.variables
parameters.headers = activeTab.headers
updateURL()
}
function updateURL() {
history.replaceState(null, null, locationQuery(parameters))
}
async function graphQLIntrospection() {
const res = await fetch(GRAPHIQL_URL, {
method: `POST`,
headers: {
Accept: `application/json`,
"Content-Type": `application/json`,
},
credentials: `include`,
body: JSON.stringify({ query: getIntrospectionQuery() }),
})
return res.json()
}
const DEFAULT_QUERY =
parameters.query ||
(window.localStorage &&
window.localStorage.getItem(LOCAL_STORAGE_NAMES.query)) ||
FALLBACK_QUERY
GraphiQL.Logo = Logo
const App = ({ initialExternalFragments }) => {
const [query, setQuery] = React.useState(DEFAULT_QUERY)
const [refreshState, setRefreshState] = React.useState({
enableRefresh: false,
refreshToken: null,
})
const [externalFragments, setExternalFragments] = React.useState(
initialExternalFragments
)
React.useEffect(() => {
const fetchData = async () => {
const result = await graphQLIntrospection()
let { enableRefresh, refreshToken } = result.extensions
switch (typeof enableRefresh) {
case `string`: {
const lowerCased = enableRefresh.toLowerCase()
enableRefresh = lowerCased === `1` || lowerCased === `true`
break
}
case `number`:
enableRefresh = enableRefresh > 0
break
}
setRefreshState({
enableRefresh,
refreshToken,
})
}
fetchData()
}, [])
const explorerPlugin = useExplorerPlugin({
query,
onEdit: setQuery,
})
const exporterPlugin = useExporterPlugin({
query,
snippets,
})
const refreshExternalDataSource = () => {
const options = { method: `POST` }
if (refreshState.refreshToken) {
options.headers = {
Authorization: refreshState.refreshToken,
}
}
fetchFragments().then(updatedExternalFragments => {
setExternalFragments(updatedExternalFragments)
})
return fetch(REFRESH_URL, options)
}
return (
<GraphiQL
fetcher={fetcher}
query={query}
variables={parameters.variables}
headers={parameters.headers}
onEditQuery={query => {
setQuery(query)
onEditQuery(query)
}}
onEditVariables={onEditVariables}
onEditOperationName={onEditOperationName}
onEditHeaders={onEditHeaders}
onTabChange={onTabChange}
toolbar={{
additionalContent: refreshState.enableRefresh && (
<RefreshDataSourceButton onClick={refreshExternalDataSource} />
),
}}
externalFragments={externalFragments}
plugins={[explorerPlugin, exporterPlugin]}
/>
)
}
const container = document.getElementById(`root`)
const root = createRoot(container)
// crude way to fetch fragments on boot time
// it won't hot reload fragments (refresh required)
// but good enough for initial integration
fetchFragments().then(initialExternalFragments => {
root.render(<App initialExternalFragments={initialExternalFragments} />)
})