aboutsummaryrefslogtreecommitdiff
path: root/source/utility/types.ts
blob: 0d8b443397941f3f64d1b647fa6d3e582bc83d5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*** IMPORT ------------------------------------------- ***/

import type { GraphQLArgs, GraphQLSchema } from "graphql";

/*** UTILITY ------------------------------------------ ***/

import type { RenderPageOptions } from "../graphiql/render.ts";

interface MutationParams {
  mutation: string;
  operationName?: string;
  query?: never;
  variables?: Record<string, unknown>;
}

interface QueryParams {
  mutation?: never;
  operationName?: string;
  query: string;
  variables?: Record<string, unknown>;
}

/*** EXPORT ------------------------------------------- ***/

/**
 * Configuration accepted by `GraphQLHTTP` and `runHttpQuery`.
 *
 * Extends `GraphQLArgs` from `graphql-js` minus `source` (supplied per request).
 */
export interface GQLOptions<Context, Req extends GQLRequest = GQLRequest> extends Omit<GraphQLArgs, "source"> {
  /** Builds the context value passed to resolvers. Runs per request. */
  context?: (val: Req) => Context | Promise<Context>;
  /** Serve the GraphQL Playground on `GET` + `Accept: text/html`. */
  graphiql?: boolean;
  /** Extra headers merged into every response. */
  headers?: HeadersInit;
  /** Passthrough options for the Playground renderer (minus `endpoint`). */
  playgroundOptions?: Omit<RenderPageOptions, "endpoint">;
  /** The executable schema to query against. */
  schema: GraphQLSchema;
}

/** A single GraphQL operation — either a `query` or a `mutation`, never both. */
export type GraphQLParams = QueryParams | MutationParams;

/** Minimal Fetch-shaped request accepted by the HTTP handler. */
export type GQLRequest = {
  headers: Headers;
  json: () => Promise<GraphQLParams>;
  method: string;
  url: string;
};