|
1 | | -import React from 'react'; |
| 1 | +import 'regenerator-runtime/runtime.js'; |
| 2 | + |
| 3 | +import * as React from 'react'; |
2 | 4 | import { render } from 'react-dom'; |
3 | 5 | import GraphiQL from 'graphiql'; |
| 6 | +import { useExplorerPlugin } from '@graphiql/plugin-explorer'; |
| 7 | +import { useExporterPlugin } from '@graphiql/plugin-code-exporter'; |
| 8 | + |
4 | 9 | import 'graphiql/graphiql.css'; |
| 10 | +import '@graphiql/plugin-explorer/dist/style.css'; |
| 11 | +import '@graphiql/plugin-code-exporter/dist/style.css'; |
| 12 | + |
| 13 | +const removeQueryName = query => |
| 14 | + query.replace( |
| 15 | + /^[^{(]+([{(])/, |
| 16 | + (_match, openingCurlyBracketsOrParenthesis) => |
| 17 | + `query ${openingCurlyBracketsOrParenthesis}`, |
| 18 | + ); |
| 19 | + |
| 20 | +const getQuery = (arg, spaceCount) => { |
| 21 | + const { operationDataList } = arg; |
| 22 | + const { query } = operationDataList[0]; |
| 23 | + const anonymousQuery = removeQueryName(query); |
| 24 | + return ( |
| 25 | + ` `.repeat(spaceCount) + |
| 26 | + anonymousQuery.replace(/\n/g, `\n` + ` `.repeat(spaceCount)) |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +const exampleSnippetOne = { |
| 31 | + name: `Example One`, |
| 32 | + language: `JavaScript`, |
| 33 | + codeMirrorMode: `jsx`, |
| 34 | + options: [], |
| 35 | + generate: arg => `export const query = graphql\` |
| 36 | +${getQuery(arg, 2)} |
| 37 | +\` |
| 38 | +`, |
| 39 | +}; |
| 40 | + |
| 41 | +const exampleSnippetTwo = { |
| 42 | + name: `Example Two`, |
| 43 | + language: `JavaScript`, |
| 44 | + codeMirrorMode: `jsx`, |
| 45 | + options: [], |
| 46 | + generate: arg => `import { graphql } from 'graphql' |
| 47 | +
|
| 48 | +export const query = graphql\` |
| 49 | +${getQuery(arg, 2)} |
| 50 | +\` |
| 51 | +`, |
| 52 | +}; |
| 53 | + |
| 54 | +const snippets = [exampleSnippetOne, exampleSnippetTwo]; |
| 55 | + |
| 56 | +const App = () => { |
| 57 | + const [query, setQuery] = React.useState(''); |
| 58 | + const explorerPlugin = useExplorerPlugin({ |
| 59 | + query, |
| 60 | + onEdit: setQuery, |
| 61 | + }); |
| 62 | + const exporterPlugin = useExporterPlugin({ |
| 63 | + query, |
| 64 | + snippets, |
| 65 | + }); |
5 | 66 |
|
6 | | -const Logo = () => <span>My Corp</span>; |
7 | | - |
8 | | -// See GraphiQL Readme - Advanced Usage section for more examples like this |
9 | | -GraphiQL.Logo = Logo; |
10 | | - |
11 | | -const App = () => ( |
12 | | - <GraphiQL |
13 | | - style={{ height: '100vh' }} |
14 | | - headerEditorEnabled |
15 | | - fetcher={async (graphQLParams, options) => { |
16 | | - const data = await fetch( |
17 | | - 'https://swapi-graphql.netlify.app/.netlify/functions/index', |
18 | | - { |
19 | | - method: 'POST', |
20 | | - headers: { |
21 | | - Accept: 'application/json', |
22 | | - 'Content-Type': 'application/json', |
23 | | - ...options.headers, |
| 67 | + return ( |
| 68 | + <GraphiQL |
| 69 | + style={{ height: '100vh' }} |
| 70 | + query={query} |
| 71 | + onEditQuery={setQuery} |
| 72 | + plugins={[explorerPlugin, exporterPlugin]} |
| 73 | + fetcher={async (graphQLParams, options) => { |
| 74 | + const data = await fetch( |
| 75 | + 'https://swapi-graphql.netlify.app/.netlify/functions/index', |
| 76 | + { |
| 77 | + method: 'POST', |
| 78 | + headers: { |
| 79 | + Accept: 'application/json', |
| 80 | + 'Content-Type': 'application/json', |
| 81 | + ...options.headers, |
| 82 | + }, |
| 83 | + body: JSON.stringify(graphQLParams), |
| 84 | + credentials: 'same-origin', |
24 | 85 | }, |
25 | | - body: JSON.stringify(graphQLParams), |
26 | | - credentials: 'same-origin', |
27 | | - }, |
28 | | - ); |
29 | | - return data.json().catch(() => data.text()); |
30 | | - }} |
31 | | - /> |
32 | | -); |
| 86 | + ); |
| 87 | + return data.json().catch(() => data.text()); |
| 88 | + }} |
| 89 | + /> |
| 90 | + ); |
| 91 | +}; |
33 | 92 |
|
34 | 93 | render(<App />, document.getElementById('root')); |
0 commit comments