summaryrefslogtreecommitdiff
path: root/src/common.ts
blob: 328e1bd17de1da585ad47560151c3317dc8c5392 (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
/// import

import { graphql } from "npm:graphql@16.6.0";
import type { ExecutionResult } from "npm:graphql@16.6.0";

/// util

import type { GQLOptions, GQLRequest, GraphQLParams } from "./utility/types.ts";



/// export

export async function runHttpQuery<
  Req extends GQLRequest = GQLRequest,
  Context = { request?: Req }>(
    params: GraphQLParams,
    options: GQLOptions<Context, Req>,
    context?: Context | any): Promise<ExecutionResult> {
  /**
   * Execute a GraphQL query
   * @param {GraphQLParams} params
   * @param {GQLOptions} options
   * @param context GraphQL context to use inside resolvers
   *
   * @example
   * ```ts
   * const { errors, data } = await runHttpQuery<ServerRequest, typeof context>({ query: `{ hello }` }, { schema }}, context)
   * ```
   */

  const contextValue = options.context && context?.request ?
    await options.context?.(context?.request) :
    context;
  const source = params.query! || params.mutation!;

  return await graphql({
    source,
    ...options,
    contextValue,
    operationName: params.operationName,
    variableValues: params.variables
  });
}