diff options
Diffstat (limited to 'src/common.ts')
| -rwxr-xr-x | src/common.ts | 47 |
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 + }); +} |
