vscode-apollo 1.19.3 → 2.0.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/.changeset/README.md +8 -0
- package/.changeset/config.json +14 -0
- package/.circleci/config.yml +91 -0
- package/.eslintrc.js +10 -0
- package/.git-blame-ignore-revs +2 -0
- package/.gitattributes +1 -0
- package/.github/workflows/release.yml +95 -0
- package/.gitleaks.toml +26 -0
- package/.nvmrc +1 -0
- package/.prettierrc +5 -0
- package/.vscode/launch.json +66 -0
- package/.vscode/settings.json +16 -0
- package/.vscode/tasks.json +60 -0
- package/.vscodeignore +28 -1
- package/CHANGELOG.md +250 -1
- package/CODEOWNERS +4 -0
- package/LICENSE +2 -2
- package/README.md +104 -55
- package/codegen.yml +12 -0
- package/graphql.configuration.json +5 -1
- package/images/IconRun.svg +8 -0
- package/images/marketplace/apollo-wordmark.png +0 -0
- package/jest.config.ts +21 -0
- package/jest.e2e.config.js +17 -0
- package/package.json +102 -23
- package/renovate.json +30 -0
- package/sampleWorkspace/clientSchema/apollo.config.cjs +10 -0
- package/sampleWorkspace/clientSchema/src/clientSchema.js +16 -0
- package/sampleWorkspace/clientSchema/src/test.js +18 -0
- package/sampleWorkspace/fixtures/starwarsSchema.graphql +299 -0
- package/sampleWorkspace/httpSchema/apollo.config.ts +8 -0
- package/sampleWorkspace/httpSchema/src/test.js +9 -0
- package/sampleWorkspace/localSchema/apollo.config.js +8 -0
- package/sampleWorkspace/localSchema/src/test.js +8 -0
- package/sampleWorkspace/localSchemaArray/apollo.config.js +12 -0
- package/sampleWorkspace/localSchemaArray/planets.graphql +20 -0
- package/sampleWorkspace/localSchemaArray/src/test.js +12 -0
- package/sampleWorkspace/sampleWorkspace.code-workspace +20 -0
- package/sampleWorkspace/spotifyGraph/apollo.config.mjs +5 -0
- package/sampleWorkspace/spotifyGraph/src/test.js +11 -0
- package/src/__e2e__/mockServer.js +117 -0
- package/src/__e2e__/mocks.js +13094 -0
- package/src/__e2e__/run.js +23 -0
- package/src/__e2e__/runTests.js +44 -0
- package/src/__e2e__/setup.js +1 -0
- package/src/__e2e__/vscode-environment.js +16 -0
- package/src/__e2e__/vscode.js +1 -0
- package/src/__mocks__/fs.js +3 -0
- package/src/__tests__/statusBar.test.ts +8 -7
- package/src/build.js +57 -0
- package/src/debug.ts +2 -5
- package/src/env/index.ts +1 -0
- package/src/env/typescript-utility-types.ts +2 -0
- package/src/extension.ts +265 -170
- package/src/language-server/__e2e__/clientSchema.e2e.ts +147 -0
- package/src/language-server/__e2e__/httpSchema.e2e.ts +21 -0
- package/src/language-server/__e2e__/localSchema.e2e.ts +25 -0
- package/src/language-server/__e2e__/localSchemaArray.e2e.ts +31 -0
- package/src/language-server/__e2e__/studioGraph.e2e.ts +65 -0
- package/src/language-server/__e2e__/utils.ts +151 -0
- package/src/language-server/__tests__/diagnostics.test.ts +86 -0
- package/src/language-server/__tests__/document.test.ts +187 -0
- package/src/language-server/__tests__/fileSet.test.ts +46 -0
- package/src/language-server/__tests__/fixtures/starwarsSchema.ts +1917 -0
- package/src/language-server/config/__tests__/config.ts +54 -0
- package/src/language-server/config/__tests__/loadConfig.ts +384 -0
- package/src/language-server/config/__tests__/utils.ts +99 -0
- package/src/language-server/config/config.ts +284 -0
- package/src/language-server/config/index.ts +3 -0
- package/src/language-server/config/loadConfig.ts +101 -0
- package/src/language-server/config/utils.ts +45 -0
- package/src/language-server/diagnostics.ts +118 -0
- package/src/language-server/document.ts +277 -0
- package/src/language-server/engine/index.ts +123 -0
- package/src/language-server/engine/operations/frontendUrlRoot.ts +15 -0
- package/src/language-server/engine/operations/schemaTagsAndFieldStats.ts +32 -0
- package/src/language-server/errors/__tests__/NoMissingClientDirectives.test.ts +225 -0
- package/src/language-server/errors/logger.ts +58 -0
- package/src/language-server/errors/validation.ts +274 -0
- package/src/language-server/fileSet.ts +63 -0
- package/src/language-server/format.ts +48 -0
- package/src/language-server/graphqlTypes.ts +16741 -0
- package/src/language-server/index.ts +28 -0
- package/src/language-server/languageProvider.ts +795 -0
- package/src/language-server/loadingHandler.ts +47 -0
- package/src/language-server/project/base.ts +406 -0
- package/src/language-server/project/client.ts +568 -0
- package/src/language-server/project/defaultClientSchema.ts +70 -0
- package/src/language-server/providers/schema/__tests__/file.ts +191 -0
- package/src/language-server/providers/schema/base.ts +15 -0
- package/src/language-server/providers/schema/endpoint.ts +138 -0
- package/src/language-server/providers/schema/engine.ts +204 -0
- package/src/language-server/providers/schema/file.ts +176 -0
- package/src/language-server/providers/schema/index.ts +59 -0
- package/src/language-server/server.ts +274 -0
- package/src/language-server/typings/graphql.d.ts +27 -0
- package/src/language-server/utilities/__tests__/graphql.test.ts +399 -0
- package/src/language-server/utilities/__tests__/uri.ts +55 -0
- package/src/language-server/utilities/debouncer.ts +8 -0
- package/src/language-server/utilities/debug.ts +90 -0
- package/src/language-server/utilities/graphql.ts +433 -0
- package/src/language-server/utilities/index.ts +3 -0
- package/src/language-server/utilities/source.ts +182 -0
- package/src/language-server/utilities/uri.ts +19 -0
- package/src/language-server/workspace.ts +254 -0
- package/src/languageServerClient.ts +22 -15
- package/src/messages.ts +75 -0
- package/src/statusBar.ts +5 -5
- package/src/tools/__tests__/buildServiceDefinition.test.ts +491 -0
- package/src/tools/__tests__/snapshotSerializers/astSerializer.ts +19 -0
- package/src/tools/__tests__/snapshotSerializers/graphQLTypeSerializer.ts +14 -0
- package/src/tools/buildServiceDefinition.ts +241 -0
- package/src/tools/index.ts +6 -0
- package/src/tools/schema/index.ts +2 -0
- package/src/tools/schema/resolveObject.ts +18 -0
- package/src/tools/schema/resolverMap.ts +23 -0
- package/src/tools/utilities/graphql.ts +22 -0
- package/src/tools/utilities/index.ts +3 -0
- package/src/tools/utilities/invariant.ts +5 -0
- package/src/tools/utilities/predicates.ts +5 -0
- package/src/utils.ts +7 -21
- package/syntaxes/graphql.dart.json +2 -4
- package/syntaxes/graphql.ex.json +1 -4
- package/syntaxes/graphql.js.json +3 -3
- package/syntaxes/graphql.json +13 -9
- package/syntaxes/graphql.lua.json +51 -0
- package/syntaxes/graphql.rb.json +1 -1
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +22 -7
- package/create-server-symlink.js +0 -8
- package/lib/debug.d.ts +0 -11
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js +0 -48
- package/lib/debug.js.map +0 -1
- package/lib/extension.d.ts +0 -4
- package/lib/extension.d.ts.map +0 -1
- package/lib/extension.js +0 -187
- package/lib/extension.js.map +0 -1
- package/lib/languageServerClient.d.ts +0 -4
- package/lib/languageServerClient.d.ts.map +0 -1
- package/lib/languageServerClient.js +0 -57
- package/lib/languageServerClient.js.map +0 -1
- package/lib/statusBar.d.ts +0 -24
- package/lib/statusBar.d.ts.map +0 -1
- package/lib/statusBar.js +0 -46
- package/lib/statusBar.js.map +0 -1
- package/lib/testRunner/index.d.ts +0 -3
- package/lib/testRunner/index.d.ts.map +0 -1
- package/lib/testRunner/index.js +0 -49
- package/lib/testRunner/index.js.map +0 -1
- package/lib/testRunner/jest-config.d.ts +0 -14
- package/lib/testRunner/jest-config.d.ts.map +0 -1
- package/lib/testRunner/jest-config.js +0 -18
- package/lib/testRunner/jest-config.js.map +0 -1
- package/lib/testRunner/jest-vscode-environment.d.ts +0 -2
- package/lib/testRunner/jest-vscode-environment.d.ts.map +0 -1
- package/lib/testRunner/jest-vscode-environment.js +0 -19
- package/lib/testRunner/jest-vscode-environment.js.map +0 -1
- package/lib/testRunner/jest-vscode-framework-setup.d.ts +0 -1
- package/lib/testRunner/jest-vscode-framework-setup.d.ts.map +0 -1
- package/lib/testRunner/jest-vscode-framework-setup.js +0 -3
- package/lib/testRunner/jest-vscode-framework-setup.js.map +0 -1
- package/lib/testRunner/vscode-test-script.d.ts +0 -2
- package/lib/testRunner/vscode-test-script.d.ts.map +0 -1
- package/lib/testRunner/vscode-test-script.js +0 -23
- package/lib/testRunner/vscode-test-script.js.map +0 -1
- package/lib/utils.d.ts +0 -18
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -52
- package/lib/utils.js.map +0 -1
- package/src/testRunner/README.md +0 -23
- package/src/testRunner/index.ts +0 -72
- package/src/testRunner/jest-config.ts +0 -17
- package/src/testRunner/jest-vscode-environment.ts +0 -25
- package/src/testRunner/jest-vscode-framework-setup.ts +0 -10
- package/src/testRunner/jest.d.ts +0 -37
- package/src/testRunner/vscode-test-script.ts +0 -38
- package/tsconfig.test.json +0 -4
- package/tsconfig.tsbuildinfo +0 -2486
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { GraphQLError } from "graphql";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
// ToolError is used for errors that are part of the expected flow
|
|
5
|
+
// and for which a stack trace should not be printed
|
|
6
|
+
|
|
7
|
+
export class ToolError extends Error {
|
|
8
|
+
name: string = "ToolError";
|
|
9
|
+
|
|
10
|
+
constructor(message: string) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.message = message;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isRunningFromXcodeScript = process.env.XCODE_VERSION_ACTUAL;
|
|
17
|
+
|
|
18
|
+
export function logError(error: Error) {
|
|
19
|
+
if (error instanceof ToolError) {
|
|
20
|
+
logErrorMessage(error.message);
|
|
21
|
+
} else if (error instanceof GraphQLError) {
|
|
22
|
+
const fileName = error.source && error.source.name;
|
|
23
|
+
if (error.locations) {
|
|
24
|
+
for (const location of error.locations) {
|
|
25
|
+
logErrorMessage(error.message, fileName, location.line);
|
|
26
|
+
}
|
|
27
|
+
} else {
|
|
28
|
+
logErrorMessage(error.message, fileName);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
console.error(error.stack);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function logErrorMessage(
|
|
36
|
+
message: string,
|
|
37
|
+
fileName?: string,
|
|
38
|
+
lineNumber?: number,
|
|
39
|
+
) {
|
|
40
|
+
if (isRunningFromXcodeScript) {
|
|
41
|
+
if (fileName && lineNumber) {
|
|
42
|
+
// Prefixing error output with file name, line and 'error: ',
|
|
43
|
+
// so Xcode will associate it with the right file and display the error inline
|
|
44
|
+
console.error(`${fileName}:${lineNumber}: error: ${message}`);
|
|
45
|
+
} else {
|
|
46
|
+
// Prefixing error output with 'error: ', so Xcode will display it as an error
|
|
47
|
+
console.error(`error: ${message}`);
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
if (fileName) {
|
|
51
|
+
const truncatedFileName =
|
|
52
|
+
"/" + fileName.split(path.sep).slice(-4).join(path.sep);
|
|
53
|
+
console.error(`...${truncatedFileName}: ${message}`);
|
|
54
|
+
} else {
|
|
55
|
+
console.error(`error: ${message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
specifiedRules,
|
|
3
|
+
NoUnusedFragmentsRule,
|
|
4
|
+
GraphQLError,
|
|
5
|
+
FieldNode,
|
|
6
|
+
ValidationContext,
|
|
7
|
+
GraphQLSchema,
|
|
8
|
+
DocumentNode,
|
|
9
|
+
OperationDefinitionNode,
|
|
10
|
+
TypeInfo,
|
|
11
|
+
FragmentDefinitionNode,
|
|
12
|
+
visit,
|
|
13
|
+
visitWithTypeInfo,
|
|
14
|
+
visitInParallel,
|
|
15
|
+
getLocation,
|
|
16
|
+
InlineFragmentNode,
|
|
17
|
+
Kind,
|
|
18
|
+
isObjectType,
|
|
19
|
+
} from "graphql";
|
|
20
|
+
|
|
21
|
+
import { TextEdit } from "vscode-languageserver/node";
|
|
22
|
+
|
|
23
|
+
import { ToolError, logError } from "./logger";
|
|
24
|
+
import { ValidationRule } from "graphql/validation/ValidationContext";
|
|
25
|
+
import { positionFromSourceLocation } from "../utilities/source";
|
|
26
|
+
import {
|
|
27
|
+
buildExecutionContext,
|
|
28
|
+
ExecutionContext,
|
|
29
|
+
} from "graphql/execution/execute";
|
|
30
|
+
import { hasClientDirective, simpleCollectFields } from "../utilities/graphql";
|
|
31
|
+
import { Debug } from "../utilities";
|
|
32
|
+
|
|
33
|
+
export interface CodeActionInfo {
|
|
34
|
+
message: string;
|
|
35
|
+
edits: TextEdit[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const specifiedRulesToBeRemoved: ValidationRule[] = [NoUnusedFragmentsRule];
|
|
39
|
+
|
|
40
|
+
export const defaultValidationRules: ValidationRule[] = [
|
|
41
|
+
NoAnonymousQueries,
|
|
42
|
+
NoTypenameAlias,
|
|
43
|
+
NoMissingClientDirectives,
|
|
44
|
+
...specifiedRules.filter((rule) => !specifiedRulesToBeRemoved.includes(rule)),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
export function getValidationErrors(
|
|
48
|
+
schema: GraphQLSchema,
|
|
49
|
+
document: DocumentNode,
|
|
50
|
+
fragments?: { [fragmentName: string]: FragmentDefinitionNode },
|
|
51
|
+
rules: ValidationRule[] = defaultValidationRules,
|
|
52
|
+
) {
|
|
53
|
+
const typeInfo = new TypeInfo(schema);
|
|
54
|
+
|
|
55
|
+
// The 4th argument to `ValidationContext` is an `onError` callback. This was
|
|
56
|
+
// introduced by https://github.com/graphql/graphql-js/pull/2074 and first
|
|
57
|
+
// published in graphql@14.5.0. It is meant to replace the `getErrors` method
|
|
58
|
+
// which was previously used. Since we support versions of graphql older than
|
|
59
|
+
// that, it's possible that this callback will not be invoked and we'll need
|
|
60
|
+
// to resort to using `getErrors`. Therefore, although we'll collect errors
|
|
61
|
+
// via this callback, if `getErrors` is present on the context we create,
|
|
62
|
+
// we'll go ahead and use that instead.
|
|
63
|
+
const errors: GraphQLError[] = [];
|
|
64
|
+
const onError = (err: GraphQLError) => errors.push(err);
|
|
65
|
+
const context = new ValidationContext(schema, document, typeInfo, onError);
|
|
66
|
+
|
|
67
|
+
if (fragments) {
|
|
68
|
+
(context as any)._fragments = fragments;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const visitors = rules.map((rule) => rule(context));
|
|
72
|
+
// Visit the whole document with each instance of all provided rules.
|
|
73
|
+
visit(document, visitWithTypeInfo(typeInfo, visitInParallel(visitors)));
|
|
74
|
+
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
// `getErrors` is gone in `graphql@15`, but we still support older versions.
|
|
77
|
+
if (typeof context.getErrors === "function") return context.getErrors();
|
|
78
|
+
|
|
79
|
+
// If `getErrors` doesn't exist, we must be on a `graphql@15` or higher,
|
|
80
|
+
// so we'll use the errors we collected via the `onError` callback.
|
|
81
|
+
return errors;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function validateQueryDocument(
|
|
85
|
+
schema: GraphQLSchema,
|
|
86
|
+
document: DocumentNode,
|
|
87
|
+
) {
|
|
88
|
+
try {
|
|
89
|
+
const validationErrors = getValidationErrors(schema, document);
|
|
90
|
+
if (validationErrors && validationErrors.length > 0) {
|
|
91
|
+
for (const error of validationErrors) {
|
|
92
|
+
logError(error);
|
|
93
|
+
}
|
|
94
|
+
return Debug.error("Validation of GraphQL query document failed");
|
|
95
|
+
}
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.error(e);
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function NoAnonymousQueries(context: ValidationContext) {
|
|
103
|
+
return {
|
|
104
|
+
OperationDefinition(node: OperationDefinitionNode) {
|
|
105
|
+
if (!node.name) {
|
|
106
|
+
context.reportError(
|
|
107
|
+
new GraphQLError("Apollo does not support anonymous operations", [
|
|
108
|
+
node,
|
|
109
|
+
]),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function NoTypenameAlias(context: ValidationContext) {
|
|
118
|
+
return {
|
|
119
|
+
Field(node: FieldNode) {
|
|
120
|
+
const aliasName = node.alias && node.alias.value;
|
|
121
|
+
if (aliasName == "__typename") {
|
|
122
|
+
context.reportError(
|
|
123
|
+
new GraphQLError(
|
|
124
|
+
"Apollo needs to be able to insert __typename when needed, please do not use it as an alias",
|
|
125
|
+
[node],
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function hasClientSchema(schema: GraphQLSchema): boolean {
|
|
134
|
+
const query = schema.getQueryType();
|
|
135
|
+
const mutation = schema.getMutationType();
|
|
136
|
+
const subscription = schema.getSubscriptionType();
|
|
137
|
+
|
|
138
|
+
return Boolean(
|
|
139
|
+
(query && query.clientSchema) ||
|
|
140
|
+
(mutation && mutation.clientSchema) ||
|
|
141
|
+
(subscription && subscription.clientSchema),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function NoMissingClientDirectives(context: ValidationContext) {
|
|
146
|
+
const root = context.getDocument();
|
|
147
|
+
const schema = context.getSchema();
|
|
148
|
+
// early return if we don't have any client fields on the schema
|
|
149
|
+
if (!hasClientSchema(schema)) return {};
|
|
150
|
+
|
|
151
|
+
// this isn't really execution context, but it does group the fragments and operations
|
|
152
|
+
// together correctly
|
|
153
|
+
// XXX we have a simplified version of this in @apollo/gateway that we could probably use
|
|
154
|
+
// intead of this
|
|
155
|
+
const executionContext = buildExecutionContext({
|
|
156
|
+
schema,
|
|
157
|
+
document: root,
|
|
158
|
+
rootValue: Object.create(null),
|
|
159
|
+
contextValue: Object.create(null),
|
|
160
|
+
});
|
|
161
|
+
function visitor(
|
|
162
|
+
node: FieldNode | InlineFragmentNode | FragmentDefinitionNode,
|
|
163
|
+
) {
|
|
164
|
+
// In cases where we are looking at a FragmentDefinition, there is no parent type
|
|
165
|
+
// but instead, the FragmentDefinition contains the type that we can read from the
|
|
166
|
+
// schema
|
|
167
|
+
const parentType =
|
|
168
|
+
node.kind === Kind.FRAGMENT_DEFINITION
|
|
169
|
+
? schema.getType(node.typeCondition.name.value)
|
|
170
|
+
: context.getParentType();
|
|
171
|
+
|
|
172
|
+
const fieldDef = context.getFieldDef();
|
|
173
|
+
|
|
174
|
+
// if we don't have a type to check then we can early return
|
|
175
|
+
if (!parentType) return;
|
|
176
|
+
|
|
177
|
+
// here we collect all of the fields on a type that are marked "local"
|
|
178
|
+
const clientFields =
|
|
179
|
+
parentType &&
|
|
180
|
+
isObjectType(parentType) &&
|
|
181
|
+
parentType.clientSchema &&
|
|
182
|
+
parentType.clientSchema.localFields;
|
|
183
|
+
|
|
184
|
+
// XXXX in the case of a fragment spread, the directive could be on the fragment definition
|
|
185
|
+
let clientDirectivePresent = hasClientDirective(node);
|
|
186
|
+
|
|
187
|
+
let message = "@client directive is missing on ";
|
|
188
|
+
let selectsClientFieldSet = false;
|
|
189
|
+
switch (node.kind) {
|
|
190
|
+
case Kind.FIELD:
|
|
191
|
+
// fields are simple because we can just see if the name exists in the local fields
|
|
192
|
+
// array on the parent type
|
|
193
|
+
selectsClientFieldSet = Boolean(
|
|
194
|
+
clientFields && clientFields.includes(fieldDef?.name || ""),
|
|
195
|
+
);
|
|
196
|
+
message += `local field "${node.name.value}"`;
|
|
197
|
+
break;
|
|
198
|
+
case Kind.INLINE_FRAGMENT:
|
|
199
|
+
case Kind.FRAGMENT_DEFINITION:
|
|
200
|
+
// XXX why isn't this type checking below?
|
|
201
|
+
if (Array.isArray(executionContext)) break;
|
|
202
|
+
|
|
203
|
+
const fields = simpleCollectFields(
|
|
204
|
+
executionContext as ExecutionContext,
|
|
205
|
+
node.selectionSet,
|
|
206
|
+
Object.create(null),
|
|
207
|
+
Object.create(null),
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// once we have a list of fields on the fragment, we can compare them
|
|
211
|
+
// to the list of types. The fields within a fragment need to be a
|
|
212
|
+
// subset of the overall local fields types
|
|
213
|
+
const fieldNames = Object.entries(fields).map(([name]) => name);
|
|
214
|
+
selectsClientFieldSet = fieldNames.every(
|
|
215
|
+
(field) => clientFields && clientFields.includes(field),
|
|
216
|
+
);
|
|
217
|
+
message += `fragment ${
|
|
218
|
+
"name" in node ? `"${node.name.value}" ` : ""
|
|
219
|
+
}around local fields "${fieldNames.join(",")}"`;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// if the field's parent is part of the client schema and that type
|
|
224
|
+
// includes a field with the same name as this node, we can see
|
|
225
|
+
// if it has an @client directive to resolve locally
|
|
226
|
+
if (selectsClientFieldSet && !clientDirectivePresent) {
|
|
227
|
+
let extensions: { [key: string]: any } | null = null;
|
|
228
|
+
const name = "name" in node && node.name;
|
|
229
|
+
// TODO support code actions for inline fragments, fragment spreads, and fragment definitions
|
|
230
|
+
if (name && name.loc) {
|
|
231
|
+
let { source, end: locToInsertDirective } = name.loc;
|
|
232
|
+
if (
|
|
233
|
+
"arguments" in node &&
|
|
234
|
+
node.arguments &&
|
|
235
|
+
node.arguments.length !== 0
|
|
236
|
+
) {
|
|
237
|
+
// must insert directive after field arguments
|
|
238
|
+
const endOfArgs = source.body.indexOf(")", locToInsertDirective);
|
|
239
|
+
locToInsertDirective = endOfArgs + 1;
|
|
240
|
+
}
|
|
241
|
+
const codeAction: CodeActionInfo = {
|
|
242
|
+
message: `Add @client directive to "${name.value}"`,
|
|
243
|
+
edits: [
|
|
244
|
+
TextEdit.insert(
|
|
245
|
+
positionFromSourceLocation(
|
|
246
|
+
source,
|
|
247
|
+
getLocation(source, locToInsertDirective),
|
|
248
|
+
),
|
|
249
|
+
" @client",
|
|
250
|
+
),
|
|
251
|
+
],
|
|
252
|
+
};
|
|
253
|
+
extensions = { codeAction };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
context.reportError(
|
|
257
|
+
new GraphQLError(message, [node], null, null, null, null, extensions),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// if we have selected a client field, no need to continue to recurse
|
|
262
|
+
if (selectsClientFieldSet) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
InlineFragment: visitor,
|
|
270
|
+
FragmentDefinition: visitor,
|
|
271
|
+
Field: visitor,
|
|
272
|
+
// TODO support directives on FragmentSpread
|
|
273
|
+
};
|
|
274
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { minimatch } from "minimatch";
|
|
2
|
+
import { globSync } from "glob";
|
|
3
|
+
import { invariant } from "../tools";
|
|
4
|
+
import { URI } from "vscode-uri";
|
|
5
|
+
import { normalizeURI } from "./utilities";
|
|
6
|
+
import { resolve } from "path";
|
|
7
|
+
|
|
8
|
+
export class FileSet {
|
|
9
|
+
private rootURI: URI;
|
|
10
|
+
private includes: string[];
|
|
11
|
+
private excludes: string[];
|
|
12
|
+
|
|
13
|
+
constructor({
|
|
14
|
+
rootURI,
|
|
15
|
+
includes,
|
|
16
|
+
excludes,
|
|
17
|
+
configURI,
|
|
18
|
+
}: {
|
|
19
|
+
rootURI: URI;
|
|
20
|
+
includes: string[];
|
|
21
|
+
excludes: string[];
|
|
22
|
+
configURI?: URI;
|
|
23
|
+
}) {
|
|
24
|
+
invariant(rootURI, `Must provide "rootURI".`);
|
|
25
|
+
invariant(includes, `Must provide "includes".`);
|
|
26
|
+
invariant(excludes, `Must provide "excludes".`);
|
|
27
|
+
|
|
28
|
+
this.rootURI = rootURI;
|
|
29
|
+
this.includes = includes;
|
|
30
|
+
this.excludes = excludes;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
includesFile(filePath: string): boolean {
|
|
34
|
+
const normalizedFilePath = normalizeURI(filePath);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
this.includes.some((include) => {
|
|
38
|
+
return minimatch(
|
|
39
|
+
normalizedFilePath,
|
|
40
|
+
resolve(this.rootURI.fsPath, include),
|
|
41
|
+
);
|
|
42
|
+
}) &&
|
|
43
|
+
!this.excludes.some((exclude) => {
|
|
44
|
+
return minimatch(
|
|
45
|
+
normalizedFilePath,
|
|
46
|
+
resolve(this.rootURI.fsPath, exclude),
|
|
47
|
+
);
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
allFiles(): string[] {
|
|
53
|
+
// since glob.sync takes a single pattern, but we allow an array of `includes`, we can join all the
|
|
54
|
+
// `includes` globs into a single pattern and pass to glob.sync. The `ignore` option does, however, allow
|
|
55
|
+
// an array of globs to ignore, so we can pass it in directly
|
|
56
|
+
const joinedIncludes = `{${this.includes.join(",")}}`;
|
|
57
|
+
return globSync(joinedIncludes, {
|
|
58
|
+
cwd: this.rootURI.fsPath,
|
|
59
|
+
absolute: true,
|
|
60
|
+
ignore: this.excludes,
|
|
61
|
+
}).map(normalizeURI);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import moment from "moment";
|
|
2
|
+
|
|
3
|
+
export function formatMS(
|
|
4
|
+
ms: number,
|
|
5
|
+
d: number,
|
|
6
|
+
allowMicros = false,
|
|
7
|
+
allowNanos = true,
|
|
8
|
+
) {
|
|
9
|
+
if (ms === 0 || ms === null) return "0";
|
|
10
|
+
const bounds = [
|
|
11
|
+
moment.duration(1, "hour").asMilliseconds(),
|
|
12
|
+
moment.duration(1, "minute").asMilliseconds(),
|
|
13
|
+
moment.duration(1, "second").asMilliseconds(),
|
|
14
|
+
1,
|
|
15
|
+
0.001,
|
|
16
|
+
0.000001,
|
|
17
|
+
];
|
|
18
|
+
const units = ["hr", "min", "s", "ms", "μs", "ns"];
|
|
19
|
+
|
|
20
|
+
const makeSmallNumbersNice = (f: number) => {
|
|
21
|
+
if (f >= 100) return f.toFixed(0);
|
|
22
|
+
if (f >= 10) return f.toFixed(1);
|
|
23
|
+
if (f === 0) return "0";
|
|
24
|
+
return f.toFixed(2);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const bound = bounds.find((b) => b <= ms) || bounds[bounds.length - 1];
|
|
28
|
+
const boundIndex = bounds.indexOf(bound);
|
|
29
|
+
const unit = boundIndex >= 0 ? units[boundIndex] : "";
|
|
30
|
+
|
|
31
|
+
if ((unit === "μs" || unit === "ns") && !allowMicros) {
|
|
32
|
+
return "< 1ms";
|
|
33
|
+
}
|
|
34
|
+
if (unit === "ns" && !allowNanos) {
|
|
35
|
+
return "< 1µs";
|
|
36
|
+
}
|
|
37
|
+
const value =
|
|
38
|
+
typeof d !== "undefined"
|
|
39
|
+
? (ms / bound).toFixed(d)
|
|
40
|
+
: makeSmallNumbersNice(ms / bound);
|
|
41
|
+
|
|
42
|
+
// if something is rounded to 1000 and not reduced this will catch and reduce it
|
|
43
|
+
if ((value === "1000" || value === "1000.0") && boundIndex >= 1) {
|
|
44
|
+
return `1${units[boundIndex - 1]}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return `${value}${unit}`;
|
|
48
|
+
}
|