diff --git a/grafast/grafast/src/args.ts b/grafast/grafast/src/args.ts index c5bb31bde..b4eff2f51 100644 --- a/grafast/grafast/src/args.ts +++ b/grafast/grafast/src/args.ts @@ -12,9 +12,11 @@ import { isPromiseLike } from "./utils.js"; */ export function hookArgs( args: ExecutionArgs, + resolvedPreset: GraphileConfig.ResolvedPreset, ctx: Partial, - resolvedPreset: GraphileConfig.ResolvedPreset = NULL_PRESET, ): ExecutionArgs | PromiseLike { + // TODO: assert that args haven't already been hooked + // Make context mutable args.contextValue = Object.assign(Object.create(null), args.contextValue); diff --git a/grafast/grafast/src/grafastGraphql.ts b/grafast/grafast/src/grafastGraphql.ts index 097d59c14..054d115c0 100644 --- a/grafast/grafast/src/grafastGraphql.ts +++ b/grafast/grafast/src/grafastGraphql.ts @@ -10,7 +10,6 @@ import type { import { GraphQLError, parse, Source, validate, validateSchema } from "graphql"; import type { PromiseOrValue } from "graphql/jsutils/PromiseOrValue"; -import { NULL_PRESET } from "./config.js"; import { SafeError } from "./error.js"; import { execute } from "./execute.js"; import { hookArgs } from "./index.js"; @@ -151,7 +150,7 @@ export function grafastGraphql( }; if (resolvedPreset && ctx) { - const argsOrPromise = hookArgs(executionArgs, ctx, resolvedPreset); + const argsOrPromise = hookArgs(executionArgs, resolvedPreset, ctx); if (isPromiseLike(argsOrPromise)) { return Promise.resolve(argsOrPromise).then((hookedArgs) => execute(hookedArgs, resolvedPreset), @@ -162,15 +161,16 @@ export function grafastGraphql( } } else { // Execute - return execute(executionArgs, resolvedPreset ?? NULL_PRESET); + return execute(executionArgs, resolvedPreset); } } export function grafastGraphqlSync( args: GraphQLArgs, - resolvedPreset: GraphileConfig.ResolvedPreset = NULL_PRESET, + resolvedPreset?: GraphileConfig.ResolvedPreset, + ctx?: Partial, ): ExecutionResult { - const result = grafastGraphql(args, resolvedPreset); + const result = grafastGraphql(args, resolvedPreset, ctx); if (isPromiseLike(result)) { throw new SafeError("Grafast execution failed to complete synchronously."); } diff --git a/grafast/grafserv/src/middleware/graphql.ts b/grafast/grafserv/src/middleware/graphql.ts index af576fc87..fd6954cab 100644 --- a/grafast/grafserv/src/middleware/graphql.ts +++ b/grafast/grafserv/src/middleware/graphql.ts @@ -493,14 +493,10 @@ export const makeGraphQLHandler = ( operationName, }; - await hookArgs( - args, - { - ...request.requestContext, - http: request, - }, - resolvedPreset, - ); + await hookArgs(args, resolvedPreset, { + ...request.requestContext, + http: request, + }); try { const result = await grafastExecute(args, resolvedPreset); diff --git a/grafast/grafserv/src/utils.ts b/grafast/grafserv/src/utils.ts index 7dde86bd3..c2c1895ae 100644 --- a/grafast/grafserv/src/utils.ts +++ b/grafast/grafserv/src/utils.ts @@ -165,23 +165,15 @@ export function makeGraphQLWSConfig(instance: GrafservBase): ServerOptions { schema: async () => instance.getSchema(), // PERF: we can remove the async/await and only use when context is async execute: async (args: ExecutionArgs) => { - await hookArgs( - args, - { - ws: (args.contextValue as any)?.[$$extra], - }, - instance.resolvedPreset, - ); + await hookArgs(args, instance.resolvedPreset, { + ws: (args.contextValue as any)?.[$$extra], + }); return maskExecutionResult(await execute(args, resolvedPreset)); }, subscribe: async (args: ExecutionArgs) => { - await hookArgs( - args, - { - ws: (args.contextValue as any)?.[$$extra], - }, - instance.resolvedPreset, - ); + await hookArgs(args, instance.resolvedPreset, { + ws: (args.contextValue as any)?.[$$extra], + }); return maskExecutionResult(await subscribe(args, resolvedPreset)); }, }; diff --git a/postgraphile/postgraphile/__tests__/helpers.ts b/postgraphile/postgraphile/__tests__/helpers.ts index 3a7eea16e..7e1b22d6b 100644 --- a/postgraphile/postgraphile/__tests__/helpers.ts +++ b/postgraphile/postgraphile/__tests__/helpers.ts @@ -267,7 +267,7 @@ export async function runTestQuery( variableValues, }; - await hookArgs(args, {}, resolvedPreset); + await hookArgs(args, resolvedPreset, {}); // We must override the context so that we can listen to the SQL queries. args.contextValue = { diff --git a/postgraphile/website/postgraphile/migrating-from-v4/index.md b/postgraphile/website/postgraphile/migrating-from-v4/index.md index fe7a96e4f..2dc5911e5 100644 --- a/postgraphile/website/postgraphile/migrating-from-v4/index.md +++ b/postgraphile/website/postgraphile/migrating-from-v4/index.md @@ -251,13 +251,9 @@ async function main() { }; // Merge in the context and anything else plugins/presets want to add - await hookArgs( - args, - { - /* optional details for your context callback(s) to use */ - }, - resolvedPreset, - ); + await hookArgs(args, resolvedPreset, { + /* optional details for your context callback(s) to use */ + }); const result = await grafast(args);