Skip to content

Commit

Permalink
Generate ‘run in explorer’ working
Browse files Browse the repository at this point in the history
  • Loading branch information
jgzuke committed Sep 16, 2021
1 parent 1255119 commit e9af3f6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@apollo/federation": "^0.23.1",
"@apollographql/graphql-language-service-interface": "^2.0.2",
"@endemolshinegroup/cosmiconfig-typescript-loader": "^1.0.0",
"@types/lz-string": "^1.3.34",
"@types/node-fetch": "2.5.10",
"apollo-datasource": "^0.8.0",
"apollo-link": "^1.2.3",
Expand All @@ -51,6 +52,7 @@
"graphql-tag": "^2.10.1",
"lodash.debounce": "^4.0.8",
"lodash.merge": "^4.6.1",
"lz-string": "^1.4.4",
"minimatch": "^3.0.4",
"moment": "^2.29.1",
"node-fetch": "^2.2.0",
Expand Down
36 changes: 32 additions & 4 deletions src/language-server/project/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import {
} from "graphql";
import { ValidationRule } from "graphql/validation/ValidationContext";
import { NotificationHandler, DiagnosticSeverity } from "vscode-languageserver";
import LZString from "lz-string";

import { rangeForASTNode } from "../utilities/source";
import { formatMS } from "../format";
import { LoadingHandler } from "../loadingHandler";
import { FileSet } from "../fileSet";
import { apolloClientSchemaDocument } from "./defaultClientSchema";

import {
Expand Down Expand Up @@ -360,14 +360,18 @@ export class GraphQLClientProject extends GraphQLProject {

for (const [uri, queryDocumentsForFile] of this.documentsByFile) {
for (const queryDocument of queryDocumentsForFile) {
if (queryDocument.ast && this.fieldLatencies) {
if (queryDocument.ast) {
const fieldLatencies = this.fieldLatencies;
const typeInfo = new TypeInfo(this.schema);
visit(
queryDocument.ast,
visitWithTypeInfo(typeInfo, {
enter: (node) => {
if (node.kind == "Field" && typeInfo.getParentType()) {
if (
node.kind == "Field" &&
typeInfo.getParentType() &&
fieldLatencies
) {
const parentName = typeInfo.getParentType()!.name;
const parentEngineStat = fieldLatencies.get(parentName);
const engineStat = parentEngineStat
Expand All @@ -381,11 +385,35 @@ export class GraphQLClientProject extends GraphQLProject {
});
}
} else if (node.kind == "OperationDefinition") {
const operationWithFragments =
this.getOperationWithFragments(node);
const document = operationWithFragments
.map(print)
.join("\n\n");
const explorerURLState =
LZString.compressToEncodedURIComponent(
JSON.stringify({ document })
);

const frontendUrlRoot =
"https://studio-staging.apollographql.com"; // TODO

const endpoint = this.config.service?.endpoint;
const variant = this.config.variant;
const graphId = this.config.graph;
this.config.client.service;

const runInExplorerLink = graphId
? `${frontendUrlRoot}/${graphId}/engine/explorer?variant=${variant}&explorerURLState=${explorerURLState}`
: `${frontendUrlRoot}/sandbox/explorer?explorerURLState=${explorerURLState}${
endpoint ? `&endpoint=${endpoint}` : ""
}`;

decorations.push({
document: uri,
glyph: ">",
range: rangeForASTNode(node),
hoverMessage: "Some hover text",
hoverMessage: `[Run in Studio](${runInExplorerLink})`,
});
}
},
Expand Down

0 comments on commit e9af3f6

Please sign in to comment.