Skip to content

Commit a44772d

Browse files
authored
fix: import LS only for monaco-graphql (#2103)
1 parent 56ac0a0 commit a44772d

File tree

14 files changed

+205
-169
lines changed

14 files changed

+205
-169
lines changed

.changeset/cuddly-moons-shave.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'graphql-language-service': patch
3+
'monaco-graphql': patch
4+
---
5+
6+
LangugeService should not be imported by `codemirror-graphql`, and thus `picomatch` should not be imported.

examples/monaco-graphql-webpack/src/schema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getIntrospectionQuery } from 'graphql';
2-
import type { SchemaConfig } from 'graphql-language-service';
2+
import type { SchemaConfig } from 'monaco-graphql/src/typings';
33
import { Uri } from 'monaco-editor';
44

55
const SCHEMA_URL = 'https://api.github.com/graphql';

packages/graphql-language-service/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
"graphql-language-service-interface": "^2.10.1",
3535
"graphql-language-service-parser": "^1.10.4",
3636
"graphql-language-service-types": "^1.8.7",
37-
"graphql-language-service-utils": "^2.7.1",
38-
"picomatch-browser": "^2.2.5"
37+
"graphql-language-service-utils": "^2.7.1"
3938
},
4039
"devDependencies": {
4140
"@types/picomatch": "^2.3.0",

packages/graphql-language-service/src/index.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@
1212
*
1313
* TODO: retire `graphql-language-service-{parser,interface,types,utils}` and merge with this workspace
1414
*/
15-
export type {
16-
SchemaConfig,
17-
BaseSchemaConfig,
18-
SchemaLoader,
19-
} from './schemaLoader';
20-
export { defaultSchemaLoader } from './schemaLoader';
21-
22-
export type { GraphQLLanguageConfig } from './LanguageService';
23-
export { LanguageService } from './LanguageService';
24-
2515
/**
2616
* A whole bunch of the key language services
2717
*/
@@ -43,6 +33,7 @@ export {
4333
GraphQLLanguageService,
4434
SEVERITY,
4535
Severity,
36+
HoverConfig,
4637
SeverityEnum,
4738
DIAGNOSTIC_SEVERITY,
4839
DefinitionQueryResult,

packages/graphql-language-service/src/schemaLoader.ts

-77
This file was deleted.

packages/monaco-graphql/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"main": "dist/monaco.contribution.js",
77
"module": "esm/monaco.contribution.js",
8-
"types": "dist/monaco.contribution.d.ts",
8+
"types": "esm/monaco.contribution.d.ts",
99
"contributors": [
1010
{
1111
"name": "Peng Lyu",
@@ -26,8 +26,9 @@
2626
"src"
2727
],
2828
"dependencies": {
29-
"graphql-language-service": "^4.1.2",
30-
"graphql-language-service-utils": "^2.7.1"
29+
"graphql-language-service": "^4.1.1",
30+
"graphql-language-service-utils": "^2.7.1",
31+
"picomatch-browser": "^2.2.5"
3132
},
3233
"devDependencies": {
3334
"graphql": "16.0.0-experimental-stream-defer.5",

packages/monaco-graphql/src/GraphQLWorker.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { FormattingOptions, ICreateData } from './typings';
8+
import { FormattingOptions, ICreateData, SchemaConfig } from './typings';
99

1010
import type { worker, Position } from 'monaco-editor';
1111

12-
import {
13-
getRange,
14-
LanguageService,
15-
SchemaConfig,
16-
} from 'graphql-language-service';
12+
import { getRange } from 'graphql-language-service';
13+
14+
import { LanguageService } from './LanguageService';
1715

1816
import {
1917
toGraphQLPosition,
@@ -27,7 +25,6 @@ export type MonacoCompletionItem = monaco.languages.CompletionItem & {
2725
isDeprecated?: boolean;
2826
deprecationReason?: string | null;
2927
};
30-
3128
export class GraphQLWorker {
3229
private _ctx: worker.IWorkerContext;
3330
private _languageService: LanguageService;
@@ -133,6 +130,7 @@ export class GraphQLWorker {
133130
...this._formattingOptions?.prettierConfig,
134131
});
135132
}
133+
136134
/**
137135
* TODO: store this in a proper document cache in the language service
138136
*/

packages/graphql-language-service/src/LanguageService.ts renamed to packages/monaco-graphql/src/LanguageService.ts

+34-58
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,37 @@ import {
1212
FragmentDefinitionNode,
1313
visit,
1414
DocumentNode,
15+
Source,
1516
} from 'graphql';
1617

17-
import { default as picomatch } from 'picomatch';
18+
import picomatch from 'picomatch-browser';
1819

19-
import type { IPosition } from 'graphql-language-service-types';
20+
import type { IPosition } from 'graphql-language-service';
2021
import {
2122
getAutocompleteSuggestions,
2223
getDiagnostics,
2324
getHoverInformation,
2425
HoverConfig,
25-
} from 'graphql-language-service-interface';
26+
} from 'graphql-language-service';
2627

2728
import {
2829
getVariablesJSONSchema,
2930
getOperationASTFacts,
3031
JSONSchemaOptions,
3132
} from 'graphql-language-service-utils';
3233

33-
import {
34-
defaultSchemaLoader,
35-
SchemaConfig,
36-
SchemaLoader,
37-
} from './schemaLoader';
34+
import { defaultSchemaLoader } from './schemaLoader';
3835

39-
/**
40-
* For the `monaco-graphql` language worker, these must be specified
41-
* in a custom webworker. see the readme.
42-
*/
43-
export type GraphQLLanguageConfig = {
44-
/**
45-
* Provide a parser that matches `graphql` `parse()` signature
46-
* Used for internal document parsing operations
47-
* for autocompletion and hover, `graphql-language-service-parser ` is used via `graphql-language-service-interface`
48-
*/
49-
parser?: typeof parse;
50-
/**
51-
* Custom options passed to `parse`, whether `graphql` parse by default or custom parser
52-
*/
53-
parseOptions?: ParseOptions;
54-
/**
55-
* Take a variety of schema inputs common for the language worker, and transform them
56-
* to at least a `schema` if not other easily available implementations
57-
*/
58-
schemaLoader?: SchemaLoader;
59-
/**
60-
* An array of schema configurations from which to match files for language features
61-
*/
62-
schemas?: SchemaConfig[];
63-
/**
64-
* External fragments to be used with completion and validation
65-
*/
66-
exteralFragmentDefinitions?: FragmentDefinitionNode[] | string;
67-
/**
68-
* Custom validation rules following `graphql` `ValidationRule` signature
69-
*/
70-
customValidationRules?: ValidationRule[];
71-
};
36+
import { SchemaConfig, SchemaLoader, GraphQLLanguageConfig } from './typings';
7237

7338
type SchemaCacheItem = Omit<SchemaConfig, 'schema'> & { schema: GraphQLSchema };
7439

7540
type SchemaCache = Map<string, SchemaCacheItem>;
7641
const schemaCache: SchemaCache = new Map();
7742

43+
/**
44+
* Currently only used by the `monaco-graphql` worker
45+
*/
7846
export class LanguageService {
7947
private _parser: typeof parse = parse;
8048
private _schemas: SchemaConfig[] = [];
@@ -128,28 +96,36 @@ export class LanguageService {
12896
schema,
12997
});
13098
}
99+
131100
/**
132101
* Provide a model uri path, and see if a schema config has a `fileMatch` to match it
133102
* @param uri {string}
134103
* @returns {SchemaCacheItem | undefined}
135104
*/
136105
public getSchemaForFile(uri: string): SchemaCacheItem | undefined {
137-
const schema = this._schemas.find(schemaConfig => {
138-
if (!schemaConfig.fileMatch) {
139-
return false;
140-
}
141-
return schemaConfig.fileMatch.some(glob => {
142-
const isMatch = picomatch(glob);
143-
return isMatch(uri);
106+
if (!this._schemas || !this._schemas.length) {
107+
return;
108+
}
109+
if (this._schemas.length === 1) {
110+
return this._schemaCache.get(this._schemas[0].uri);
111+
} else {
112+
const schema = this._schemas.find(schemaConfig => {
113+
if (!schemaConfig.fileMatch) {
114+
return false;
115+
}
116+
return schemaConfig.fileMatch.some(glob => {
117+
const isMatch = picomatch(glob);
118+
return isMatch(uri);
119+
});
144120
});
145-
});
146-
if (schema) {
147-
const cacheEntry = this._schemaCache.get(schema.uri);
148-
if (cacheEntry) {
149-
return cacheEntry;
121+
if (schema) {
122+
const cacheEntry = this._schemaCache.get(schema.uri);
123+
if (cacheEntry) {
124+
return cacheEntry;
125+
}
126+
const cache = this._cacheSchema(schema);
127+
return cache.get(schema.uri);
150128
}
151-
const cache = this._cacheSchema(schema);
152-
return cache.get(schema.uri);
153129
}
154130
}
155131

@@ -212,11 +188,11 @@ export class LanguageService {
212188
}
213189
/**
214190
* Uses the configured parser
215-
* @param text
216-
* @param options
191+
* @param text {string | Source}
192+
* @param options {ParseOptions}
217193
* @returns {DocumentNode}
218194
*/
219-
public parse(text: string, options?: ParseOptions): DocumentNode {
195+
public parse(text: string | Source, options?: ParseOptions): DocumentNode {
220196
return this._parser(text, options || this._parseOptions);
221197
}
222198
/**

packages/monaco-graphql/src/api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { SchemaConfig } from 'graphql-language-service';
98
import { Emitter } from 'monaco-editor';
109

1110
import type { IEvent } from 'monaco-editor';
@@ -15,6 +14,7 @@ import type {
1514
FormattingOptions,
1615
ModeConfiguration,
1716
MonacoGraphQLInitializeConfig,
17+
SchemaConfig,
1818
} from './typings';
1919

2020
export type MonacoGraphQLAPIOptions = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { buildClientSchema, buildASTSchema } from 'graphql';
2+
3+
import type { SchemaLoader } from './typings';
4+
5+
export const defaultSchemaLoader: SchemaLoader = (schemaConfig, parser) => {
6+
const {
7+
schema,
8+
documentAST,
9+
introspectionJSON,
10+
introspectionJSONString,
11+
buildSchemaOptions,
12+
documentString,
13+
} = schemaConfig;
14+
if (schema) {
15+
return schema;
16+
}
17+
if (introspectionJSONString) {
18+
const introspectionJSONResult = JSON.parse(introspectionJSONString);
19+
return buildClientSchema(introspectionJSONResult, buildSchemaOptions);
20+
}
21+
if (documentString && parser) {
22+
const docAST = parser(documentString);
23+
return buildASTSchema(docAST, buildSchemaOptions);
24+
}
25+
if (introspectionJSON) {
26+
return buildClientSchema(introspectionJSON, buildSchemaOptions);
27+
}
28+
if (documentAST) {
29+
return buildASTSchema(documentAST, buildSchemaOptions);
30+
}
31+
throw Error('no schema supplied');
32+
};

0 commit comments

Comments
 (0)