-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: monaco variables json, improved schema & worker loading & monac…
…o api (#1997) Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>
- Loading branch information
Showing
45 changed files
with
2,498 additions
and
880 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
'graphql-language-service-utils': minor | ||
'graphql-language-service': major | ||
'monaco-graphql': major | ||
--- | ||
|
||
This introduces some big changes to `monaco-graphql`, and some exciting features, including multi-model support, multi-schema support, and variables json language feature support 🎉. | ||
|
||
see [the readme](https://github.com/graphql/graphiql/tree/main/packages/monaco-graphql#monaco-graphql) to learn how to configure and use the new interface. | ||
|
||
## 🚨 BREAKING CHANGES!! 🚨 | ||
|
||
* `monaco-graphql` 🚨 **no longer loads schemas using `fetch` introspection** 🚨, you must specify the schema in one of many ways statically or dynamically. specifying just a schema `uri` no longer works. see [the readme](https://github.com/graphql/graphiql/tree/main/packages/monaco-graphql#monaco-graphql) | ||
* when specifying the language to an editor or model, **use `graphql` as the language id instead of `graphqlDev`** | ||
* the mode now extends the default basic language support from `monaco-editor` itself | ||
* when bundling, for syntax highlighting and basic language features, you must specify `graphql` in languages for your webpack or vite monaco plugins | ||
* The exported mode api for configfuration been entirely rewritten. It is simple for now, but we will add more powerful methods to the `monaco.languages.api` over time :) | ||
|
||
## New Features | ||
|
||
this introduces many improvements: | ||
- json language support, by mapping from each graphql model uri to a set of json variable model uris | ||
- we generate a json schema definition for the json variables on the fly | ||
- it updates alongside editor validation as you type | ||
- less redundant schema loading - schema is loaded in main process instead of in the webworker | ||
- web worker stability has been improved by contributors in previous patches, but removing remote schema loading vastly simplifies worker creation | ||
- the editor now supports multiple graphql models, configurable against multiple schema configurations | ||
* You can now use `intializeMode()` to initialize the language mode & worker with the schema, but you can still lazily load it, and fall back on default monaco editor basic languages support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import * as monaco from 'monaco-editor'; | ||
|
||
// NOTE: using loader syntax becuase Yaml worker imports editor.worker directly and that | ||
// import shouldn't go through loader syntax. | ||
// @ts-ignore | ||
import EditorWorker from 'worker-loader!monaco-editor/esm/vs/editor/editor.worker'; | ||
// @ts-ignore | ||
import JSONWorker from 'worker-loader!monaco-editor/esm/vs/language/json/json.worker'; | ||
// @ts-ignore | ||
import GraphQLWorker from 'worker-loader!monaco-graphql/esm/graphql.worker'; | ||
|
||
const GRAPHQL_LANGUAGE_ID = 'graphql'; | ||
|
||
// @ts-ignore | ||
window.MonacoEnvironment = { | ||
getWorker(_workerId: string, label: string) { | ||
if (label === GRAPHQL_LANGUAGE_ID) { | ||
return new GraphQLWorker(); | ||
} | ||
if (label === 'json') { | ||
return new JSONWorker(); | ||
} | ||
return new EditorWorker(); | ||
}, | ||
}; | ||
|
||
const operationString = | ||
localStorage.getItem('operations') ?? | ||
` | ||
# right click to view context menu | ||
# F1 for command palette | ||
# enjoy prettier formatting, autocompletion, | ||
# validation, hinting and more for GraphQL SDL and operations! | ||
query Example( | ||
$owner: String! | ||
$name: String! | ||
$reviewEvent: PullRequestReviewEvent! | ||
$user: FollowUserInput! | ||
) { | ||
repository(owner: $owner, name: $name) { | ||
stargazerCount | ||
} | ||
} | ||
`; | ||
|
||
const variablesString = | ||
localStorage.getItem('variables') ?? | ||
`{ | ||
"reviewEvent": "graphql", | ||
"name": true | ||
}`; | ||
|
||
const resultsString = `{}`; | ||
|
||
const THEME = 'vs-dark'; | ||
|
||
export function createEditors() { | ||
const variablesModel = monaco.editor.createModel( | ||
variablesString, | ||
'json', | ||
monaco.Uri.file('/1/variables.json'), | ||
); | ||
|
||
const variablesEditor = monaco.editor.create( | ||
document.getElementById('variables') as HTMLElement, | ||
{ | ||
model: variablesModel, | ||
language: 'json', | ||
formatOnPaste: true, | ||
formatOnType: true, | ||
theme: THEME, | ||
comments: { | ||
insertSpace: true, | ||
ignoreEmptyLines: true, | ||
}, | ||
}, | ||
); | ||
|
||
const operationModel = monaco.editor.createModel( | ||
operationString, | ||
GRAPHQL_LANGUAGE_ID, | ||
monaco.Uri.file('/1/operation.graphql'), | ||
); | ||
|
||
const operationEditor = monaco.editor.create( | ||
document.getElementById('operation') as HTMLElement, | ||
{ | ||
model: operationModel, | ||
formatOnPaste: true, | ||
formatOnType: true, | ||
folding: true, | ||
theme: THEME, | ||
language: GRAPHQL_LANGUAGE_ID, | ||
}, | ||
); | ||
|
||
const resultsModel = monaco.editor.createModel( | ||
resultsString, | ||
'json', | ||
monaco.Uri.file('/1/results.json'), | ||
); | ||
|
||
const resultsEditor = monaco.editor.create( | ||
document.getElementById('results') as HTMLElement, | ||
{ | ||
model: resultsModel, | ||
language: 'json', | ||
theme: THEME, | ||
wordWrap: 'on', | ||
readOnly: true, | ||
showFoldingControls: 'always', | ||
}, | ||
); | ||
|
||
return { | ||
operationEditor, | ||
variablesEditor, | ||
resultsEditor, | ||
operationModel, | ||
variablesModel, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
9df315b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://monaco-graphql.netlify.app as production
🚀 Deployed on https://61ad4379607f18a688e9ff48--monaco-graphql.netlify.app
9df315b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://graphiql-test.netlify.app as production
🚀 Deployed on https://61ad43b4920f5fe0a64dc960--graphiql-test.netlify.app