Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MiddlewareHandlers helper #2240

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-meals-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"grafast": patch
---

Internal: adopt new MiddlewareHandlers type for simplicity.
7 changes: 7 additions & 0 deletions .changeset/mean-mugs-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"grafserv": patch
---

Adopt new MiddlewareHandlers type for simplicity, and in doing so fix type of
middleware (for plugins) that incorrectly unwrapped promise resulting in
TypeScript incorrectly suggesting that `await` was not necessary.
6 changes: 6 additions & 0 deletions .changeset/nine-islands-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-config": patch
---

Adds `MiddlewareHandlers<TActivities>` type to help with adding middleware to
configs
12 changes: 2 additions & 10 deletions grafast/grafast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./thereCanBeOnlyOne.js";

import type LRU from "@graphile/lru";
import debugFactory from "debug";
import type { CallbackOrDescriptor, MiddlewareNext } from "graphile-config";
import type { MiddlewareHandlers } from "graphile-config";
import type {
DocumentNode,
GraphQLError,
Expand Down Expand Up @@ -780,15 +780,7 @@ declare global {
}
interface Plugin {
grafast?: {
middleware?: {
[key in keyof GrafastMiddleware]?: CallbackOrDescriptor<
GrafastMiddleware[key] extends (
...args: infer UArgs
) => infer UResult
? (next: MiddlewareNext<UResult>, ...args: UArgs) => UResult
: never
>;
};
middleware?: MiddlewareHandlers<GrafastMiddleware>;
};
}
}
Expand Down
15 changes: 2 additions & 13 deletions grafast/grafserv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PromiseOrDirect } from "grafast";
import type { ExecutionArgs, GraphQLError } from "grafast/graphql";
import type { CallbackOrDescriptor, MiddlewareNext } from "graphile-config";
import type { CallbackOrDescriptor, MiddlewareHandlers } from "graphile-config";
import type { RuruHTMLParts } from "ruru/server";

import type {
Expand Down Expand Up @@ -86,18 +86,7 @@ declare global {
: never
>;
};
middleware?: {
[key in keyof GrafservMiddleware]?: CallbackOrDescriptor<
GrafservMiddleware[key] extends (
...args: infer UArgs
) => infer UResult
? (
next: MiddlewareNext<Awaited<UResult>>,
...args: UArgs
) => UResult
: never
>;
};
middleware?: MiddlewareHandlers<GrafservMiddleware>;
};
}
interface GrafservOptions {
Expand Down
2 changes: 1 addition & 1 deletion utils/graphile-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type {
CallbackOrDescriptor,
FunctionalityObject,
} from "./interfaces.js";
export type { MiddlewareNext } from "./middleware.js";
export type { MiddlewareHandlers, MiddlewareNext } from "./middleware.js";
export { Middleware } from "./middleware.js";
export { isResolvedPreset, resolvePresets } from "./resolvePresets.js";

Expand Down
10 changes: 10 additions & 0 deletions utils/graphile-config/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ function makeNext<TRawResult, TAwaitedResult = Awaited<TRawResult>>(
};
return next;
}

export type MiddlewareHandlers<
TActivities extends FunctionalityObject<TActivities>,
> = {
[key in keyof TActivities]?: CallbackOrDescriptor<
TActivities[key] extends (...args: infer UArgs) => infer UResult
? (next: MiddlewareNext<UResult>, ...args: UArgs) => UResult
: never
>;
};
26 changes: 26 additions & 0 deletions utils/website/graphile-config/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ type SomeActionResult = number;

export type PromiseOrDirect<T> = Promise<T> | T;

/***** index.ts *****/

import type { MiddlewareHandlers } from "graphile-config";

// Extend Plugin with support for registering handlers for the middleware activities:
declare global {
namespace GraphileConfig {
interface Plugin {
libraryName?: {
middleware?: MiddlewareHandlers<MyMiddleware>;

// Prior to [email protected], a more verbose alternative was required:
/*
middleware?: {
[key in keyof MyMiddleware]?: CallbackOrDescriptor<
MyMiddleware[key] extends (...args: infer UArgs) => infer UResult
? (next: MiddlewareNext<UResult>, ...args: UArgs) => UResult
: never
>;
};
*/
};
}
}
}

/***** getMiddleware.ts *****/

import { Middleware, orderedApply, resolvePresets } from "graphile-config";
Expand Down
Loading