Skip to content

Commit

Permalink
Code complete default query variables (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj authored Jul 27, 2018
1 parent 42635af commit a17aeea
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 73 deletions.
91 changes: 37 additions & 54 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/apollo-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"core-js": "^2.5.7",
"glob": "^7.1.2",
"graphql": "^0.13.1",
"graphql-language-service-server": "^1.1.2",
"@apollographql/graphql-language-service-interface": "1.3.2",
"graphql-tag": "^2.9.2",
"minimatch": "^3.0.4",
"moment": "^2.22.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DiagnosticSeverity
} from "vscode-languageserver";

import { getRange as rangeOfTokenAtLocation } from "graphql-language-service-interface/dist/getDiagnostics";
import { getRange as rangeOfTokenAtLocation } from "@apollographql/graphql-language-service-interface/dist/getDiagnostics";

import {
positionFromSourceLocation,
Expand Down
28 changes: 17 additions & 11 deletions packages/apollo-language-server/src/languageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
InsertTextFormat
} from "vscode-languageserver";

import { getAutocompleteSuggestions } from "graphql-language-service-interface";
import { getAutocompleteSuggestions } from "@apollographql/graphql-language-service-interface";
import {
getTokenAtPosition,
getTypeInfo
} from "graphql-language-service-interface/dist/getAutocompleteSuggestions";
} from "@apollographql/graphql-language-service-interface/dist/getAutocompleteSuggestions";

import { GraphQLWorkspace } from "./workspace";
import { DocumentUri, GraphQLProject } from "./project";
Expand Down Expand Up @@ -114,6 +114,7 @@ export class GraphQLLanguageProvider {
doc.source.body,
positionInDocument
).map(suggest => {
// when code completing fields, expand out required variables and open braces
const suggestedField = parentFields[suggest.label] as GraphQLField<
void,
void
Expand Down Expand Up @@ -203,8 +204,12 @@ export class GraphQLLanguageProvider {
const fieldDef = typeInfo.getFieldDef();

if (parentType && fieldDef) {
const argsString = fieldDef.args.length > 0 ?
`(${fieldDef.args.map(a => `${a.name}: ${a.type}`).join(", ")})` : "";
const argsString =
fieldDef.args.length > 0
? `(${fieldDef.args
.map(a => `${a.name}: ${a.type}`)
.join(", ")})`
: "";
return {
contents: `
\`\`\`graphql
Expand All @@ -220,7 +225,9 @@ ${fieldDef.description}
}

case Kind.NAMED_TYPE: {
const type = set.schema.getType(node.name.value) as GraphQLNamedType | void;
const type = set.schema.getType(
node.name.value
) as GraphQLNamedType | void;
if (!type) break;

return {
Expand All @@ -244,7 +251,7 @@ ${argumentNode.name}: ${argumentNode.type}
${argumentNode.description}
`,
range: rangeForASTNode(highlightNodeForNode(node))
}
};
}
}
}
Expand Down Expand Up @@ -390,7 +397,9 @@ ${argumentNode.description}
for (const definition of doc.ast.definitions) {
if (definition.kind === Kind.OPERATION_DEFINITION) {
if (set.endpoint) {
const fragmentSpreads: Set<graphql.FragmentDefinitionNode> = new Set();
const fragmentSpreads: Set<
graphql.FragmentDefinitionNode
> = new Set();
const searchForReferencedFragments = (node: graphql.ASTNode) => {
visit(node, {
FragmentSpread(node: FragmentSpreadNode) {
Expand All @@ -413,10 +422,7 @@ ${argumentNode.description}
`Run ${definition.operation}`,
"apollographql.runQuery",
graphql.parse(
[
definition,
...fragmentSpreads
]
[definition, ...fragmentSpreads]
.map(n => graphql.print(n))
.join("\n")
),
Expand Down
4 changes: 1 addition & 3 deletions packages/apollo-language-server/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
FragmentSpreadNode,
TypeInfo,
visitWithTypeInfo,
GraphQLCompositeType,
printSchema,
buildSchema,
Source
Expand Down Expand Up @@ -343,8 +342,7 @@ export class GraphQLProject {
visitWithTypeInfo(typeInfo, {
enter: node => {
if (node.kind == "Field" && typeInfo.getParentType()) {
const parentName = (typeInfo.getParentType() as GraphQLCompositeType)
.name;
const parentName = typeInfo.getParentType()!.name;
const parentEngineStat = this.engineStats
.get(set.engineKey!)!
.get(parentName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "graphql-language-service-interface" {
declare module "@apollographql/graphql-language-service-interface" {
import { DocumentNode, GraphQLSchema, Location } from "graphql";
import {
Diagnostic,
Expand All @@ -14,9 +14,9 @@ declare module "graphql-language-service-interface" {
): CompletionItem[];
}

declare module "graphql-language-service-interface/dist/getAutocompleteSuggestions";
declare module "@apollographql/graphql-language-service-interface/dist/getAutocompleteSuggestions";

declare module "graphql-language-service-interface/dist/getDiagnostics" {
declare module "@apollographql/graphql-language-service-interface/dist/getDiagnostics" {
import { Location } from "graphql";
import { Range } from "vscode-languageserver";

Expand Down

0 comments on commit a17aeea

Please sign in to comment.