vscode-apollo 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +1 -1
- package/.vscode/launch.json +4 -1
- package/CHANGELOG.md +27 -0
- package/package.json +9 -3
- package/renovate.json +2 -1
- package/sampleWorkspace/localSchema/src/test.js +3 -0
- package/sampleWorkspace/rover/apollo.config.js +3 -0
- package/sampleWorkspace/rover/src/test.graphql +14 -0
- package/sampleWorkspace/rover/src/test.js +30 -0
- package/sampleWorkspace/sampleWorkspace.code-workspace +25 -19
- package/src/language-server/__tests__/document.test.ts +161 -3
- package/src/language-server/__tests__/fixtures/TypeScript.tmLanguage.json +5749 -0
- package/src/language-server/__tests__/fixtures/documents/commentWithTemplate.ts +41 -0
- package/src/language-server/__tests__/fixtures/documents/commentWithTemplate.ts.snap +185 -0
- package/src/language-server/__tests__/fixtures/documents/functionCall.ts +93 -0
- package/src/language-server/__tests__/fixtures/documents/functionCall.ts.snap +431 -0
- package/src/language-server/__tests__/fixtures/documents/taggedTemplate.ts +80 -0
- package/src/language-server/__tests__/fixtures/documents/taggedTemplate.ts.snap +353 -0
- package/src/language-server/__tests__/fixtures/documents/templateWithComment.ts +38 -0
- package/src/language-server/__tests__/fixtures/documents/templateWithComment.ts.snap +123 -0
- package/src/language-server/config/__tests__/loadConfig.ts +19 -10
- package/src/language-server/config/config.ts +26 -1
- package/src/language-server/config/which.d.ts +19 -0
- package/src/language-server/document.ts +86 -53
- package/src/language-server/fileSet.ts +7 -0
- package/src/language-server/project/base.ts +58 -316
- package/src/language-server/project/client.ts +730 -7
- package/src/language-server/project/internal.ts +349 -0
- package/src/language-server/project/rover/DocumentSynchronization.ts +308 -0
- package/src/language-server/project/rover/__tests__/DocumentSynchronization.test.ts +302 -0
- package/src/language-server/project/rover/project.ts +276 -0
- package/src/language-server/server.ts +129 -62
- package/src/language-server/utilities/__tests__/source.test.ts +162 -0
- package/src/language-server/utilities/source.ts +38 -3
- package/src/language-server/workspace.ts +34 -9
- package/syntaxes/graphql.js.json +18 -21
- package/src/language-server/languageProvider.ts +0 -795
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
FragmentDefinitionNode,
|
|
12
12
|
Kind,
|
|
13
13
|
FragmentSpreadNode,
|
|
14
|
-
separateOperations,
|
|
15
14
|
OperationDefinitionNode,
|
|
16
15
|
extendSchema,
|
|
17
16
|
DocumentNode,
|
|
@@ -21,16 +20,52 @@ import {
|
|
|
21
20
|
DefinitionNode,
|
|
22
21
|
ExecutableDefinitionNode,
|
|
23
22
|
print,
|
|
23
|
+
GraphQLNamedType,
|
|
24
|
+
GraphQLField,
|
|
25
|
+
GraphQLNonNull,
|
|
26
|
+
isAbstractType,
|
|
27
|
+
TypeNameMetaFieldDef,
|
|
28
|
+
SchemaMetaFieldDef,
|
|
29
|
+
TypeMetaFieldDef,
|
|
30
|
+
typeFromAST,
|
|
31
|
+
GraphQLType,
|
|
32
|
+
isObjectType,
|
|
33
|
+
isListType,
|
|
34
|
+
GraphQLList,
|
|
35
|
+
isNonNullType,
|
|
36
|
+
ASTNode,
|
|
37
|
+
FieldDefinitionNode,
|
|
38
|
+
isExecutableDefinitionNode,
|
|
39
|
+
isTypeSystemDefinitionNode,
|
|
40
|
+
isTypeSystemExtensionNode,
|
|
41
|
+
DirectiveLocation,
|
|
24
42
|
} from "graphql";
|
|
25
43
|
import { ValidationRule } from "graphql/validation/ValidationContext";
|
|
26
44
|
import {
|
|
27
45
|
NotificationHandler,
|
|
28
46
|
DiagnosticSeverity,
|
|
47
|
+
CancellationToken,
|
|
48
|
+
Position,
|
|
49
|
+
Location,
|
|
50
|
+
CodeLens,
|
|
51
|
+
InsertTextFormat,
|
|
52
|
+
DocumentSymbol,
|
|
53
|
+
SymbolKind,
|
|
54
|
+
SymbolInformation,
|
|
55
|
+
CodeAction,
|
|
56
|
+
CodeActionKind,
|
|
57
|
+
MarkupKind,
|
|
58
|
+
CompletionItemKind,
|
|
29
59
|
} from "vscode-languageserver/node";
|
|
30
60
|
import LZString from "lz-string";
|
|
31
61
|
import { URL } from "node:url";
|
|
32
62
|
|
|
33
|
-
import {
|
|
63
|
+
import {
|
|
64
|
+
positionFromPositionInContainingDocument,
|
|
65
|
+
rangeForASTNode,
|
|
66
|
+
getASTNodeAndTypeInfoAtPosition,
|
|
67
|
+
positionToOffset,
|
|
68
|
+
} from "../utilities/source";
|
|
34
69
|
import { formatMS } from "../format";
|
|
35
70
|
import { LoadingHandler } from "../loadingHandler";
|
|
36
71
|
import { apolloClientSchemaDocument } from "./defaultClientSchema";
|
|
@@ -43,9 +78,6 @@ import {
|
|
|
43
78
|
} from "../engine";
|
|
44
79
|
import { ClientConfig } from "../config";
|
|
45
80
|
import {
|
|
46
|
-
removeDirectives,
|
|
47
|
-
removeDirectiveAnnotatedFields,
|
|
48
|
-
withTypenameFieldAddedWhereNeeded,
|
|
49
81
|
ClientSchemaInfo,
|
|
50
82
|
isDirectiveDefinitionNode,
|
|
51
83
|
} from "../utilities/graphql";
|
|
@@ -58,7 +90,20 @@ import {
|
|
|
58
90
|
} from "../diagnostics";
|
|
59
91
|
import { URI } from "vscode-uri";
|
|
60
92
|
import type { EngineDecoration } from "../../messages";
|
|
61
|
-
|
|
93
|
+
|
|
94
|
+
// should eventually be moved into this package, since we're overriding a lot of the existing behavior here
|
|
95
|
+
import { getAutocompleteSuggestions } from "graphql-language-service";
|
|
96
|
+
import { Position as GraphQlPosition } from "graphql-language-service";
|
|
97
|
+
import { getTokenAtPosition, getTypeInfo } from "graphql-language-service";
|
|
98
|
+
import type { DocumentUri } from "../project/base";
|
|
99
|
+
|
|
100
|
+
import { highlightNodeForNode } from "../utilities/graphql";
|
|
101
|
+
|
|
102
|
+
import { isNotNullOrUndefined } from "../../tools";
|
|
103
|
+
import type { CodeActionInfo } from "../errors/validation";
|
|
104
|
+
import { GraphQLDiagnostic } from "../diagnostics";
|
|
105
|
+
import { isInterfaceType } from "graphql";
|
|
106
|
+
import { GraphQLInternalProject } from "./internal";
|
|
62
107
|
|
|
63
108
|
type Maybe<T> = null | undefined | T;
|
|
64
109
|
|
|
@@ -84,6 +129,41 @@ function augmentSchemaWithGeneratedSDLIfNeeded(
|
|
|
84
129
|
);
|
|
85
130
|
}
|
|
86
131
|
|
|
132
|
+
const DirectiveLocations = Object.keys(DirectiveLocation);
|
|
133
|
+
|
|
134
|
+
function hasFields(type: GraphQLType): boolean {
|
|
135
|
+
return (
|
|
136
|
+
isObjectType(type) ||
|
|
137
|
+
(isListType(type) && hasFields((type as GraphQLList<any>).ofType)) ||
|
|
138
|
+
(isNonNullType(type) && hasFields((type as GraphQLNonNull<any>).ofType))
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function uriForASTNode(node: ASTNode): DocumentUri | null {
|
|
143
|
+
const uri = node.loc && node.loc.source && node.loc.source.name;
|
|
144
|
+
if (!uri || uri === "GraphQL") {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return uri;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function locationForASTNode(node: ASTNode): Location | null {
|
|
151
|
+
const uri = uriForASTNode(node);
|
|
152
|
+
if (!uri) return null;
|
|
153
|
+
return Location.create(uri, rangeForASTNode(node));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function symbolForFieldDefinition(
|
|
157
|
+
definition: FieldDefinitionNode,
|
|
158
|
+
): DocumentSymbol {
|
|
159
|
+
return {
|
|
160
|
+
name: definition.name.value,
|
|
161
|
+
kind: SymbolKind.Field,
|
|
162
|
+
range: rangeForASTNode(definition),
|
|
163
|
+
selectionRange: rangeForASTNode(definition),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
87
167
|
export function isClientProject(
|
|
88
168
|
project: GraphQLProject,
|
|
89
169
|
): project is GraphQLClientProject {
|
|
@@ -96,7 +176,7 @@ export interface GraphQLClientProjectConfig {
|
|
|
96
176
|
configFolderURI: URI;
|
|
97
177
|
loadingHandler: LoadingHandler;
|
|
98
178
|
}
|
|
99
|
-
export class GraphQLClientProject extends
|
|
179
|
+
export class GraphQLClientProject extends GraphQLInternalProject {
|
|
100
180
|
public serviceID?: string;
|
|
101
181
|
public config!: ClientConfig;
|
|
102
182
|
|
|
@@ -534,6 +614,649 @@ export class GraphQLClientProject extends GraphQLProject {
|
|
|
534
614
|
// if we don't have an `engineClient`, we are not in a studio project and `this.config.graph` could be just about anything
|
|
535
615
|
return this.engineClient ? this.config.graph : undefined;
|
|
536
616
|
}
|
|
617
|
+
|
|
618
|
+
onCompletion: GraphQLProject["onCompletion"] = async (
|
|
619
|
+
{ textDocument: { uri }, position },
|
|
620
|
+
_token: CancellationToken,
|
|
621
|
+
) => {
|
|
622
|
+
const document = this.documentAt(uri, position);
|
|
623
|
+
if (!document) return [];
|
|
624
|
+
|
|
625
|
+
if (!this.schema) return [];
|
|
626
|
+
|
|
627
|
+
const rawPositionInDocument = positionFromPositionInContainingDocument(
|
|
628
|
+
document.source,
|
|
629
|
+
position,
|
|
630
|
+
);
|
|
631
|
+
const positionInDocument = new GraphQlPosition(
|
|
632
|
+
rawPositionInDocument.line,
|
|
633
|
+
rawPositionInDocument.character,
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
const token = getTokenAtPosition(document.source.body, positionInDocument);
|
|
637
|
+
const state =
|
|
638
|
+
token.state.kind === "Invalid" ? token.state.prevState : token.state;
|
|
639
|
+
const typeInfo = getTypeInfo(this.schema, token.state);
|
|
640
|
+
|
|
641
|
+
if (state?.kind === "DirectiveDef") {
|
|
642
|
+
return DirectiveLocations.map((location) => ({
|
|
643
|
+
label: location,
|
|
644
|
+
kind: CompletionItemKind.Constant,
|
|
645
|
+
}));
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const suggestions = getAutocompleteSuggestions(
|
|
649
|
+
this.schema,
|
|
650
|
+
document.source.body,
|
|
651
|
+
positionInDocument,
|
|
652
|
+
);
|
|
653
|
+
|
|
654
|
+
if (
|
|
655
|
+
state?.kind === "SelectionSet" ||
|
|
656
|
+
state?.kind === "Field" ||
|
|
657
|
+
state?.kind === "AliasedField"
|
|
658
|
+
) {
|
|
659
|
+
const parentType = typeInfo.parentType;
|
|
660
|
+
const parentFields =
|
|
661
|
+
isInterfaceType(parentType) || isObjectType(parentType)
|
|
662
|
+
? parentType.getFields()
|
|
663
|
+
: {};
|
|
664
|
+
|
|
665
|
+
if (isAbstractType(parentType)) {
|
|
666
|
+
parentFields[TypeNameMetaFieldDef.name] = TypeNameMetaFieldDef;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if (parentType === this.schema.getQueryType()) {
|
|
670
|
+
parentFields[SchemaMetaFieldDef.name] = SchemaMetaFieldDef;
|
|
671
|
+
parentFields[TypeMetaFieldDef.name] = TypeMetaFieldDef;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
return suggestions.map((suggest) => {
|
|
675
|
+
// when code completing fields, expand out required variables and open braces
|
|
676
|
+
const suggestedField = parentFields[suggest.label] as GraphQLField<
|
|
677
|
+
void,
|
|
678
|
+
void
|
|
679
|
+
>;
|
|
680
|
+
if (!suggestedField) {
|
|
681
|
+
return suggest;
|
|
682
|
+
} else {
|
|
683
|
+
const requiredArgs = suggestedField.args.filter((a) =>
|
|
684
|
+
isNonNullType(a.type),
|
|
685
|
+
);
|
|
686
|
+
const paramsSection =
|
|
687
|
+
requiredArgs.length > 0
|
|
688
|
+
? `(${requiredArgs
|
|
689
|
+
.map((a, i) => `${a.name}: $${i + 1}`)
|
|
690
|
+
.join(", ")})`
|
|
691
|
+
: ``;
|
|
692
|
+
|
|
693
|
+
const isClientType =
|
|
694
|
+
parentType &&
|
|
695
|
+
"clientSchema" in parentType &&
|
|
696
|
+
parentType.clientSchema?.localFields?.includes(suggestedField.name);
|
|
697
|
+
const directives = isClientType ? " @client" : "";
|
|
698
|
+
|
|
699
|
+
const snippet = hasFields(suggestedField.type)
|
|
700
|
+
? `${suggest.label}${paramsSection}${directives} {\n\t$0\n}`
|
|
701
|
+
: `${suggest.label}${paramsSection}${directives}`;
|
|
702
|
+
|
|
703
|
+
return {
|
|
704
|
+
...suggest,
|
|
705
|
+
insertText: snippet,
|
|
706
|
+
insertTextFormat: InsertTextFormat.Snippet,
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (state?.kind === "Directive") {
|
|
713
|
+
return suggestions.map((suggest) => {
|
|
714
|
+
const directive = this.schema!.getDirective(suggest.label);
|
|
715
|
+
if (!directive) {
|
|
716
|
+
return suggest;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const requiredArgs = directive.args.filter(isNonNullType);
|
|
720
|
+
const paramsSection =
|
|
721
|
+
requiredArgs.length > 0
|
|
722
|
+
? `(${requiredArgs
|
|
723
|
+
.map((a, i) => `${a.name}: $${i + 1}`)
|
|
724
|
+
.join(", ")})`
|
|
725
|
+
: ``;
|
|
726
|
+
|
|
727
|
+
const snippet = `${suggest.label}${paramsSection}`;
|
|
728
|
+
|
|
729
|
+
const argsString =
|
|
730
|
+
directive.args.length > 0
|
|
731
|
+
? `(${directive.args
|
|
732
|
+
.map((a) => `${a.name}: ${a.type}`)
|
|
733
|
+
.join(", ")})`
|
|
734
|
+
: "";
|
|
735
|
+
|
|
736
|
+
const content = [
|
|
737
|
+
[`\`\`\`graphql`, `@${suggest.label}${argsString}`, `\`\`\``].join(
|
|
738
|
+
"\n",
|
|
739
|
+
),
|
|
740
|
+
];
|
|
741
|
+
|
|
742
|
+
if (suggest.documentation) {
|
|
743
|
+
if (typeof suggest.documentation === "string") {
|
|
744
|
+
content.push(suggest.documentation);
|
|
745
|
+
} else {
|
|
746
|
+
// TODO (jason) `(string | MarkupContent) & (string | null)` is a weird type,
|
|
747
|
+
// leaving this for safety for now
|
|
748
|
+
content.push((suggest.documentation as any).value);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
const doc = {
|
|
753
|
+
kind: MarkupKind.Markdown,
|
|
754
|
+
value: content.join("\n\n"),
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
return {
|
|
758
|
+
...suggest,
|
|
759
|
+
documentation: doc,
|
|
760
|
+
insertText: snippet,
|
|
761
|
+
insertTextFormat: InsertTextFormat.Snippet,
|
|
762
|
+
};
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
return suggestions;
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
onHover: GraphQLProject["onHover"] = async (
|
|
770
|
+
{ textDocument: { uri }, position },
|
|
771
|
+
_token,
|
|
772
|
+
) => {
|
|
773
|
+
const document = this.documentAt(uri, position);
|
|
774
|
+
if (!(document && document.ast)) return null;
|
|
775
|
+
|
|
776
|
+
if (!this.schema) return null;
|
|
777
|
+
|
|
778
|
+
const positionInDocument = positionFromPositionInContainingDocument(
|
|
779
|
+
document.source,
|
|
780
|
+
position,
|
|
781
|
+
);
|
|
782
|
+
|
|
783
|
+
const nodeAndTypeInfo = getASTNodeAndTypeInfoAtPosition(
|
|
784
|
+
document.source,
|
|
785
|
+
positionInDocument,
|
|
786
|
+
document.ast,
|
|
787
|
+
this.schema,
|
|
788
|
+
);
|
|
789
|
+
|
|
790
|
+
if (nodeAndTypeInfo) {
|
|
791
|
+
const [node, typeInfo] = nodeAndTypeInfo;
|
|
792
|
+
|
|
793
|
+
switch (node.kind) {
|
|
794
|
+
case Kind.FRAGMENT_SPREAD: {
|
|
795
|
+
const fragmentName = node.name.value;
|
|
796
|
+
const fragment = this.fragments[fragmentName];
|
|
797
|
+
if (fragment) {
|
|
798
|
+
return {
|
|
799
|
+
contents: {
|
|
800
|
+
language: "graphql",
|
|
801
|
+
value: `fragment ${fragmentName} on ${fragment.typeCondition.name.value}`,
|
|
802
|
+
},
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
case Kind.FIELD: {
|
|
809
|
+
const parentType = typeInfo.getParentType();
|
|
810
|
+
const fieldDef = typeInfo.getFieldDef();
|
|
811
|
+
|
|
812
|
+
if (parentType && fieldDef) {
|
|
813
|
+
const argsString =
|
|
814
|
+
fieldDef.args.length > 0
|
|
815
|
+
? `(${fieldDef.args
|
|
816
|
+
.map((a) => `${a.name}: ${a.type}`)
|
|
817
|
+
.join(", ")})`
|
|
818
|
+
: "";
|
|
819
|
+
const isClientType =
|
|
820
|
+
parentType.clientSchema &&
|
|
821
|
+
parentType.clientSchema.localFields &&
|
|
822
|
+
parentType.clientSchema.localFields.includes(fieldDef.name);
|
|
823
|
+
|
|
824
|
+
const isResolvedLocally =
|
|
825
|
+
node.directives &&
|
|
826
|
+
node.directives.some(
|
|
827
|
+
(directive) => directive.name.value === "client",
|
|
828
|
+
);
|
|
829
|
+
|
|
830
|
+
const content = [
|
|
831
|
+
[
|
|
832
|
+
`\`\`\`graphql`,
|
|
833
|
+
`${parentType}.${fieldDef.name}${argsString}: ${fieldDef.type}`,
|
|
834
|
+
`\`\`\``,
|
|
835
|
+
].join("\n"),
|
|
836
|
+
];
|
|
837
|
+
|
|
838
|
+
const info: string[] = [];
|
|
839
|
+
if (isClientType) {
|
|
840
|
+
info.push("`Client-Only Field`");
|
|
841
|
+
}
|
|
842
|
+
if (isResolvedLocally) {
|
|
843
|
+
info.push("`Resolved locally`");
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
if (info.length !== 0) {
|
|
847
|
+
content.push(info.join(" "));
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
if (fieldDef.description) {
|
|
851
|
+
content.push(fieldDef.description);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
return {
|
|
855
|
+
contents: content.join("\n\n---\n\n"),
|
|
856
|
+
range: rangeForASTNode(highlightNodeForNode(node)),
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
case Kind.NAMED_TYPE: {
|
|
864
|
+
const type = this.schema.getType(
|
|
865
|
+
node.name.value,
|
|
866
|
+
) as GraphQLNamedType | void;
|
|
867
|
+
if (!type) break;
|
|
868
|
+
|
|
869
|
+
const content = [[`\`\`\`graphql`, `${type}`, `\`\`\``].join("\n")];
|
|
870
|
+
|
|
871
|
+
if (type.description) {
|
|
872
|
+
content.push(type.description);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
return {
|
|
876
|
+
contents: content.join("\n\n---\n\n"),
|
|
877
|
+
range: rangeForASTNode(highlightNodeForNode(node)),
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
case Kind.ARGUMENT: {
|
|
882
|
+
const argumentNode = typeInfo.getArgument()!;
|
|
883
|
+
const content = [
|
|
884
|
+
[
|
|
885
|
+
`\`\`\`graphql`,
|
|
886
|
+
`${argumentNode.name}: ${argumentNode.type}`,
|
|
887
|
+
`\`\`\``,
|
|
888
|
+
].join("\n"),
|
|
889
|
+
];
|
|
890
|
+
if (argumentNode.description) {
|
|
891
|
+
content.push(argumentNode.description);
|
|
892
|
+
}
|
|
893
|
+
return {
|
|
894
|
+
contents: content.join("\n\n---\n\n"),
|
|
895
|
+
range: rangeForASTNode(highlightNodeForNode(node)),
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
case Kind.DIRECTIVE: {
|
|
900
|
+
const directiveNode = typeInfo.getDirective();
|
|
901
|
+
if (!directiveNode) break;
|
|
902
|
+
const argsString =
|
|
903
|
+
directiveNode.args.length > 0
|
|
904
|
+
? `(${directiveNode.args
|
|
905
|
+
.map((a) => `${a.name}: ${a.type}`)
|
|
906
|
+
.join(", ")})`
|
|
907
|
+
: "";
|
|
908
|
+
const content = [
|
|
909
|
+
[
|
|
910
|
+
`\`\`\`graphql`,
|
|
911
|
+
`@${directiveNode.name}${argsString}`,
|
|
912
|
+
`\`\`\``,
|
|
913
|
+
].join("\n"),
|
|
914
|
+
];
|
|
915
|
+
if (directiveNode.description) {
|
|
916
|
+
content.push(directiveNode.description);
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
contents: content.join("\n\n---\n\n"),
|
|
920
|
+
range: rangeForASTNode(highlightNodeForNode(node)),
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return null;
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
onDefinition: GraphQLProject["onDefinition"] = async ({
|
|
929
|
+
position,
|
|
930
|
+
textDocument: { uri },
|
|
931
|
+
}) => {
|
|
932
|
+
const document = this.documentAt(uri, position);
|
|
933
|
+
if (!(document && document.ast)) return null;
|
|
934
|
+
|
|
935
|
+
if (!this.schema) return null;
|
|
936
|
+
|
|
937
|
+
const positionInDocument = positionFromPositionInContainingDocument(
|
|
938
|
+
document.source,
|
|
939
|
+
position,
|
|
940
|
+
);
|
|
941
|
+
|
|
942
|
+
const nodeAndTypeInfo = getASTNodeAndTypeInfoAtPosition(
|
|
943
|
+
document.source,
|
|
944
|
+
positionInDocument,
|
|
945
|
+
document.ast,
|
|
946
|
+
this.schema,
|
|
947
|
+
);
|
|
948
|
+
|
|
949
|
+
if (nodeAndTypeInfo) {
|
|
950
|
+
const [node, typeInfo] = nodeAndTypeInfo;
|
|
951
|
+
|
|
952
|
+
switch (node.kind) {
|
|
953
|
+
case Kind.FRAGMENT_SPREAD: {
|
|
954
|
+
const fragmentName = node.name.value;
|
|
955
|
+
const fragment = this.fragments[fragmentName];
|
|
956
|
+
if (fragment && fragment.loc) {
|
|
957
|
+
return locationForASTNode(fragment);
|
|
958
|
+
}
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
case Kind.FIELD: {
|
|
962
|
+
const fieldDef = typeInfo.getFieldDef();
|
|
963
|
+
|
|
964
|
+
if (!(fieldDef && fieldDef.astNode && fieldDef.astNode.loc)) break;
|
|
965
|
+
|
|
966
|
+
return locationForASTNode(fieldDef.astNode);
|
|
967
|
+
}
|
|
968
|
+
case Kind.NAMED_TYPE: {
|
|
969
|
+
const type = typeFromAST(this.schema, node);
|
|
970
|
+
|
|
971
|
+
if (!(type && type.astNode && type.astNode.loc)) break;
|
|
972
|
+
|
|
973
|
+
return locationForASTNode(type.astNode);
|
|
974
|
+
}
|
|
975
|
+
case Kind.DIRECTIVE: {
|
|
976
|
+
const directive = this.schema.getDirective(node.name.value);
|
|
977
|
+
|
|
978
|
+
if (!(directive && directive.astNode && directive.astNode.loc)) break;
|
|
979
|
+
|
|
980
|
+
return locationForASTNode(directive.astNode);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
return null;
|
|
985
|
+
};
|
|
986
|
+
|
|
987
|
+
onReferences: GraphQLProject["onReferences"] = async ({
|
|
988
|
+
position,
|
|
989
|
+
textDocument: { uri },
|
|
990
|
+
}) => {
|
|
991
|
+
const document = this.documentAt(uri, position);
|
|
992
|
+
if (!(document && document.ast)) return null;
|
|
993
|
+
|
|
994
|
+
if (!this.schema) return null;
|
|
995
|
+
|
|
996
|
+
const positionInDocument = positionFromPositionInContainingDocument(
|
|
997
|
+
document.source,
|
|
998
|
+
position,
|
|
999
|
+
);
|
|
1000
|
+
|
|
1001
|
+
const nodeAndTypeInfo = getASTNodeAndTypeInfoAtPosition(
|
|
1002
|
+
document.source,
|
|
1003
|
+
positionInDocument,
|
|
1004
|
+
document.ast,
|
|
1005
|
+
this.schema,
|
|
1006
|
+
);
|
|
1007
|
+
|
|
1008
|
+
if (nodeAndTypeInfo) {
|
|
1009
|
+
const [node, typeInfo] = nodeAndTypeInfo;
|
|
1010
|
+
|
|
1011
|
+
switch (node.kind) {
|
|
1012
|
+
case Kind.FRAGMENT_DEFINITION: {
|
|
1013
|
+
const fragmentName = node.name.value;
|
|
1014
|
+
return this.fragmentSpreadsForFragment(fragmentName)
|
|
1015
|
+
.map((fragmentSpread) => locationForASTNode(fragmentSpread))
|
|
1016
|
+
.filter(isNotNullOrUndefined);
|
|
1017
|
+
}
|
|
1018
|
+
// TODO(jbaxleyiii): manage no parent type references (unions + scalars)
|
|
1019
|
+
// TODO(jbaxleyiii): support more than fields
|
|
1020
|
+
case Kind.FIELD_DEFINITION: {
|
|
1021
|
+
// case Kind.ENUM_VALUE_DEFINITION:
|
|
1022
|
+
// case Kind.INPUT_OBJECT_TYPE_DEFINITION:
|
|
1023
|
+
// case Kind.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
1024
|
+
const offset = positionToOffset(document.source, positionInDocument);
|
|
1025
|
+
// withWithTypeInfo doesn't suppport SDL so we instead
|
|
1026
|
+
// write our own visitor methods here to collect the fields that we
|
|
1027
|
+
// care about
|
|
1028
|
+
let parent: ASTNode | null = null;
|
|
1029
|
+
visit(document.ast, {
|
|
1030
|
+
enter(node: ASTNode) {
|
|
1031
|
+
// the parent types we care about
|
|
1032
|
+
if (
|
|
1033
|
+
node.loc &&
|
|
1034
|
+
node.loc.start <= offset &&
|
|
1035
|
+
offset <= node.loc.end &&
|
|
1036
|
+
(node.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
1037
|
+
node.kind === Kind.OBJECT_TYPE_EXTENSION ||
|
|
1038
|
+
node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
1039
|
+
node.kind === Kind.INTERFACE_TYPE_EXTENSION ||
|
|
1040
|
+
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
|
|
1041
|
+
node.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION ||
|
|
1042
|
+
node.kind === Kind.ENUM_TYPE_DEFINITION ||
|
|
1043
|
+
node.kind === Kind.ENUM_TYPE_EXTENSION)
|
|
1044
|
+
) {
|
|
1045
|
+
parent = node;
|
|
1046
|
+
}
|
|
1047
|
+
return;
|
|
1048
|
+
},
|
|
1049
|
+
});
|
|
1050
|
+
return this.getOperationFieldsFromFieldDefinition(
|
|
1051
|
+
node.name.value,
|
|
1052
|
+
parent,
|
|
1053
|
+
)
|
|
1054
|
+
.map((fieldNode) => locationForASTNode(fieldNode))
|
|
1055
|
+
.filter(isNotNullOrUndefined);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
return null;
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
onDocumentSymbol: GraphQLProject["onDocumentSymbol"] = async ({
|
|
1064
|
+
textDocument: { uri },
|
|
1065
|
+
}) => {
|
|
1066
|
+
const definitions = this.definitionsAt(uri);
|
|
1067
|
+
|
|
1068
|
+
const symbols: DocumentSymbol[] = [];
|
|
1069
|
+
|
|
1070
|
+
for (const definition of definitions) {
|
|
1071
|
+
if (isExecutableDefinitionNode(definition)) {
|
|
1072
|
+
if (!definition.name) continue;
|
|
1073
|
+
const location = locationForASTNode(definition);
|
|
1074
|
+
if (!location) continue;
|
|
1075
|
+
symbols.push({
|
|
1076
|
+
name: definition.name.value,
|
|
1077
|
+
kind: SymbolKind.Function,
|
|
1078
|
+
range: rangeForASTNode(definition),
|
|
1079
|
+
selectionRange: rangeForASTNode(highlightNodeForNode(definition)),
|
|
1080
|
+
});
|
|
1081
|
+
} else if (
|
|
1082
|
+
isTypeSystemDefinitionNode(definition) ||
|
|
1083
|
+
isTypeSystemExtensionNode(definition)
|
|
1084
|
+
) {
|
|
1085
|
+
if (
|
|
1086
|
+
definition.kind === Kind.SCHEMA_DEFINITION ||
|
|
1087
|
+
definition.kind === Kind.SCHEMA_EXTENSION
|
|
1088
|
+
) {
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
symbols.push({
|
|
1092
|
+
name: definition.name.value,
|
|
1093
|
+
kind: SymbolKind.Class,
|
|
1094
|
+
range: rangeForASTNode(definition),
|
|
1095
|
+
selectionRange: rangeForASTNode(highlightNodeForNode(definition)),
|
|
1096
|
+
children:
|
|
1097
|
+
definition.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
1098
|
+
definition.kind === Kind.OBJECT_TYPE_EXTENSION
|
|
1099
|
+
? (definition.fields || []).map(symbolForFieldDefinition)
|
|
1100
|
+
: undefined,
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
return symbols;
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
async provideSymbol(
|
|
1109
|
+
_query: string,
|
|
1110
|
+
_token: CancellationToken,
|
|
1111
|
+
): Promise<SymbolInformation[]> {
|
|
1112
|
+
const symbols: SymbolInformation[] = [];
|
|
1113
|
+
|
|
1114
|
+
for (const definition of this.definitions) {
|
|
1115
|
+
if (isExecutableDefinitionNode(definition)) {
|
|
1116
|
+
if (!definition.name) continue;
|
|
1117
|
+
const location = locationForASTNode(definition);
|
|
1118
|
+
if (!location) continue;
|
|
1119
|
+
symbols.push({
|
|
1120
|
+
name: definition.name.value,
|
|
1121
|
+
kind: SymbolKind.Function,
|
|
1122
|
+
location,
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
return symbols;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
onCodeLens: GraphQLProject["onCodeLens"] = async ({
|
|
1130
|
+
textDocument: { uri },
|
|
1131
|
+
}) => {
|
|
1132
|
+
// Wait for the project to be fully initialized, so we always provide code lenses for open files, even
|
|
1133
|
+
// if we receive the request before the project is ready.
|
|
1134
|
+
await this.whenReady;
|
|
1135
|
+
|
|
1136
|
+
const documents = this.documentsAt(uri);
|
|
1137
|
+
if (!documents) return [];
|
|
1138
|
+
|
|
1139
|
+
let codeLenses: CodeLens[] = [];
|
|
1140
|
+
|
|
1141
|
+
for (const document of documents) {
|
|
1142
|
+
if (!document.ast) continue;
|
|
1143
|
+
|
|
1144
|
+
for (const definition of document.ast.definitions) {
|
|
1145
|
+
if (definition.kind === Kind.OPERATION_DEFINITION) {
|
|
1146
|
+
/*
|
|
1147
|
+
if (set.endpoint) {
|
|
1148
|
+
const fragmentSpreads: Set<
|
|
1149
|
+
graphql.FragmentDefinitionNode
|
|
1150
|
+
> = new Set();
|
|
1151
|
+
const searchForReferencedFragments = (node: graphql.ASTNode) => {
|
|
1152
|
+
visit(node, {
|
|
1153
|
+
FragmentSpread(node: FragmentSpreadNode) {
|
|
1154
|
+
const fragDefn = project.fragments[node.name.value];
|
|
1155
|
+
if (!fragDefn) return;
|
|
1156
|
+
|
|
1157
|
+
if (!fragmentSpreads.has(fragDefn)) {
|
|
1158
|
+
fragmentSpreads.add(fragDefn);
|
|
1159
|
+
searchForReferencedFragments(fragDefn);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
searchForReferencedFragments(definition);
|
|
1166
|
+
|
|
1167
|
+
codeLenses.push({
|
|
1168
|
+
range: rangeForASTNode(definition),
|
|
1169
|
+
command: Command.create(
|
|
1170
|
+
`Run ${definition.operation}`,
|
|
1171
|
+
"apollographql.runQuery",
|
|
1172
|
+
graphql.parse(
|
|
1173
|
+
[definition, ...fragmentSpreads]
|
|
1174
|
+
.map(n => graphql.print(n))
|
|
1175
|
+
.join("\n")
|
|
1176
|
+
),
|
|
1177
|
+
definition.operation === "subscription"
|
|
1178
|
+
? set.endpoint.subscriptions
|
|
1179
|
+
: set.endpoint.url,
|
|
1180
|
+
set.endpoint.headers,
|
|
1181
|
+
graphql.printSchema(set.schema!)
|
|
1182
|
+
)
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
*/
|
|
1186
|
+
} else if (definition.kind === Kind.FRAGMENT_DEFINITION) {
|
|
1187
|
+
// remove project references for fragment now
|
|
1188
|
+
// const fragmentName = definition.name.value;
|
|
1189
|
+
// const locations = project
|
|
1190
|
+
// .fragmentSpreadsForFragment(fragmentName)
|
|
1191
|
+
// .map(fragmentSpread => locationForASTNode(fragmentSpread))
|
|
1192
|
+
// .filter(isNotNullOrUndefined);
|
|
1193
|
+
// const command = Command.create(
|
|
1194
|
+
// `${locations.length} references`,
|
|
1195
|
+
// "editor.action.showReferences",
|
|
1196
|
+
// uri,
|
|
1197
|
+
// rangeForASTNode(definition).start,
|
|
1198
|
+
// locations
|
|
1199
|
+
// );
|
|
1200
|
+
// codeLenses.push({
|
|
1201
|
+
// range: rangeForASTNode(definition),
|
|
1202
|
+
// command
|
|
1203
|
+
// });
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
return codeLenses;
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
onCodeAction: GraphQLProject["onCodeAction"] = async ({
|
|
1211
|
+
textDocument: { uri },
|
|
1212
|
+
range,
|
|
1213
|
+
}) => {
|
|
1214
|
+
function isPositionLessThanOrEqual(a: Position, b: Position) {
|
|
1215
|
+
return a.line !== b.line ? a.line < b.line : a.character <= b.character;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
if (!this.diagnosticSet) return [];
|
|
1219
|
+
|
|
1220
|
+
await this.whenReady;
|
|
1221
|
+
|
|
1222
|
+
const documents = this.documentsAt(uri);
|
|
1223
|
+
if (!documents) return [];
|
|
1224
|
+
|
|
1225
|
+
const errors: Set<GraphQLError> = new Set();
|
|
1226
|
+
|
|
1227
|
+
for (const [diagnosticUri, diagnostics] of this.diagnosticSet.entries()) {
|
|
1228
|
+
if (diagnosticUri !== uri) continue;
|
|
1229
|
+
|
|
1230
|
+
for (const diagnostic of diagnostics) {
|
|
1231
|
+
if (
|
|
1232
|
+
GraphQLDiagnostic.is(diagnostic) &&
|
|
1233
|
+
isPositionLessThanOrEqual(range.start, diagnostic.range.end) &&
|
|
1234
|
+
isPositionLessThanOrEqual(diagnostic.range.start, range.end)
|
|
1235
|
+
) {
|
|
1236
|
+
errors.add(diagnostic.error);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
const result: CodeAction[] = [];
|
|
1242
|
+
|
|
1243
|
+
for (const error of errors) {
|
|
1244
|
+
const { extensions } = error;
|
|
1245
|
+
if (!extensions || !extensions.codeAction) continue;
|
|
1246
|
+
|
|
1247
|
+
const { message, edits }: CodeActionInfo = extensions.codeAction as any;
|
|
1248
|
+
|
|
1249
|
+
const codeAction = CodeAction.create(
|
|
1250
|
+
message,
|
|
1251
|
+
{ changes: { [uri]: edits } },
|
|
1252
|
+
CodeActionKind.QuickFix,
|
|
1253
|
+
);
|
|
1254
|
+
|
|
1255
|
+
result.push(codeAction);
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
return result;
|
|
1259
|
+
};
|
|
537
1260
|
}
|
|
538
1261
|
|
|
539
1262
|
function buildExplorerURL({
|