@@ -12,69 +12,37 @@ import {
12
12
FragmentDefinitionNode ,
13
13
visit ,
14
14
DocumentNode ,
15
+ Source ,
15
16
} from 'graphql' ;
16
17
17
- import { default as picomatch } from 'picomatch' ;
18
+ import picomatch from 'picomatch-browser ' ;
18
19
19
- import type { IPosition } from 'graphql-language-service-types ' ;
20
+ import type { IPosition } from 'graphql-language-service' ;
20
21
import {
21
22
getAutocompleteSuggestions ,
22
23
getDiagnostics ,
23
24
getHoverInformation ,
24
25
HoverConfig ,
25
- } from 'graphql-language-service-interface ' ;
26
+ } from 'graphql-language-service' ;
26
27
27
28
import {
28
29
getVariablesJSONSchema ,
29
30
getOperationASTFacts ,
30
31
JSONSchemaOptions ,
31
32
} from 'graphql-language-service-utils' ;
32
33
33
- import {
34
- defaultSchemaLoader ,
35
- SchemaConfig ,
36
- SchemaLoader ,
37
- } from './schemaLoader' ;
34
+ import { defaultSchemaLoader } from './schemaLoader' ;
38
35
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' ;
72
37
73
38
type SchemaCacheItem = Omit < SchemaConfig , 'schema' > & { schema : GraphQLSchema } ;
74
39
75
40
type SchemaCache = Map < string , SchemaCacheItem > ;
76
41
const schemaCache : SchemaCache = new Map ( ) ;
77
42
43
+ /**
44
+ * Currently only used by the `monaco-graphql` worker
45
+ */
78
46
export class LanguageService {
79
47
private _parser : typeof parse = parse ;
80
48
private _schemas : SchemaConfig [ ] = [ ] ;
@@ -128,28 +96,36 @@ export class LanguageService {
128
96
schema,
129
97
} ) ;
130
98
}
99
+
131
100
/**
132
101
* Provide a model uri path, and see if a schema config has a `fileMatch` to match it
133
102
* @param uri {string}
134
103
* @returns {SchemaCacheItem | undefined }
135
104
*/
136
105
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
+ } ) ;
144
120
} ) ;
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 ) ;
150
128
}
151
- const cache = this . _cacheSchema ( schema ) ;
152
- return cache . get ( schema . uri ) ;
153
129
}
154
130
}
155
131
@@ -212,11 +188,11 @@ export class LanguageService {
212
188
}
213
189
/**
214
190
* Uses the configured parser
215
- * @param text
216
- * @param options
191
+ * @param text {string | Source}
192
+ * @param options {ParseOptions}
217
193
* @returns {DocumentNode }
218
194
*/
219
- public parse ( text : string , options ?: ParseOptions ) : DocumentNode {
195
+ public parse ( text : string | Source , options ?: ParseOptions ) : DocumentNode {
220
196
return this . _parser ( text , options || this . _parseOptions ) ;
221
197
}
222
198
/**
0 commit comments