index.d.ts 5.0 KB

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