diff options
Diffstat (limited to 'source/common.ts')
| -rwxr-xr-x | source/common.ts | 49 |
1 files changed, 49 insertions, 0 deletions
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<Ctx, Req>, + request: Req): Promise<ExecutionResult> { + 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 + }); +} |