Skip to content

Commit

Permalink
Overhaul legacy support and middleware creation; fix linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 21, 2024
1 parent cd993e3 commit 95414e5
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 113 deletions.
88 changes: 47 additions & 41 deletions grafast/grafast/src/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ExecutionArgs } from "graphql";

import type { GrafastExecutionArgs, PromiseOrDirect } from "./interfaces.js";
import type {
GrafastExecutionArgs,
PrepareArgsEvent,
PromiseOrDirect,
} from "./interfaces.js";
import { $$hooked } from "./interfaces.js";
import { getMiddlewares } from "./middlewares.js";
import { isPromiseLike } from "./utils.js";
Expand All @@ -26,61 +30,63 @@ export function hookArgs(
legacyResolvedPreset?: GraphileConfig.ResolvedPreset,
legacyCtx?: Partial<Grafast.RequestContext>,
): PromiseOrDirect<Grafast.ExecutionArgs> {
if (
legacyResolvedPreset !== undefined ||
legacyCtx !== undefined ||
rawArgs.middlewares === undefined
) {
rawArgs.resolvedPreset = rawArgs.resolvedPreset ?? legacyResolvedPreset;
if (legacyResolvedPreset !== undefined) {
rawArgs.resolvedPreset = legacyResolvedPreset;
}
if (legacyCtx !== undefined) {
rawArgs.requestContext = rawArgs.requestContext ?? legacyCtx;
rawArgs.middlewares =
rawArgs.middlewares === undefined && rawArgs.resolvedPreset != null
? getMiddlewares(rawArgs.resolvedPreset)
: rawArgs.middlewares;
}
const args = rawArgs as Grafast.ExecutionArgs;
const {
middlewares: rawMiddlewares,
resolvedPreset,
requestContext: ctx = EMPTY_OBJECT,
middlewares,
} = args;
contextValue: rawContextValue,
} = rawArgs;
// Make context mutable
rawArgs.contextValue = Object.assign(Object.create(null), rawContextValue);
const middlewares =
rawMiddlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: rawMiddlewares ?? null;
if (rawMiddlewares === undefined) {
rawArgs.middlewares = middlewares;
}
const args = rawArgs as Grafast.ExecutionArgs;
// Assert that args haven't already been hooked
if (args[$$hooked]) {
throw new Error("Must not call hookArgs twice!");
}
args[$$hooked] = true;

// Make context mutable
args.contextValue = Object.assign(Object.create(null), args.contextValue);
if (middlewares) {
return middlewares.run("prepareArgs", { args }, finalizeWithEvent);
} else {
return finalize(args);
}
}

// finalize(args): args is deliberately shadowed
const finalize = (args: Grafast.ExecutionArgs) => {
const userContext = resolvedPreset?.grafast?.context;
if (typeof userContext === "function") {
const result = userContext(ctx, args);
if (isPromiseLike(result)) {
// Deliberately shadowed 'result'
return result.then((result) => {
Object.assign(args.contextValue as Partial<Grafast.Context>, result);
return args;
});
} else {
Object.assign(args.contextValue as Partial<Grafast.Context>, result);
function finalize(args: Grafast.ExecutionArgs) {
const userContext = args.resolvedPreset?.grafast?.context;
const contextValue = args.contextValue as Partial<Grafast.Context>;
if (typeof userContext === "function") {
const result = userContext(args.requestContext ?? EMPTY_OBJECT, args);
if (isPromiseLike(result)) {
// Deliberately shadowed 'result'
return result.then((result) => {
Object.assign(contextValue, result);
return args;
}
} else if (typeof userContext === "object" && userContext !== null) {
Object.assign(args.contextValue as Partial<Grafast.Context>, userContext);
return args;
});
} else {
Object.assign(contextValue, result);
return args;
}
};

if (middlewares) {
return middlewares.run("prepareArgs", { args }, ({ args }) =>
finalize(args),
);
} else if (typeof userContext === "object" && userContext !== null) {
Object.assign(contextValue, userContext);
return args;
} else {
return finalize(args);
return args;
}
}

function finalizeWithEvent(event: PrepareArgsEvent) {
return finalize(event.args);
}
1 change: 0 additions & 1 deletion grafast/grafast/src/engine/executeBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
ExecutionExtra,
ExecutionValue,
ForcedValues,
GrafastExecutionArgs,
GrafastInternalResultsOrStream,
GrafastResultsList,
GrafastResultStreamList,
Expand Down
34 changes: 17 additions & 17 deletions grafast/grafast/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ export function execute(
): PromiseOrValue<
ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, undefined>
> {
if (
legacyResolvedPreset !== undefined ||
legacyOutputDataAsString !== undefined ||
args.middlewares === undefined
) {
const resolvedPreset = args.resolvedPreset ?? legacyResolvedPreset;
const middlewares =
args.middlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: args.middlewares;
return execute({
...args,
resolvedPreset,
middlewares,
});
// TODO: remove legacy compatibility
if (legacyResolvedPreset !== undefined) {
args.resolvedPreset = legacyResolvedPreset;
}
if (legacyOutputDataAsString !== undefined) {
args.outputDataAsString = legacyOutputDataAsString;
}

const { resolvedPreset } = args;
const middlewares =
args.middlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: args.middlewares ?? null;
if (args.middlewares === undefined) {
args.middlewares = middlewares;
}
if (args.middlewares != null) {
return args.middlewares.run("execute", { args }, executeMiddlewareCallback);
if (middlewares !== null) {
return middlewares.run("execute", { args }, executeMiddlewareCallback);
} else {
return withGrafastArgs(args);
}
Expand Down
33 changes: 14 additions & 19 deletions grafast/grafast/src/grafastGraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import LRU from "@graphile/lru";
import type {
AsyncExecutionResult,
DocumentNode,
ExecutionArgs,
ExecutionResult,
GraphQLSchema,
} from "graphql";
Expand Down Expand Up @@ -140,24 +139,14 @@ export function grafast(
): PromiseOrValue<
ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, undefined>
> {
if (
legacyResolvedPreset !== undefined ||
legacyCtx !== undefined ||
args.middlewares === undefined
) {
const resolvedPreset = args.resolvedPreset ?? legacyResolvedPreset;
const requestContext = args.requestContext ?? legacyCtx;
const middlewares =
args.middlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: args.middlewares;
return grafast({
...args,
resolvedPreset,
requestContext,
middlewares,
});
// Convert legacy args to properties on `args`:
if (legacyResolvedPreset !== undefined) {
args.resolvedPreset = args.resolvedPreset ?? legacyResolvedPreset;
}
if (legacyCtx !== undefined) {
args.requestContext = args.requestContext ?? legacyCtx;
}

const {
schema,
source,
Expand All @@ -169,8 +158,14 @@ export function grafast(
typeResolver,
resolvedPreset,
requestContext,
middlewares,
middlewares: rawMiddlewares,
} = args;
const middlewares =
rawMiddlewares !== undefined
? rawMiddlewares
: resolvedPreset != null
? getMiddlewares(resolvedPreset)
: null;

// Validate Schema
const schemaValidationErrors = middlewares
Expand Down
1 change: 0 additions & 1 deletion grafast/grafast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import debugFactory from "debug";
import type { CallbackOrDescriptor, MiddlewareNext } from "graphile-config";
import type {
DocumentNode,
ExecutionArgs as GraphQLExecutionArgs,
GraphQLError,
OperationDefinitionNode,
} from "graphql";
Expand Down
2 changes: 1 addition & 1 deletion grafast/grafast/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ export type StreamMoreableArray<T = any> = Array<T> & {
export interface GrafastArgs extends GraphQLArgs {
resolvedPreset?: GraphileConfig.ResolvedPreset;
requestContext?: Partial<Grafast.RequestContext>;
middlewares?: Middlewares<GraphileConfig.GrafastMiddlewares>;
middlewares?: Middlewares<GraphileConfig.GrafastMiddlewares> | null;
}
export type Maybe<T> = T | null | undefined;

Expand Down
6 changes: 1 addition & 5 deletions grafast/grafast/src/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
type MiddlewareNext,
Middlewares,
orderedApply,
} from "graphile-config";
import { Middlewares, orderedApply } from "graphile-config";

const $$middleware = Symbol("middleware");
export function getMiddlewares(
Expand Down
1 change: 0 additions & 1 deletion grafast/grafast/src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ExecutionArgs } from "graphql";
import * as graphql from "graphql";
import type {
AsyncExecutionResult,
Expand Down
39 changes: 17 additions & 22 deletions grafast/grafast/src/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
} from "graphql";
import type { PromiseOrValue } from "graphql/jsutils/PromiseOrValue";

import { NULL_PRESET } from "./config.js";
import { withGrafastArgs } from "./execute.js";
import type { GrafastExecutionArgs } from "./index.js";
import type { SubscribeEvent } from "./interfaces.js";
Expand Down Expand Up @@ -41,28 +40,24 @@ export function subscribe(
| AsyncGenerator<ExecutionResult | AsyncExecutionResult, void, void>
| ExecutionResult
> {
if (
legacyResolvedPreset !== undefined ||
legacyOutputDataAsString !== undefined ||
args.middlewares === undefined
) {
const resolvedPreset = args.resolvedPreset ?? legacyResolvedPreset;
const middlewares =
args.middlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: args.middlewares;
return subscribe({
...args,
resolvedPreset,
middlewares,
});
// TODO: remove legacy compatibility
if (legacyResolvedPreset !== undefined) {
args.resolvedPreset = legacyResolvedPreset;
}
if (args.middlewares != null) {
return args.middlewares.run(
"subscribe",
{ args },
subscribeMiddlewareCallback,
);
if (legacyOutputDataAsString !== undefined) {
args.outputDataAsString = legacyOutputDataAsString;
}

const { resolvedPreset } = args;
const middlewares =
args.middlewares === undefined && resolvedPreset != null
? getMiddlewares(resolvedPreset)
: args.middlewares ?? null;
if (args.middlewares === undefined) {
args.middlewares = middlewares;
}
if (middlewares !== null) {
return middlewares.run("subscribe", { args }, subscribeMiddlewareCallback);
} else {
return withGrafastArgs(args);
}
Expand Down
6 changes: 1 addition & 5 deletions grafast/grafserv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { PromiseOrDirect } from "grafast";
import type { ExecutionArgs, GraphQLError } from "grafast/graphql";
import type {
CallbackOrDescriptor,
MiddlewareNext,
PluginHook,
} from "graphile-config";
import type { CallbackOrDescriptor, MiddlewareNext } from "graphile-config";
import type { RuruHTMLParts } from "ruru/server";

import type {
Expand Down

0 comments on commit 95414e5

Please sign in to comment.