|
| 1 | +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'; |
| 2 | + |
| 3 | +import 'regenerator-runtime/runtime'; |
| 4 | +import 'monaco-graphql/esm/monaco.contribution'; |
| 5 | + |
| 6 | +// NOTE: using loader syntax becuase Yaml worker imports editor.worker directly and that |
| 7 | +// import shouldn't go through loader syntax. |
| 8 | +// @ts-ignore |
| 9 | +import EditorWorker from 'worker-loader!monaco-editor/esm/vs/editor/editor.worker'; |
| 10 | +// @ts-ignore |
| 11 | +import JSONWorker from 'worker-loader!monaco-editor/esm/vs/language/json/json.worker'; |
| 12 | +// @ts-ignore |
| 13 | +import GraphQLWorker from 'worker-loader!monaco-graphql/esm/graphql.worker'; |
| 14 | + |
| 15 | +const SCHEMA_URL = 'https://swapi-graphql.netlify.com/.netlify/functions/index'; |
| 16 | + |
| 17 | +// @ts-ignore |
| 18 | +window.MonacoEnvironment = { |
| 19 | + getWorker(_workerId: string, label: string) { |
| 20 | + if (label === 'graphqlDev') { |
| 21 | + return new GraphQLWorker(); |
| 22 | + } |
| 23 | + if (label === 'json') { |
| 24 | + return new JSONWorker(); |
| 25 | + } |
| 26 | + return new EditorWorker(); |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +// const schemaInput = document.createElement('input'); |
| 31 | +// schemaInput.type = 'text' |
| 32 | + |
| 33 | +// // @ts-ignore |
| 34 | +// schemaInput.value = SCHEMA_URL |
| 35 | + |
| 36 | +// schemaInput.onchange = (e) => { |
| 37 | +// e.preventDefault() |
| 38 | +// console.log(e.target.value) |
| 39 | +// } |
| 40 | + |
| 41 | +// const toolbar = document.getElementById('toolbar') |
| 42 | +// toolbar?.appendChild(schemaInput) |
| 43 | + |
| 44 | +const variablesModel = monaco.editor.createModel( |
| 45 | + `{}`, |
| 46 | + 'json', |
| 47 | + monaco.Uri.file('/1/variables.json'), |
| 48 | +); |
| 49 | + |
| 50 | +const resultsEditor = monaco.editor.create( |
| 51 | + document.getElementById('results') as HTMLElement, |
| 52 | + { |
| 53 | + model: variablesModel, |
| 54 | + }, |
| 55 | +); |
| 56 | +const variablesEditor = monaco.editor.create( |
| 57 | + document.getElementById('variables') as HTMLElement, |
| 58 | + { |
| 59 | + value: `{ }`, |
| 60 | + language: 'json', |
| 61 | + }, |
| 62 | +); |
| 63 | +const model = monaco.editor.createModel( |
| 64 | + ` |
| 65 | +query Example { |
| 66 | + allFilms { |
| 67 | + films { |
| 68 | + id |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +`, |
| 73 | + 'graphqlDev', |
| 74 | + monaco.Uri.file('/1/operation.graphql'), |
| 75 | +); |
| 76 | + |
| 77 | +const operationEditor = monaco.editor.create( |
| 78 | + document.getElementById('operation') as HTMLElement, |
| 79 | + { |
| 80 | + model, |
| 81 | + }, |
| 82 | +); |
| 83 | + |
| 84 | +/** |
| 85 | + * Basic Operation Exec Example |
| 86 | + */ |
| 87 | + |
| 88 | +async function executeCurrentOp() { |
| 89 | + try { |
| 90 | + const operation = operationEditor.getValue(); |
| 91 | + const variables = variablesEditor.getValue(); |
| 92 | + const body: { variables?: string; query: string } = { query: operation }; |
| 93 | + const parsedVariables = JSON.parse(variables); |
| 94 | + if (parsedVariables && Object.keys(parsedVariables).length) { |
| 95 | + body.variables = variables; |
| 96 | + } |
| 97 | + const result = await fetch(SCHEMA_URL, { |
| 98 | + method: 'POST', |
| 99 | + headers: { 'content-type': 'application/json' }, |
| 100 | + body: JSON.stringify(body), |
| 101 | + }); |
| 102 | + const resultText = await result.text(); |
| 103 | + resultsEditor.setValue(JSON.stringify(JSON.parse(resultText), null, 2)); |
| 104 | + } catch (err) { |
| 105 | + resultsEditor.setValue(err.toString()); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const opAction: monaco.editor.IActionDescriptor = { |
| 110 | + id: 'graphql-run', |
| 111 | + label: 'Run Operation', |
| 112 | + contextMenuOrder: 0, |
| 113 | + contextMenuGroupId: 'graphql', |
| 114 | + keybindings: [ |
| 115 | + // eslint-disable-next-line no-bitwise |
| 116 | + monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, |
| 117 | + ], |
| 118 | + run: executeCurrentOp, |
| 119 | +}; |
| 120 | + |
| 121 | +operationEditor.addAction(opAction); |
| 122 | +variablesEditor.addAction(opAction); |
| 123 | +resultsEditor.addAction(opAction); |
| 124 | + |
| 125 | +// add your own diagnostics? why not! |
| 126 | +// monaco.editor.setModelMarkers( |
| 127 | +// model, |
| 128 | +// 'graphql', |
| 129 | +// [{ |
| 130 | +// severity: 5, |
| 131 | +// message: 'An example diagnostic error', |
| 132 | +// startColumn: 2, |
| 133 | +// startLineNumber: 4, |
| 134 | +// endLineNumber: 4, |
| 135 | +// endColumn: 0, |
| 136 | +// }], |
| 137 | +// ); |
0 commit comments