Skip to content

Commit 662c143

Browse files
authored
feat(monaco-graphql): support monaco-editor 0.52 / render Markdown in completion popup descriptions (#3996)
* upd * upd * fix netlify and vitest * upd vitest
1 parent 60d5bbc commit 662c143

22 files changed

+206
-102
lines changed

.changeset/forty-laws-thank.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'monaco-graphql': minor
3+
---
4+
5+
- support monaco-editor 0.52
6+
- remove unnecessary conditions
7+
- render Markdown in completion popup descriptions

examples/monaco-graphql-webpack/babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ module.exports = {
77
plugins: [
88
require.resolve('@babel/plugin-proposal-class-properties'),
99
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
10+
require.resolve('@babel/plugin-transform-class-static-block'),
1011
],
1112
};

examples/monaco-graphql-webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@babel/core": "^7.21.0",
2323
"@babel/plugin-proposal-class-properties": "^7.18.6",
2424
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
25+
"@babel/plugin-transform-class-static-block": "^7.27.1",
2526
"@babel/preset-env": "^7.20.2",
2627
"@babel/preset-react": "^7.18.6",
2728
"@babel/preset-typescript": "^7.21.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"test:ci": "yarn build && jest --coverage && yarn vitest",
5959
"test:coverage": "yarn jest --coverage",
6060
"test:watch": "yarn jest --watch",
61-
"tsc": "tsc --build",
61+
"tsc": "tsc --build && node resources/patch-monaco-editor-type.mjs",
6262
"vitest": "yarn wsrun -p -m test",
6363
"wsrun:noexamples": "wsrun --exclude-missing --exclude example-monaco-graphql-react-vite --exclude example-monaco-graphql-nextjs --exclude example-monaco-graphql-webpack --exclude example-graphiql-webpack",
6464
"gen-agenda": "wgutils agenda gen"

packages/monaco-graphql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ import { GraphQLWorker } from 'monaco-graphql/esm/GraphQLWorker';
486486

487487
import { myValidationRules } from './custom';
488488

489-
self.onmessage = () => {
489+
globalThis.onmessage = () => {
490490
worker.initialize(
491491
(
492492
ctx: WorkerNamespace.IWorkerContext,

packages/monaco-graphql/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
},
6060
"files": [
6161
"dist",
62-
"esm",
63-
"src"
62+
"esm"
6463
],
6564
"scripts": {
6665
"types:check": "tsc --noEmit",
@@ -73,7 +72,7 @@
7372
"devDependencies": {
7473
"execa": "^7.1.1",
7574
"graphql": "^16.9.0",
76-
"monaco-editor": "^0.39.0",
75+
"monaco-editor": "^0.52.2",
7776
"prettier": "3.3.2",
7877
"vscode-languageserver-types": "^3.17.1"
7978
},

packages/monaco-graphql/src/GraphQLWorker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ export class GraphQLWorker {
111111
{ useMarkdownDescription: true },
112112
);
113113
if (jsonSchema) {
114-
jsonSchema.$id = 'monaco://variables-schema.json';
115-
jsonSchema.title = 'GraphQL Variables';
116-
return jsonSchema;
114+
return {
115+
...jsonSchema,
116+
$id: 'monaco://variables-schema.json',
117+
title: 'GraphQL Variables',
118+
};
117119
}
118120

119121
return null;

packages/monaco-graphql/src/LanguageService.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ export class LanguageService {
108108
* @returns {SchemaCacheItem | undefined}
109109
*/
110110
public getSchemaForFile(uri: string): SchemaCacheItem | undefined {
111-
if (!this._schemas?.length) {
111+
if (!this._schemas.length) {
112112
return;
113113
}
114114
if (this._schemas.length === 1) {
115-
return this._schemaCache.get(this._schemas[0].uri);
115+
return this._schemaCache.get(this._schemas[0]!.uri);
116116
}
117117
const schema = this._schemas.find(schemaConfig => {
118118
if (!schemaConfig.fileMatch) {
@@ -157,17 +157,15 @@ export class LanguageService {
157157
}
158158

159159
/**
160-
* override `schemas` config entirely
161-
* @param schema {schemaString}
160+
* Override `schemas` config entirely.
162161
*/
163162
public async updateSchemas(schemas: SchemaConfig[]): Promise<void> {
164163
this._schemas = schemas;
165164
this._cacheSchemas();
166165
}
167166

168167
/**
169-
* overwrite an existing schema config by Uri string
170-
* @param schema {schemaString}
168+
* Overwrite an existing schema config by Uri string.
171169
*/
172170
public updateSchema(schema: SchemaConfig): void {
173171
const schemaIndex = this._schemas.findIndex(c => c.uri === schema.uri);
@@ -184,8 +182,7 @@ export class LanguageService {
184182
}
185183

186184
/**
187-
* add a schema to the config
188-
* @param schema {schemaString}
185+
* Add a schema to the config.
189186
*/
190187
public addSchema(schema: SchemaConfig): void {
191188
this._schemas.push(schema);
@@ -213,7 +210,7 @@ export class LanguageService {
213210
position: IPosition,
214211
) => {
215212
const schema = this.getSchemaForFile(uri);
216-
if (!documentText || documentText.length < 1 || !schema?.schema) {
213+
if (!documentText || !schema?.schema) {
217214
return [];
218215
}
219216
return getAutocompleteSuggestions(
@@ -257,7 +254,7 @@ export class LanguageService {
257254
options?: HoverConfig,
258255
) => {
259256
const schema = this.getSchemaForFile(uri);
260-
if (schema && documentText?.length > 3) {
257+
if (schema && documentText.length > 3) {
261258
return getHoverInformation(
262259
schema.schema,
263260
documentText,
@@ -280,9 +277,12 @@ export class LanguageService {
280277
if (schema && documentText.length > 3) {
281278
try {
282279
const documentAST = this.parse(documentText);
283-
const operationFacts = getOperationASTFacts(documentAST, schema.schema);
284-
if (operationFacts?.variableToType) {
285-
return getVariablesJSONSchema(operationFacts.variableToType, {
280+
const { variableToType } = getOperationASTFacts(
281+
documentAST,
282+
schema.schema,
283+
);
284+
if (variableToType) {
285+
return getVariablesJSONSchema(variableToType, {
286286
...options,
287287
scalarSchemas: schema.customScalarSchemas,
288288
});

packages/monaco-graphql/src/api.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ export class MonacoGraphQLAPI {
5959
if (schemas) {
6060
this.setSchemaConfig(schemas);
6161
}
62-
this._modeConfiguration = modeConfiguration ?? modeConfigurationDefault;
63-
this._completionSettings = completionSettings ?? completionSettingDefault;
64-
this._diagnosticSettings = diagnosticSettings ?? diagnosticSettingDefault;
65-
this._formattingOptions = formattingOptions ?? formattingDefaults;
62+
this._modeConfiguration = modeConfiguration;
63+
this._completionSettings = completionSettings;
64+
this._diagnosticSettings = diagnosticSettings;
65+
this._formattingOptions = formattingOptions;
6666
}
6767

6868
public get onDidChange(): monaco.IEvent<MonacoGraphQLAPI> {
@@ -93,8 +93,8 @@ export class MonacoGraphQLAPI {
9393
return {
9494
...this._completionSettings,
9595
fillLeafsOnComplete:
96-
this._completionSettings?.__experimental__fillLeafsOnComplete ??
97-
this._completionSettings?.fillLeafsOnComplete,
96+
this._completionSettings.__experimental__fillLeafsOnComplete ??
97+
this._completionSettings.fillLeafsOnComplete,
9898
};
9999
}
100100
public get externalFragmentDefinitions() {
@@ -107,7 +107,7 @@ export class MonacoGraphQLAPI {
107107
* @param schemas {SchemaConfig[]}
108108
*/
109109
public setSchemaConfig(schemas: SchemaConfig[]): void {
110-
this._schemas = schemas || null;
110+
this._schemas = schemas;
111111
this._schemasById = schemas.reduce((result, schema) => {
112112
result[schema.uri] = schema;
113113
return result;
@@ -122,22 +122,22 @@ export class MonacoGraphQLAPI {
122122
}
123123

124124
public setModeConfiguration(modeConfiguration: ModeConfiguration): void {
125-
this._modeConfiguration = modeConfiguration || Object.create(null);
125+
this._modeConfiguration = modeConfiguration;
126126
this._onDidChange.fire(this);
127127
}
128128

129129
public setFormattingOptions(formattingOptions: FormattingOptions): void {
130-
this._formattingOptions = formattingOptions || Object.create(null);
130+
this._formattingOptions = formattingOptions;
131131
this._onDidChange.fire(this);
132132
}
133133

134134
public setDiagnosticSettings(diagnosticSettings: DiagnosticSettings): void {
135-
this._diagnosticSettings = diagnosticSettings || Object.create(null);
135+
this._diagnosticSettings = diagnosticSettings;
136136
this._onDidChange.fire(this);
137137
}
138138

139139
public setCompletionSettings(completionSettings: CompletionSettings): void {
140-
this._completionSettings = completionSettings || Object.create(null);
140+
this._completionSettings = completionSettings;
141141
this._onDidChange.fire(this);
142142
}
143143
}

packages/monaco-graphql/src/full.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'monaco-editor/esm/vs/editor/contrib/inlineCompletions/browser/inlineCompletions.contribution';
22
import 'monaco-editor/esm/vs/editor/contrib/format/browser/formatActions';
33
import 'monaco-editor/esm/vs/editor/contrib/bracketMatching/browser/bracketMatching';
4-
import 'monaco-editor/esm/vs/editor/contrib/hover/browser/hover';
54
import 'monaco-editor/esm/vs/editor/browser/coreCommands';
65
import 'monaco-editor/esm/vs/editor/contrib/clipboard/browser/clipboard';
76
import 'monaco-editor/esm/vs/editor/contrib/cursorUndo/browser/cursorUndo';

0 commit comments

Comments
 (0)