From 4c6194c4c2b5506f6d482347b0c13033ef17b5c7 Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Fri, 24 Apr 2026 07:43:33 -0700 Subject: initial commit --- source/common.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 source/common.ts (limited to 'source/common.ts') diff --git a/source/common.ts b/source/common.ts new file mode 100755 index 0000000..902bdd0 --- /dev/null +++ b/source/common.ts @@ -0,0 +1,49 @@ + + + +/*** 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 + }); +} -- cgit v1.2.3