/*** IMPORT ------------------------------------------- ***/ import { graphql, type ExecutionResult } from "graphql"; /*** UTILITY ------------------------------------------ ***/ import type { GQLOptions, GQLRequest, GraphQLParams } from "./utility/types.ts"; /*** EXPORT ------------------------------------------- ***/ /** * Executes a GraphQL query or mutation against the provided schema. * * Resolves `options.context` (if given) with the incoming request, then * forwards `query`/`mutation`, `operationName`, and `variables` to `graphql()`. * * @typeParam Req - Request shape; defaults to {@link GQLRequest}. * @typeParam Ctx - Context value passed to resolvers; defaults to `{ request }`. * @param params - Parsed GraphQL params (query or mutation, with optional vars). * @param options - Executable schema plus optional context builder. * @param request - The inbound HTTP request (used to build context). * @returns The raw {@link ExecutionResult} from `graphql-js`. */ export async function runHttpQuery< Req extends GQLRequest = GQLRequest, Ctx = { request: Req }>( params: GraphQLParams, options: GQLOptions, request: Req): Promise { const contextValue = options.context ? await options.context(request) : { request } as Ctx; const source = params.query! || params.mutation!; return await graphql({ source, ...options, contextValue, operationName: params.operationName, variableValues: params.variables }); }