UNPKG

5.06 kBTypeScriptView Raw
1// TypeScript Version: 3.4
2
3import { IncomingMessage, ServerResponse } from 'http';
4
5import {
6 Source,
7 ASTVisitor,
8 DocumentNode,
9 ValidationRule,
10 ValidationContext,
11 ExecutionArgs,
12 ExecutionResult,
13 GraphQLError,
14 GraphQLSchema,
15 GraphQLFieldResolver,
16 GraphQLTypeResolver,
17} from 'graphql';
18
19import { GraphiQLOptions } from './renderGraphiQL';
20
21export {};
22
23type Request = IncomingMessage;
24
25type Response = ServerResponse & { json?: (data: unknown) => void };
26type MaybePromise<T> = Promise<T> | T;
27
28type Middleware = (request: Request, response: Response) => Promise<void>;
29
30/**
31 * Used to configure the graphqlHTTP middleware by providing a schema
32 * and other configuration options.
33 *
34 * Options can be provided as an Object, a Promise for an Object, or a Function
35 * that returns an Object or a Promise for an Object.
36 */
37export type Options =
38 | ((
39 request: Request,
40 response: Response,
41 params?: GraphQLParams,
42 ) => MaybePromise<OptionsData>)
43 | MaybePromise<OptionsData>;
44
45export interface OptionsData {
46 /**
47 * A GraphQL schema from graphql-js.
48 */
49 schema: GraphQLSchema;
50
51 /**
52 * A value to pass as the context to the graphql() function.
53 */
54 context?: unknown;
55
56 /**
57 * An object to pass as the rootValue to the graphql() function.
58 */
59 rootValue?: unknown;
60
61 /**
62 * A boolean to configure whether the output should be pretty-printed.
63 */
64 pretty?: boolean;
65
66 /**
67 * An optional array of validation rules that will be applied on the document
68 * in additional to those defined by the GraphQL spec.
69 */
70 validationRules?: ReadonlyArray<(ctx: ValidationContext) => ASTVisitor>;
71
72 /**
73 * An optional function which will be used to validate instead of default `validate`
74 * from `graphql-js`.
75 */
76 customValidateFn?: (
77 schema: GraphQLSchema,
78 documentAST: DocumentNode,
79 rules: ReadonlyArray<ValidationRule>,
80 ) => ReadonlyArray<GraphQLError>;
81
82 /**
83 * An optional function which will be used to execute instead of default `execute`
84 * from `graphql-js`.
85 */
86 customExecuteFn?: (args: ExecutionArgs) => MaybePromise<ExecutionResult>;
87
88 /**
89 * An optional function which will be used to format any errors produced by
90 * fulfilling a GraphQL operation. If no function is provided, GraphQL's
91 * default spec-compliant `formatError` function will be used.
92 */
93 customFormatErrorFn?: (error: GraphQLError) => unknown;
94
95 /**
96 * An optional function which will be used to create a document instead of
97 * the default `parse` from `graphql-js`.
98 */
99 customParseFn?: (source: Source) => DocumentNode;
100
101 /**
102 * `formatError` is deprecated and replaced by `customFormatErrorFn`. It will
103 * be removed in version 1.0.0.
104 */
105 formatError?: (error: GraphQLError) => unknown;
106
107 /**
108 * An optional function for adding additional metadata to the GraphQL response
109 * as a key-value object. The result will be added to "extensions" field in
110 * the resulting JSON. This is often a useful place to add development time
111 * info such as the runtime of a query or the amount of resources consumed.
112 *
113 * Information about the request is provided to be used.
114 *
115 * This function may be async.
116 */
117 extensions?: (
118 info: RequestInfo,
119 ) => MaybePromise<undefined | { [key: string]: unknown }>;
120
121 /**
122 * A boolean to optionally enable GraphiQL mode.
123 * Alternatively, instead of `true` you can pass in an options object.
124 */
125 graphiql?: boolean | GraphiQLOptions;
126
127 /**
128 * A resolver function to use when one is not provided by the schema.
129 * If not provided, the default field resolver is used (which looks for a
130 * value or method on the source value with the field's name).
131 */
132 fieldResolver?: GraphQLFieldResolver<unknown, unknown>;
133
134 /**
135 * A type resolver function to use when none is provided by the schema.
136 * If not provided, the default type resolver is used (which looks for a
137 * `__typename` field or alternatively calls the `isTypeOf` method).
138 */
139 typeResolver?: GraphQLTypeResolver<unknown, unknown>;
140}
141
142/**
143 * All information about a GraphQL request.
144 */
145export interface RequestInfo {
146 /**
147 * The parsed GraphQL document.
148 */
149 document: DocumentNode;
150
151 /**
152 * The variable values used at runtime.
153 */
154 variables: { readonly [name: string]: unknown } | null;
155
156 /**
157 * The (optional) operation name requested.
158 */
159 operationName: string | null;
160
161 /**
162 * The result of executing the operation.
163 */
164 result: ExecutionResult;
165
166 /**
167 * A value to pass as the context to the graphql() function.
168 */
169 context?: unknown;
170}
171
172/**
173 * Middleware for express; takes an options object or function as input to
174 * configure behavior, and returns an express middleware.
175 */
176export function graphqlHTTP(options: Options): Middleware;
177
178export interface GraphQLParams {
179 query: string | null;
180 variables: { readonly [name: string]: unknown } | null;
181 operationName: string | null;
182 raw: boolean;
183}
184
185export function getGraphQLParams(request: Request): Promise<GraphQLParams>;