From ca16dcf93922d63dd1783dc471b2ac0c61f8d11f Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 14:19:10 -0700 Subject: initial commit --- src/common.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 src/common.ts (limited to 'src/common.ts') diff --git a/src/common.ts b/src/common.ts new file mode 100755 index 0000000..328e1bd --- /dev/null +++ b/src/common.ts @@ -0,0 +1,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?: Context | any): Promise { + /** + * 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({ 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 + }); +} -- cgit v1.2.3