Skip to content

Commit

Permalink
clean up dead code and correctly pass identity from language client
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed Nov 12, 2018
1 parent 4e37b45 commit 338c073
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 335 deletions.
144 changes: 4 additions & 140 deletions packages/apollo-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ import {
ProposedFeatures,
TextDocuments,
FileChangeType,
NotificationType,
ServerCapabilities
} from "vscode-languageserver";
import { QuickPickItem } from "vscode";
import { GraphQLWorkspace } from "./workspace";
import { GraphQLLanguageProvider } from "./languageProvider";
import { LanguageServerLoadingHandler } from "./loadingHandler";

import { execute, DocumentNode } from "apollo-link";
import { createHttpLink } from "apollo-link-http";
import { fetch } from "apollo-env";
import { OperationDefinitionNode } from "graphql";

import { WebSocketLink } from "apollo-link-ws";
import { SubscriptionClient } from "subscriptions-transport-ws";

const flags = require("minimist")(process.argv.slice(2));

import * as ws from "ws";

const connection = createConnection(ProposedFeatures.all);

let hasWorkspaceFolderCapability = false;
Expand All @@ -32,9 +19,9 @@ const workspace = new GraphQLWorkspace(
new LanguageServerLoadingHandler(connection),
{
clientIdentity: {
name: flags["apollo-client-name"],
version: flags["apollo-client-version"],
referenceID: flags["apollo-client-reference-id"]
name: process.env["APOLLO_CLIENT_NAME"],
version: process.env["APOLLO_CLIENT_VERSION"],
referenceID: process.env["APOLLO_CLIENT_REFERENCE_ID"]
}
}
);
Expand Down Expand Up @@ -85,10 +72,7 @@ connection.onInitialize(async params => {
resolveProvider: false
},
executeCommandProvider: {
commands: [
"apollographql.runQuery",
"apollographql.runQueryWithVariables"
]
commands: []
},
textDocumentSync: documents.syncKind
} as ServerCapabilities
Expand Down Expand Up @@ -226,125 +210,5 @@ connection.onNotification(
(selection: QuickPickItem) => workspace.updateSchemaTag(selection)
);

const createSubscriptionLink = (endpoint: string) => {
const client = new SubscriptionClient(
endpoint,
{
reconnect: true
},
ws
);

return new WebSocketLink(client);
};

const cancellationFunctions: { [id: number]: () => void } = {};
let nextCancellationID = 1;

export const executeAndNotify = (
query: DocumentNode,
endpoint: string,
headers: any,
variables: any
) => {
const operation = query.definitions[0] as OperationDefinitionNode;
const link =
operation.operation === "subscription"
? createSubscriptionLink(endpoint)
: createHttpLink({ uri: endpoint, fetch } as any);

const cancellationID = nextCancellationID;
nextCancellationID++;

const sub = execute(link, {
query,
variables,
context: { headers }
}).subscribe(
value => {
connection.sendNotification(
new NotificationType<any, void>("apollographql/queryResult"),
{ result: value, cancellationID }
);
},
error => {
if (error.result) {
connection.sendNotification(
new NotificationType<any, void>("apollographql/queryResult"),
{ result: error.result, cancellationID }
);
} else {
connection.sendNotification(
new NotificationType<any, void>("apollographql/queryResult"),
{ result: { errors: [error] }, cancellationID }
);
}
}
);

connection.sendNotification(
new NotificationType<any, void>("apollographql/queryResult"),
{ result: "Loading...", cancellationID }
);

cancellationFunctions[cancellationID] = () => {
sub.unsubscribe();
};
};

const operationHasVariables = (operation: OperationDefinitionNode) => {
return (
operation.variableDefinitions && operation.variableDefinitions.length > 0
);
};

connection.onExecuteCommand(params => {
switch (params.command) {
case "apollographql.runQuery":
const operation = (params.arguments![0] as DocumentNode)
.definitions[0] as OperationDefinitionNode;
if (operationHasVariables(operation)) {
connection.sendNotification(
new NotificationType<any, void>("apollographql/requestVariables"),
{
query: params.arguments![0],
endpoint: params.arguments![1],
headers: params.arguments![2],
schema: params.arguments![3],
requestedVariables: operation.variableDefinitions!.map(v => {
return {
name: v.variable.name.value,
typeNode: v.type
};
})
}
);
} else {
executeAndNotify(
params.arguments![0],
params.arguments![1],
params.arguments![2],
{}
);
}

break;

default:
}
});

connection.onNotification(
"apollographql/runQueryWithVariables",
({ query, endpoint, headers, variables }) => {
executeAndNotify(query, endpoint, headers, variables);
}
);

connection.onNotification("apollographql/cancelQuery", ({ cancellationID }) => {
cancellationFunctions[cancellationID]();
delete cancellationFunctions[cancellationID];
});

// Listen on the connection
connection.listen();
Loading

0 comments on commit 338c073

Please sign in to comment.