Skip to content

Commit

Permalink
refactor(core): update CallableMiddlewareOptions for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
xcfox committed Dec 3, 2024
1 parent a7c6c02 commit 25c27ac
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
6 changes: 3 additions & 3 deletions examples/middlewares/src/middlewares/output-validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Middleware, silk } from "@gqloom/core"

export const outputValidator: Middleware = async (next, { outputSilk }) => {
const output = await next()
return await silk.parse(outputSilk, output)
export const outputValidator: Middleware = async (opts) => {
const output = await opts.next()
return await silk.parse(opts.outputSilk, output)
}
1 change: 1 addition & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

* Fix: add all objects in `resolver.of` to `context.types`
* Chore: update `@standard-schema/spec` to 1.0.0-beta.4
* Refactor: update `CallableMiddlewareOptions` for middleware

## 0.4.0 (2024-11-120)

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/utils/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { describe, expect, it } from "vitest"
import { createInputParser, silk } from "../resolver"
import {
type Middleware,
type MiddlewarePayload,
type MiddlewareOptions,
applyMiddlewares,
} from "./middleware"

function initPayload(): MiddlewarePayload {
function initOptions(): MiddlewareOptions {
return {
outputSilk: silk(GraphQLString),
parent: undefined,
Expand All @@ -24,7 +24,7 @@ describe("middleware", async () => {
const result = await applyMiddlewares(
[simpleMiddleware],
() => answer,
initPayload()
initOptions()
)
expect(result).toBe(answer)
})
Expand Down Expand Up @@ -55,7 +55,7 @@ describe("middleware", async () => {
results.push("Resolve")
return "resolved"
}
await applyMiddlewares(middlewares, resolve, initPayload())
await applyMiddlewares(middlewares, resolve, initOptions())
expect(results).toEqual([
"A Start",
"B Start",
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("middleware", async () => {
},
]
const resolve = () => 0
const result = await applyMiddlewares(middlewares, resolve, initPayload())
const result = await applyMiddlewares(middlewares, resolve, initOptions())
expect(result).toBe(6)
})

Expand All @@ -104,7 +104,7 @@ describe("middleware", async () => {
() => {
return asyncLocalStorage.getStore()?.cat
},
initPayload()
initOptions()
)

expect(result).toBe("meow")
Expand Down
25 changes: 18 additions & 7 deletions packages/core/src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from "../resolver"
import type { MayPromise } from "./types"

export interface MiddlewarePayload<
export interface MiddlewareOptions<
TField extends GenericFieldOrOperation = FieldOrOperation<any, any, any, any>,
> {
/** The Output Silk of the field */
Expand All @@ -30,13 +30,20 @@ export interface MiddlewarePayload<
type: FieldOrOperationType
}

export interface CallableMiddlewareOptions<
TField extends GenericFieldOrOperation = FieldOrOperation<any, any, any, any>,
> extends MiddlewareOptions<TField> {
/** The function to call next in the middleware chain. */
next: () => MayPromise<StandardSchemaV1.InferOutput<InferFieldOutput<TField>>>

/** The function to call next in the middleware chain. */
(): MayPromise<StandardSchemaV1.InferOutput<InferFieldOutput<TField>>>
}

export type Middleware<
TField extends GenericFieldOrOperation = FieldOrOperation<any, any, any, any>,
> = (
next: () => MayPromise<
StandardSchemaV1.InferOutput<InferFieldOutput<TField>>
>,
payload: MiddlewarePayload<TField>
options: CallableMiddlewareOptions<TField>
) => MayPromise<StandardSchemaV1.InferOutput<InferFieldOutput<TField>>>

export function applyMiddlewares<
Expand All @@ -46,7 +53,7 @@ export function applyMiddlewares<
resolveFunction: () => MayPromise<
StandardSchemaV1.InferOutput<InferFieldOutput<TField>>
>,
payload: MiddlewarePayload<TField>
options: MiddlewareOptions<TField>
): Promise<StandardSchemaV1.InferOutput<InferFieldOutput<TField>>> {
const next = (
index: number
Expand All @@ -55,7 +62,11 @@ export function applyMiddlewares<
return resolveFunction()
}
const middleware = middlewares[index]
return middleware(() => next(index + 1), payload)
const callableOptions = Object.assign(() => next(index + 1), {
...options,
next: () => next(index + 1),
})
return middleware(callableOptions)
}
return next(0)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/resolver.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("resolver type", () => {
createGiraffe: mutation(Giraffe, {
input: GiraffeInput,
middlewares: [
async (next, { parent, parseInput }) => {
async ({ parent, next, parseInput }) => {
it("should infer input type", () => {
expectTypeOf(parseInput).returns.resolves.toEqualTypeOf<
StandardSchemaV1.Result<Partial<IGiraffe>>
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("resolver type", () => {
greeting: field(silk<string>(new GraphQLNonNull(GraphQLString)), {
input: { myName: silk(GraphQLString) },
middlewares: [
async (next, { parent, parseInput }) => {
async ({ parent, next, parseInput }) => {
it("should infer parent type", () => {
expectTypeOf(parent).toEqualTypeOf<IGiraffe>()
})
Expand Down Expand Up @@ -153,7 +153,7 @@ describe("resolver type", () => {
},
{
middlewares: [
async (next, { parent, parseInput }) => {
async ({ parent, next, parseInput }) => {
it("should infer parent type", () => {
expectTypeOf(parent).toEqualTypeOf<IGiraffe | undefined>()
})
Expand Down
10 changes: 5 additions & 5 deletions packages/mikro-orm/test/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ describe("MikroOperationsBobbin", async () => {
describe("FindOneQuery", async () => {
const findOne = bobbin.FindOneQuery({
middlewares: [
async (next, { parseInput }) => {
async (opts) => {
assertType<
CallableInputParser<
GraphQLSilk<FindOneFilter<IGiraffe>, FindOneFilter<IGiraffe>>
>
>(parseInput)
return next()
>(opts.parseInput)
return opts.next()
},
],
})
Expand All @@ -282,15 +282,15 @@ describe("MikroOperationsBobbin", async () => {
{
findOne: bobbin.FindOneQuery({
middlewares: [
async (next, { parseInput }) => {
async (next) => {
assertType<
CallableInputParser<
GraphQLSilk<
FindOneFilter<IGiraffe>,
FindOneFilter<IGiraffe>
>
>
>(parseInput)
>(next.parseInput)
return next()
},
],
Expand Down
22 changes: 11 additions & 11 deletions packages/prisma/test/bobbin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a countQuery", () => {
const q = UserBobbin.countQuery({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -238,7 +238,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a findFirstQuery", () => {
const q = UserBobbin.findFirstQuery({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -296,7 +296,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a findManyQuery", () => {
const q = UserBobbin.findManyQuery({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -354,7 +354,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a findUniqueQuery", () => {
const q = UserBobbin.findUniqueQuery({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -412,7 +412,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a createMutation", () => {
const m = UserBobbin.createMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -470,7 +470,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a createManyMutation", () => {
const m = UserBobbin.createManyMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -529,7 +529,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a deleteMutation", () => {
const m = UserBobbin.deleteMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -586,7 +586,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a deleteManyMutation", async () => {
const m = UserBobbin.deleteManyMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -646,7 +646,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a deleteMutation", async () => {
const m = UserBobbin.updateMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -713,7 +713,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a deleteMutation", async () => {
const m = UserBobbin.updateManyMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down Expand Up @@ -784,7 +784,7 @@ describe("PrismaModelBobbin", () => {
it("should be able to create a deleteMutation", async () => {
const m = UserBobbin.upsertMutation({
middlewares: [
async (next, { parseInput }) => {
async ({ next, parseInput }) => {
const input = await parseInput()
expectTypeOf(input).toEqualTypeOf<
StandardSchemaV1.Result<
Expand Down

0 comments on commit 25c27ac

Please sign in to comment.