index.d.ts 5.3 KB

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