|
19 | 19 | } |
20 | 20 | </style> |
21 | 21 |
|
22 | | - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphiql@2.2.0/graphiql.min.css" /> |
23 | | - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@graphiql/plugin-explorer@0.1.15/dist/style.css" /> |
24 | | - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@graphiql/plugin-code-exporter@0.1.2/dist/style.css" /> |
| 22 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphiql@3.0.6/graphiql.min.css" /> |
25 | 23 | </head> |
26 | 24 |
|
27 | 25 | <body> |
|
33 | 31 | <script src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.development.js" |
34 | 32 | integrity="sha512-Wr9OKCTtq1anK0hq5bY3X/AvDI5EflDSAh0mE9gma+4hl+kXdTJPKZ3TwLMBcrgUeoY0s3dq9JjhCQc7vddtFg==" |
35 | 33 | crossorigin="anonymous"></script> |
36 | | -<script src="https://cdn.jsdelivr.net/npm/graphiql@2.2.0/graphiql.min.js" |
37 | | - integrity="sha512-FVCV2//UVo1qJ3Kg6kkHLe0Hg+IJhjrGa+aYHh8xD4KmwbbjthIzvaAcCJsQgA43+k+6u7HqORKXMyMt82Srfw==" |
38 | | - crossorigin="anonymous"></script> |
39 | | -<script src="https://cdn.jsdelivr.net/npm/@graphiql/plugin-explorer@0.1.15/dist/graphiql-plugin-explorer.umd.js" |
40 | | - integrity="sha512-VQtND1w103C6dmo8VAhbV2DlkFdXKtWpr2nVuYEdj2dFB0UIMYeSbpKDLAD7n+UP+b0Eg79Gn40Onju687384A==" |
41 | | - crossorigin="anonymous"></script> |
42 | | -<script src="https://cdn.jsdelivr.net/npm/@graphiql/plugin-code-exporter@0.1.2/dist/graphiql-plugin-code-exporter.umd.js" |
43 | | - integrity="sha512-bZesuT5p2QZ6cSlpgPxI83Db5G3Bw35RTKz+Z+kc6RUu4/PtPkNddzax0VWA6VoXvIwT8s4i5nQklZ+AGJQD2Q==" |
| 34 | +<script src="https://cdn.jsdelivr.net/npm/graphiql@3.0.6/graphiql.min.js" |
| 35 | + integrity="sha512-J1qOEDzw11Yb9g9NS8iyR/Gq92y7k3fGnIxFBvQd9VRuS0iRbDkZo6S+k0SK+3UjQotW35LP/jSXgJfu2LsjHA==" |
44 | 36 | crossorigin="anonymous"></script> |
45 | 37 |
|
46 | 38 | <script> |
|
52 | 44 | subscriptionUrl |
53 | 45 | }); |
54 | 46 |
|
55 | | - var fetchSnippet = { |
56 | | - language: 'JavaScript', |
57 | | - name: 'Fetch', |
58 | | - codeMirrorMode: 'jsx', |
59 | | - options: [], |
60 | | - generate: (generateOptions) => { |
61 | | - const operation = generateOptions.operationDataList[0]; |
62 | | - const context = generateOptions.context; |
63 | | - const headers = context.headers.length !== 0 ? `headers: ${context.headers},\n ` : ''; |
64 | | - const variables = JSON.stringify(operation.variables); |
65 | | - return ` |
66 | | -const res = await fetch("${context.serverUrl}", { |
67 | | - method: 'POST', |
68 | | - ${headers}body: JSON.stringify({ |
69 | | - operationName: "${operation.name}", |
70 | | - query: \`${operation.query.replaceAll("\n", "\\n")}\`, |
71 | | - variables: ${variables} |
72 | | - }), |
73 | | -}); |
74 | | -
|
75 | | -const { errors, data } = await res.json(); |
76 | | -
|
77 | | -// Do something with the response |
78 | | -console.log(data, errors); |
79 | | -`; |
80 | | - } |
81 | | - }; |
82 | | - |
83 | | - var curlSnippet = { |
84 | | - language: 'Bash', |
85 | | - name: 'Curl', |
86 | | - codeMirrorMode: 'jsx', |
87 | | - options: [], |
88 | | - generate: (generateOptions) => { |
89 | | - const operation = generateOptions.operationDataList[0]; |
90 | | - const context = generateOptions.context; |
91 | | - let headersObject; |
92 | | - try { |
93 | | - headersObject = JSON.parse(context.headers); |
94 | | - } catch (e) { |
95 | | - headersObject = {}; |
96 | | - } |
97 | | - const headers = Object.entries(headersObject) |
98 | | - .reduce((acc, [headerName, headerValue]) => `${acc} -H '${headerName}: ${headerValue}' \\\n`, ''); |
99 | | - const payload = JSON.stringify({ |
100 | | - operationName: operation.name, |
101 | | - query: `${operation.query.replaceAll("\n", "\\n")}`, |
102 | | - variables: operation.variables |
103 | | - }) |
104 | | - return `curl '${context.serverUrl}' \\\n${headers}--data-raw $'${payload}' --compressed`; |
105 | | - } |
106 | | - } |
107 | | - |
108 | 47 | function GraphiQLWithPlugins() { |
109 | 48 | var [query, setQuery] = React.useState(''); |
110 | 49 | var [variables, setVariables] = React.useState(''); |
111 | 50 | var [headers, setHeaders] = React.useState(''); |
112 | 51 | var defaultHeaders = `{\n "content-type": "application/json"\n}`; |
113 | 52 |
|
114 | | - var explorerPlugin = GraphiQLPluginExplorer.useExplorerPlugin({ |
115 | | - query, |
116 | | - onEdit: setQuery, |
117 | | - }); |
118 | | - var exporterPlugin = GraphiQLPluginCodeExporter.useExporterPlugin({ |
119 | | - query, |
120 | | - variables, |
121 | | - context: { serverUrl, headers }, |
122 | | - snippets: [curlSnippet, fetchSnippet] |
123 | | - }); |
124 | | - |
125 | 53 | return React.createElement(GraphiQL, { |
126 | 54 | fetcher, |
127 | 55 | query, |
|
132 | 60 | onEditHeaders: setHeaders, |
133 | 61 | defaultHeaders, |
134 | 62 | defaultEditorToolsVisibility: true, |
135 | | - plugins: [explorerPlugin, exporterPlugin] |
| 63 | + plugins: [], |
| 64 | + shouldPersistHeaders: true |
136 | 65 | }); |
137 | 66 | } |
138 | 67 |
|
|
0 commit comments