Skip to content

File tree

94 files changed

+5715
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+5715
-15
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
"node": ">= 6.x"
2424
},
2525
"scripts": {
26-
"test": "npm run prettier:check && npm run lint && npm run check && npm run testonly",
27-
"test:ci": "yarn check --integrity && npm run prettier:check && npm run lint -- --no-cache && npm run check && npm run testonly:cover && npm run build",
26+
"test": "npm run prettier:check && npm run lint && npm run check && npm run testonly && npm run check:ts",
27+
"test:ci": "yarn check --integrity && npm run prettier:check && npm run lint -- --no-cache && npm run check && npm run testonly:cover && npm run check:ts && npm run build",
2828
"testonly": "mocha --full-trace src/**/__tests__/**/*-test.js",
2929
"testonly:cover": "nyc npm run testonly",
3030
"lint": "eslint --cache --report-unused-disable-directives src resources",
3131
"benchmark": "node --noconcurrent_sweeping --expose-gc --predictable ./resources/benchmark.js",
32-
"prettier": "prettier --ignore-path .gitignore --write --list-different \"**/*.{js,md,json,yml}\"",
33-
"prettier:check": "prettier --ignore-path .gitignore --check \"**/*.{js,md,json,yml}\"",
32+
"prettier": "prettier --ignore-path .gitignore --write --list-different \"**/*.{js,ts,md,json,yml}\"",
33+
"prettier:check": "prettier --ignore-path .gitignore --check \"**/*.{js,ts,md,json,yml}\"",
3434
"check": "flow check",
35+
"check:ts": "dtslint tstypes",
3536
"check:cover": "node resources/check-cover.js && nyc report --nycrc-path .nycflowrc.yml",
3637
"build": "node resources/build.js",
3738
"changelog": "node resources/gen-changelog.js",
@@ -50,6 +51,7 @@
5051
"@babel/register": "7.5.5",
5152
"babel-eslint": "10.0.2",
5253
"chai": "4.2.0",
54+
"dtslint": "^0.8.0",
5355
"eslint": "5.16.0",
5456
"eslint-plugin-flowtype": "3.12.1",
5557
"flow-bin": "0.105.2",

resources/build.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ if (require.main === module) {
2929
}
3030
}
3131

32+
const tsFiles = readdirRecursive('./tstypes', { ignoreDir: /^__.*__$/ });
33+
for (const filepath of tsFiles) {
34+
if (filepath.endsWith('.d.ts')) {
35+
const srcPath = path.join('./tstypes', filepath);
36+
const destPath = path.join('./dist', filepath);
37+
38+
copyFile(srcPath, destPath);
39+
}
40+
}
41+
3242
const packageJSON = buildPackageJSON();
3343
assert(
3444
packageJSON.version === require('../dist/version').version,

tstypes/error/GraphQLError.d.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Maybe from '../tsutils/Maybe';
2+
import { getLocation } from '../language';
3+
import { ASTNode } from '../language/ast';
4+
import { Source } from '../language/source';
5+
import { SourceLocation } from '../language/location';
6+
7+
/**
8+
* A GraphQLError describes an Error found during the parse, validate, or
9+
* execute phases of performing a GraphQL operation. In addition to a message
10+
* and stack trace, it also includes information about the locations in a
11+
* GraphQL document and/or execution result that correspond to the Error.
12+
*/
13+
export class GraphQLError extends Error {
14+
/**
15+
* A message describing the Error for debugging purposes.
16+
*
17+
* Enumerable, and appears in the result of JSON.stringify().
18+
*
19+
* Note: should be treated as readonly, despite invariant usage.
20+
*/
21+
message: string;
22+
23+
/**
24+
* An array of { line, column } locations within the source GraphQL document
25+
* which correspond to this error.
26+
*
27+
* Errors during validation often contain multiple locations, for example to
28+
* point out two things with the same name. Errors during execution include a
29+
* single location, the field which produced the error.
30+
*
31+
* Enumerable, and appears in the result of JSON.stringify().
32+
*/
33+
readonly locations: ReadonlyArray<SourceLocation> | undefined;
34+
35+
/**
36+
* An array describing the JSON-path into the execution response which
37+
* corresponds to this error. Only included for errors during execution.
38+
*
39+
* Enumerable, and appears in the result of JSON.stringify().
40+
*/
41+
readonly path: ReadonlyArray<string | number> | undefined;
42+
43+
/**
44+
* An array of GraphQL AST Nodes corresponding to this error.
45+
*/
46+
readonly nodes: ReadonlyArray<ASTNode> | undefined;
47+
48+
/**
49+
* The source GraphQL document corresponding to this error.
50+
*/
51+
readonly source: Source | undefined;
52+
53+
/**
54+
* An array of character offsets within the source GraphQL document
55+
* which correspond to this error.
56+
*/
57+
readonly positions: ReadonlyArray<number> | undefined;
58+
59+
/**
60+
* The original error thrown from a field resolver during execution.
61+
*/
62+
readonly originalError: Maybe<Error>;
63+
64+
/**
65+
* Extension fields to add to the formatted error.
66+
*/
67+
readonly extensions: { [key: string]: any } | undefined;
68+
69+
constructor(
70+
message: string,
71+
nodes?: ReadonlyArray<ASTNode> | ASTNode | undefined,
72+
source?: Maybe<Source>,
73+
positions?: Maybe<ReadonlyArray<number>>,
74+
path?: Maybe<ReadonlyArray<string | number>>,
75+
originalError?: Maybe<Error>,
76+
extensions?: Maybe<{ [key: string]: any }>,
77+
);
78+
}

tstypes/error/formatError.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { GraphQLError } from './GraphQLError';
2+
import { SourceLocation } from '../language/location';
3+
4+
/**
5+
* Given a GraphQLError, format it according to the rules described by the
6+
* Response Format, Errors section of the GraphQL Specification.
7+
*/
8+
export function formatError(error: GraphQLError): GraphQLFormattedError;
9+
10+
/**
11+
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
12+
*/
13+
export interface GraphQLFormattedError<
14+
TExtensions extends Record<string, any> = Record<string, any>
15+
> {
16+
/**
17+
* A short, human-readable summary of the problem that **SHOULD NOT** change
18+
* from occurrence to occurrence of the problem, except for purposes of
19+
* localization.
20+
*/
21+
readonly message: string;
22+
/**
23+
* If an error can be associated to a particular point in the requested
24+
* GraphQL document, it should contain a list of locations.
25+
*/
26+
readonly locations?: ReadonlyArray<SourceLocation>;
27+
/**
28+
* If an error can be associated to a particular field in the GraphQL result,
29+
* it _must_ contain an entry with the key `path` that details the path of
30+
* the response field which experienced the error. This allows clients to
31+
* identify whether a null result is intentional or caused by a runtime error.
32+
*/
33+
readonly path?: ReadonlyArray<string | number>;
34+
/**
35+
* Reserved for implementors to extend the protocol however they see fit,
36+
* and hence there are no additional restrictions on its contents.
37+
*/
38+
readonly extensions?: TExtensions;
39+
}

tstypes/error/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { GraphQLError } from './GraphQLError';
2+
export { syntaxError } from './syntaxError';
3+
export { locatedError } from './locatedError';
4+
export { printError } from './printError';
5+
export { formatError, GraphQLFormattedError } from './formatError';

tstypes/error/locatedError.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { GraphQLError } from './GraphQLError';
2+
import { ASTNode } from '../language/ast';
3+
4+
/**
5+
* Given an arbitrary Error, presumably thrown while attempting to execute a
6+
* GraphQL operation, produce a new GraphQLError aware of the location in the
7+
* document responsible for the original Error.
8+
*/
9+
export function locatedError(
10+
originalError: Error | GraphQLError,
11+
nodes: ReadonlyArray<ASTNode>,
12+
path: ReadonlyArray<string | number>,
13+
): GraphQLError;

tstypes/error/printError.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { GraphQLError } from './GraphQLError';
2+
3+
/**
4+
* Prints a GraphQLError to a string, representing useful location information
5+
* about the error's position in the source.
6+
*/
7+
export function printError(error: GraphQLError): string;

tstypes/error/syntaxError.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Source } from '../language/source';
2+
import { GraphQLError } from './GraphQLError';
3+
4+
/**
5+
* Produces a GraphQLError representing a syntax error, containing useful
6+
* descriptive information about the syntax error's position in the source.
7+
*/
8+
export function syntaxError(
9+
source: Source,
10+
position: number,
11+
description: string,
12+
): GraphQLError;

tstypes/execution/execute.d.ts

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import Maybe from '../tsutils/Maybe';
2+
import { GraphQLError, locatedError } from '../error';
3+
import { GraphQLSchema } from '../type/schema';
4+
import {
5+
GraphQLField,
6+
GraphQLFieldResolver,
7+
ResponsePath,
8+
GraphQLObjectType,
9+
GraphQLResolveInfo,
10+
} from '../type/definition';
11+
import {
12+
DirectiveNode,
13+
DocumentNode,
14+
OperationDefinitionNode,
15+
SelectionSetNode,
16+
FieldNode,
17+
InlineFragmentNode,
18+
FragmentDefinitionNode,
19+
} from '../language/ast';
20+
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
21+
22+
/**
23+
* Data that must be available at all points during query execution.
24+
*
25+
* Namely, schema of the type system that is currently executing,
26+
* and the fragments defined in the query document
27+
*/
28+
export interface ExecutionContext {
29+
schema: GraphQLSchema;
30+
fragments: { [key: string]: FragmentDefinitionNode };
31+
rootValue: any;
32+
contextValue: any;
33+
operation: OperationDefinitionNode;
34+
variableValues: { [key: string]: any };
35+
fieldResolver: GraphQLFieldResolver<any, any>;
36+
errors: GraphQLError[];
37+
}
38+
39+
export interface ExecutionResultDataDefault {
40+
[key: string]: any;
41+
}
42+
43+
/**
44+
* The result of GraphQL execution.
45+
*
46+
* - `errors` is included when any errors occurred as a non-empty array.
47+
* - `data` is the result of a successful execution of the query.
48+
*/
49+
export interface ExecutionResult<TData = ExecutionResultDataDefault> {
50+
errors?: ReadonlyArray<GraphQLError>;
51+
data?: TData;
52+
}
53+
54+
export type ExecutionArgs = {
55+
schema: GraphQLSchema;
56+
document: DocumentNode;
57+
rootValue?: any;
58+
contextValue?: any;
59+
variableValues?: Maybe<{ [key: string]: any }>;
60+
operationName?: Maybe<string>;
61+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
62+
};
63+
64+
/**
65+
* Implements the "Evaluating requests" section of the GraphQL specification.
66+
*
67+
* Returns either a synchronous ExecutionResult (if all encountered resolvers
68+
* are synchronous), or a Promise of an ExecutionResult that will eventually be
69+
* resolved and never rejected.
70+
*
71+
* If the arguments to this function do not result in a legal execution context,
72+
* a GraphQLError will be thrown immediately explaining the invalid input.
73+
*
74+
* Accepts either an object with named arguments, or individual arguments.
75+
*/
76+
export function execute<TData = ExecutionResultDataDefault>(
77+
args: ExecutionArgs,
78+
): PromiseOrValue<ExecutionResult<TData>>;
79+
export function execute<TData = ExecutionResultDataDefault>(
80+
schema: GraphQLSchema,
81+
document: DocumentNode,
82+
rootValue?: any,
83+
contextValue?: any,
84+
variableValues?: Maybe<{ [key: string]: any }>,
85+
operationName?: Maybe<string>,
86+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
87+
): PromiseOrValue<ExecutionResult<TData>>;
88+
89+
/**
90+
* Given a ResponsePath (found in the `path` entry in the information provided
91+
* as the last argument to a field resolver), return an Array of the path keys.
92+
*/
93+
export function responsePathAsArray(
94+
path: ResponsePath,
95+
): ReadonlyArray<string | number>;
96+
97+
/**
98+
* Given a ResponsePath and a key, return a new ResponsePath containing the
99+
* new key.
100+
101+
*/
102+
export function addPath(
103+
prev: ResponsePath | undefined,
104+
key: string | number,
105+
): { prev: ResponsePath | undefined; key: string | number };
106+
107+
/**
108+
* Essential assertions before executing to provide developer feedback for
109+
* improper use of the GraphQL library.
110+
*/
111+
export function assertValidExecutionArguments(
112+
schema: GraphQLSchema,
113+
document: DocumentNode,
114+
rawVariableValues: Maybe<{ [key: string]: any }>,
115+
): void;
116+
117+
/**
118+
* Constructs a ExecutionContext object from the arguments passed to
119+
* execute, which we will pass throughout the other execution methods.
120+
*
121+
* Throws a GraphQLError if a valid execution context cannot be created.
122+
*/
123+
export function buildExecutionContext(
124+
schema: GraphQLSchema,
125+
document: DocumentNode,
126+
rootValue: any,
127+
contextValue: any,
128+
rawVariableValues: Maybe<{ [key: string]: any }>,
129+
operationName: Maybe<string>,
130+
fieldResolver: Maybe<GraphQLFieldResolver<any, any>>,
131+
): ReadonlyArray<GraphQLError> | ExecutionContext;
132+
133+
/**
134+
* Given a selectionSet, adds all of the fields in that selection to
135+
* the passed in map of fields, and returns it at the end.
136+
*
137+
* CollectFields requires the "runtime type" of an object. For a field which
138+
* returns an Interface or Union type, the "runtime type" will be the actual
139+
* Object type returned by that field.
140+
*/
141+
export function collectFields(
142+
exeContext: ExecutionContext,
143+
runtimeType: GraphQLObjectType,
144+
selectionSet: SelectionSetNode,
145+
fields: { [key: string]: Array<FieldNode> },
146+
visitedFragmentNames: { [key: string]: boolean },
147+
): { [key: string]: Array<FieldNode> };
148+
149+
export function buildResolveInfo(
150+
exeContext: ExecutionContext,
151+
fieldDef: GraphQLField<any, any>,
152+
fieldNodes: ReadonlyArray<FieldNode>,
153+
parentType: GraphQLObjectType,
154+
path: ResponsePath,
155+
): GraphQLResolveInfo;
156+
157+
// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
158+
// function. Returns the result of resolveFn or the abrupt-return Error object.
159+
export function resolveFieldValueOrError<TSource>(
160+
exeContext: ExecutionContext,
161+
fieldDef: GraphQLField<TSource, any>,
162+
fieldNodes: ReadonlyArray<FieldNode>,
163+
resolveFn: GraphQLFieldResolver<TSource, any>,
164+
source: TSource,
165+
info: GraphQLResolveInfo,
166+
): Error | any;
167+
168+
/**
169+
* If a resolve function is not given, then a default resolve behavior is used
170+
* which takes the property of the source object of the same name as the field
171+
* and returns it as the result, or if it's a function, returns the result
172+
* of calling that function while passing along args and context.
173+
*/
174+
export const defaultFieldResolver: GraphQLFieldResolver<any, any>;
175+
176+
/**
177+
* This method looks up the field on the given type defintion.
178+
* It has special casing for the two introspection fields, __schema
179+
* and __typename. __typename is special because it can always be
180+
* queried as a field, even in situations where no other fields
181+
* are allowed, like on a Union. __schema could get automatically
182+
* added to the query type, but that would require mutating type
183+
* definitions, which would cause issues.
184+
*/
185+
export function getFieldDef(
186+
schema: GraphQLSchema,
187+
parentType: GraphQLObjectType,
188+
fieldName: string,
189+
): Maybe<GraphQLField<any, any>>;

0 commit comments

Comments
 (0)