UNPKG

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