-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
GraphQLWorker.ts
180 lines (162 loc) · 4.74 KB
/
GraphQLWorker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright (c) 2021 GraphQL Contributors.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { FormattingOptions, ICreateData, SchemaConfig } from './typings';
import type { worker, Position } from 'monaco-editor';
import { getRange } from 'graphql-language-service';
import { LanguageService } from './LanguageService';
import {
toGraphQLPosition,
toMonacoRange,
toMarkerData,
toCompletion,
GraphQLWorkerCompletionItem,
} from './utils';
export type MonacoCompletionItem = monaco.languages.CompletionItem & {
isDeprecated?: boolean;
deprecationReason?: string | null;
};
export class GraphQLWorker {
private _ctx: worker.IWorkerContext;
private _languageService: LanguageService;
private _formattingOptions: FormattingOptions | undefined;
constructor(ctx: worker.IWorkerContext, createData: ICreateData) {
this._ctx = ctx;
this._languageService = new LanguageService(createData.languageConfig);
this._formattingOptions = createData.formattingOptions;
}
public async doValidation(uri: string) {
try {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!document) {
return [];
}
const graphqlDiagnostics = this._languageService.getDiagnostics(
uri,
document,
);
return graphqlDiagnostics.map(toMarkerData);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return [];
}
}
public async doComplete(
uri: string,
position: Position,
): Promise<GraphQLWorkerCompletionItem[]> {
try {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!document) {
return [];
}
const graphQLPosition = toGraphQLPosition(position);
const suggestions = this._languageService.getCompletion(
uri,
document,
graphQLPosition,
);
return suggestions.map(suggestion => toCompletion(suggestion));
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return [];
}
}
public async doHover(uri: string, position: Position) {
try {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!document) {
return null;
}
const graphQLPosition = toGraphQLPosition(position);
const hover = this._languageService.getHover(
uri,
document,
graphQLPosition,
);
return {
content: hover,
range: toMonacoRange(
getRange(
{
column: graphQLPosition.character,
line: graphQLPosition.line,
},
document,
),
),
};
} catch (err) {
// eslint-disable-next-line
console.error(err);
return null;
}
}
public async doGetVariablesJSONSchema(uri: string): Promise<unknown> {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!documentModel || !document) {
return null;
}
const jsonSchema = this._languageService.getVariablesJSONSchema(
uri,
document,
{ useMarkdownDescription: true },
);
if (jsonSchema) {
jsonSchema.$id = 'monaco://variables-schema.json';
jsonSchema.title = 'GraphQL Variables';
return jsonSchema;
}
return null;
}
async doFormat(uri: string): Promise<string | null> {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!documentModel || !document) {
return null;
}
const prettierStandalone = await import('prettier/standalone');
const prettierGraphqlParser = await import('prettier/parser-graphql');
return prettierStandalone.format(document, {
parser: 'graphql',
plugins: [prettierGraphqlParser],
...this._formattingOptions?.prettierConfig,
});
}
/**
* TODO: store this in a proper document cache in the language service
*/
private _getTextModel(uri: string): monaco.worker.IMirrorModel | null {
const models = this._ctx.getMirrorModels();
for (const model of models) {
if (model.uri.toString() === uri) {
return model;
}
}
return null;
}
public doUpdateSchema(schema: SchemaConfig) {
return this._languageService.updateSchema(schema);
}
public doUpdateSchemas(schemas: SchemaConfig[]) {
return this._languageService.updateSchemas(schemas);
}
}
export default {
GraphQLWorker,
};
export function create(
ctx: worker.IWorkerContext,
createData: ICreateData,
): GraphQLWorker {
return new GraphQLWorker(ctx, createData);
}