subscribe.d.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Maybe from '../tsutils/Maybe';
  2. import { DocumentNode } from '../language/ast';
  3. import {
  4. ExecutionResult,
  5. ExecutionResultDataDefault,
  6. } from '../execution/execute';
  7. import { GraphQLSchema } from '../type/schema';
  8. import { GraphQLFieldResolver } from '../type/definition';
  9. export interface SubscriptionArgs {
  10. schema: GraphQLSchema;
  11. document: DocumentNode;
  12. rootValue?: any;
  13. contextValue?: any;
  14. variableValues?: Maybe<Record<string, any>>;
  15. operationName?: Maybe<string>;
  16. fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
  17. subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
  18. }
  19. /**
  20. * Implements the "Subscribe" algorithm described in the GraphQL specification.
  21. *
  22. * Returns a Promise which resolves to either an AsyncIterator (if successful)
  23. * or an ExecutionResult (client error). The promise will be rejected if a
  24. * server error occurs.
  25. *
  26. * If the client-provided arguments to this function do not result in a
  27. * compliant subscription, a GraphQL Response (ExecutionResult) with
  28. * descriptive errors and no data will be returned.
  29. *
  30. * If the the source stream could not be created due to faulty subscription
  31. * resolver logic or underlying systems, the promise will resolve to a single
  32. * ExecutionResult containing `errors` and no `data`.
  33. *
  34. * If the operation succeeded, the promise resolves to an AsyncIterator, which
  35. * yields a stream of ExecutionResults representing the response stream.
  36. *
  37. * Accepts either an object with named arguments, or individual arguments.
  38. */
  39. export function subscribe<TData = ExecutionResultDataDefault>(
  40. args: SubscriptionArgs,
  41. ): Promise<
  42. AsyncIterableIterator<ExecutionResult<TData>> | ExecutionResult<TData>
  43. >;
  44. export function subscribe<TData = ExecutionResultDataDefault>(
  45. schema: GraphQLSchema,
  46. document: DocumentNode,
  47. rootValue?: any,
  48. contextValue?: any,
  49. variableValues?: Maybe<{ [key: string]: any }>,
  50. operationName?: Maybe<string>,
  51. fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
  52. subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
  53. ): Promise<
  54. AsyncIterableIterator<ExecutionResult<TData>> | ExecutionResult<TData>
  55. >;
  56. /**
  57. * Implements the "CreateSourceEventStream" algorithm described in the
  58. * GraphQL specification, resolving the subscription source event stream.
  59. *
  60. * Returns a Promise<AsyncIterable>.
  61. *
  62. * If the client-provided invalid arguments, the source stream could not be
  63. * created, or the resolver did not return an AsyncIterable, this function will
  64. * will throw an error, which should be caught and handled by the caller.
  65. *
  66. * A Source Event Stream represents a sequence of events, each of which triggers
  67. * a GraphQL execution for that event.
  68. *
  69. * This may be useful when hosting the stateful subscription service in a
  70. * different process or machine than the stateless GraphQL execution engine,
  71. * or otherwise separating these two steps. For more on this, see the
  72. * "Supporting Subscriptions at Scale" information in the GraphQL specification.
  73. */
  74. export function createSourceEventStream<TData = ExecutionResultDataDefault>(
  75. schema: GraphQLSchema,
  76. document: DocumentNode,
  77. rootValue?: any,
  78. contextValue?: any,
  79. variableValues?: { [key: string]: any },
  80. operationName?: Maybe<string>,
  81. fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
  82. ): Promise<AsyncIterable<any> | ExecutionResult<TData>>;