summaryrefslogtreecommitdiff
path: root/src/common.ts
diff options
context:
space:
mode:
authornetop://ウィビ <paul@webb.page>2026-04-11 14:19:10 -0700
committernetop://ウィビ <paul@webb.page>2026-04-11 14:19:10 -0700
commitca16dcf93922d63dd1783dc471b2ac0c61f8d11f (patch)
tree4568cfda773657f01030747b2e2b804abc092901 /src/common.ts
initial commitHEADprimary
Diffstat (limited to 'src/common.ts')
-rwxr-xr-xsrc/common.ts47
1 files changed, 47 insertions, 0 deletions
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, 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
+ });
+}