|
| 1 | +import { dirname } from "path"; |
1 | 2 | import { |
2 | 3 | createConnection, |
3 | 4 | ProposedFeatures, |
4 | 5 | TextDocuments, |
5 | 6 | FileChangeType, |
6 | 7 | NotificationType |
7 | 8 | } from "vscode-languageserver"; |
8 | | - |
9 | | -import { GraphQLWorkspace } from "./workspace"; |
10 | | -import { GraphQLLanguageProvider } from "./languageProvider"; |
11 | | - |
12 | | -import { execute, DocumentNode } from "apollo-link"; |
13 | | -import { createHttpLink } from "apollo-link-http"; |
14 | | -import fetch from "node-fetch"; |
15 | | -import { OperationDefinitionNode } from "graphql"; |
16 | | - |
17 | | -import { WebSocketLink } from "apollo-link-ws"; |
18 | | -import { SubscriptionClient } from "subscriptions-transport-ws"; |
19 | | - |
20 | 9 | import Uri from "vscode-uri"; |
21 | | - |
22 | | -import * as ws from "ws"; |
23 | | - |
24 | | -import { dirname } from "path"; |
25 | 10 | import { findAndLoadConfig } from "apollo/lib/config"; |
| 11 | +import { GraphQLWorkspace } from "./workspace"; |
| 12 | +import { GraphQLLanguageProvider } from "./languageProvider"; |
26 | 13 |
|
27 | 14 | const connection = createConnection(ProposedFeatures.all); |
28 | 15 |
|
@@ -131,12 +118,6 @@ connection.onInitialize(async params => { |
131 | 118 | codeLensProvider: { |
132 | 119 | resolveProvider: false |
133 | 120 | }, |
134 | | - executeCommandProvider: { |
135 | | - commands: [ |
136 | | - "apollographql.runQuery", |
137 | | - "apollographql.runQueryWithVariables" |
138 | | - ] |
139 | | - }, |
140 | 121 | textDocumentSync: documents.syncKind |
141 | 122 | } |
142 | 123 | }; |
@@ -244,125 +225,4 @@ connection.onCodeLens((params, token) => { |
244 | 225 | return languageProvider.provideCodeLenses(params.textDocument.uri, token); |
245 | 226 | }); |
246 | 227 |
|
247 | | -const createSubscriptionLink = (endpoint: string) => { |
248 | | - const client = new SubscriptionClient( |
249 | | - endpoint, |
250 | | - { |
251 | | - reconnect: true |
252 | | - }, |
253 | | - ws |
254 | | - ); |
255 | | - |
256 | | - return new WebSocketLink(client); |
257 | | -}; |
258 | | - |
259 | | -const cancellationFunctions: { [id: number]: () => void } = {}; |
260 | | -let nextCancellationID = 1; |
261 | | - |
262 | | -export const executeAndNotify = ( |
263 | | - query: DocumentNode, |
264 | | - endpoint: string, |
265 | | - headers: any, |
266 | | - variables: any |
267 | | -) => { |
268 | | - const operation = query.definitions[0] as OperationDefinitionNode; |
269 | | - const link = |
270 | | - operation.operation === "subscription" |
271 | | - ? createSubscriptionLink(endpoint) |
272 | | - : createHttpLink({ uri: endpoint, fetch } as any); |
273 | | - |
274 | | - const cancellationID = nextCancellationID; |
275 | | - nextCancellationID++; |
276 | | - |
277 | | - const sub = execute(link, { |
278 | | - query, |
279 | | - variables, |
280 | | - context: { headers } |
281 | | - }).subscribe( |
282 | | - value => { |
283 | | - connection.sendNotification( |
284 | | - new NotificationType<any, void>("apollographql/queryResult"), |
285 | | - { result: value, cancellationID } |
286 | | - ); |
287 | | - }, |
288 | | - error => { |
289 | | - if (error.result) { |
290 | | - connection.sendNotification( |
291 | | - new NotificationType<any, void>("apollographql/queryResult"), |
292 | | - { result: error.result, cancellationID } |
293 | | - ); |
294 | | - } else { |
295 | | - connection.sendNotification( |
296 | | - new NotificationType<any, void>("apollographql/queryResult"), |
297 | | - { result: { errors: [error] }, cancellationID } |
298 | | - ); |
299 | | - } |
300 | | - } |
301 | | - ); |
302 | | - |
303 | | - connection.sendNotification( |
304 | | - new NotificationType<any, void>("apollographql/queryResult"), |
305 | | - { result: "Loading...", cancellationID } |
306 | | - ); |
307 | | - |
308 | | - cancellationFunctions[cancellationID] = () => { |
309 | | - sub.unsubscribe(); |
310 | | - }; |
311 | | -}; |
312 | | - |
313 | | -const operationHasVariables = (operation: OperationDefinitionNode) => { |
314 | | - return ( |
315 | | - operation.variableDefinitions && operation.variableDefinitions.length > 0 |
316 | | - ); |
317 | | -}; |
318 | | - |
319 | | -connection.onExecuteCommand(params => { |
320 | | - switch (params.command) { |
321 | | - case "apollographql.runQuery": |
322 | | - const operation = (params.arguments![0] as DocumentNode) |
323 | | - .definitions[0] as OperationDefinitionNode; |
324 | | - if (operationHasVariables(operation)) { |
325 | | - connection.sendNotification( |
326 | | - new NotificationType<any, void>("apollographql/requestVariables"), |
327 | | - { |
328 | | - query: params.arguments![0], |
329 | | - endpoint: params.arguments![1], |
330 | | - headers: params.arguments![2], |
331 | | - schema: params.arguments![3], |
332 | | - requestedVariables: operation.variableDefinitions!.map(v => { |
333 | | - return { |
334 | | - name: v.variable.name.value, |
335 | | - typeNode: v.type |
336 | | - }; |
337 | | - }) |
338 | | - } |
339 | | - ); |
340 | | - } else { |
341 | | - executeAndNotify( |
342 | | - params.arguments![0], |
343 | | - params.arguments![1], |
344 | | - params.arguments![2], |
345 | | - {} |
346 | | - ); |
347 | | - } |
348 | | - |
349 | | - break; |
350 | | - |
351 | | - default: |
352 | | - } |
353 | | -}); |
354 | | - |
355 | | -connection.onNotification( |
356 | | - "apollographql/runQueryWithVariables", |
357 | | - ({ query, endpoint, headers, variables }) => { |
358 | | - executeAndNotify(query, endpoint, headers, variables); |
359 | | - } |
360 | | -); |
361 | | - |
362 | | -connection.onNotification("apollographql/cancelQuery", ({ cancellationID }) => { |
363 | | - cancellationFunctions[cancellationID](); |
364 | | - delete cancellationFunctions[cancellationID]; |
365 | | -}); |
366 | | - |
367 | | -// Listen on the connection |
368 | 228 | connection.listen(); |
0 commit comments