From b17099a973b0491396757ff40e1bd39d2804dc2f Mon Sep 17 00:00:00 2001 From: Jacques Germishuys Date: Sat, 13 Apr 2024 22:23:21 +0100 Subject: [PATCH 1/3] test: add type test for HttpResponse.error --- test/typings/graphql.test-d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/typings/graphql.test-d.ts b/test/typings/graphql.test-d.ts index a4bdf2414..672abf9b8 100644 --- a/test/typings/graphql.test-d.ts +++ b/test/typings/graphql.test-d.ts @@ -141,6 +141,10 @@ it('graphql handlers allow passthrough responses', () => { }) }) +it('graphql handlers allow error response', () => { + graphql.operation(() => HttpResponse.error()) +}) + it("graphql variables cannot extract type from the runtime 'DocumentNode'", () => { /** * Supports `DocumentNode` as the GraphQL operation name. From 4a875d31518cd0c24aeb22b5e05b9e58d16cfbf6 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Wed, 8 May 2024 16:52:19 +0200 Subject: [PATCH 2/3] test: elaborate on graphql handlers in tests --- test/typings/graphql.test-d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/typings/graphql.test-d.ts b/test/typings/graphql.test-d.ts index 672abf9b8..744f306f6 100644 --- a/test/typings/graphql.test-d.ts +++ b/test/typings/graphql.test-d.ts @@ -142,6 +142,8 @@ it('graphql handlers allow passthrough responses', () => { }) it('graphql handlers allow error response', () => { + graphql.query('GetUser', () => HttpResponse.error()) + graphql.mutation('UpdatePost', () => HttpResponse.error()) graphql.operation(() => HttpResponse.error()) }) From a7d389f5fc4eb4849005ddfb22b85e0ebc8c7c29 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sat, 25 May 2024 10:07:32 +0300 Subject: [PATCH 3/3] fix(wip): allow Response.error in graphql handlers --- src/core/HttpResponse.ts | 2 +- src/core/handlers/RequestHandler.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/HttpResponse.ts b/src/core/HttpResponse.ts index de3f6ea9d..ce13c747b 100644 --- a/src/core/HttpResponse.ts +++ b/src/core/HttpResponse.ts @@ -9,7 +9,7 @@ export interface HttpResponseInit extends ResponseInit { type?: ResponseType } -declare const bodyType: unique symbol +export declare const bodyType: unique symbol export interface StrictRequest extends Request { diff --git a/src/core/handlers/RequestHandler.ts b/src/core/handlers/RequestHandler.ts index 5c0c3ffa7..a09deb2e9 100644 --- a/src/core/handlers/RequestHandler.ts +++ b/src/core/handlers/RequestHandler.ts @@ -3,7 +3,7 @@ import { getCallFrame } from '../utils/internal/getCallFrame' import { isIterable } from '../utils/internal/isIterable' import type { ResponseResolutionContext } from '../utils/executeHandlers' import type { MaybePromise } from '../typeUtils' -import { StrictRequest, StrictResponse } from '..//HttpResponse' +import { StrictRequest, StrictResponse, bodyType } from '..//HttpResponse' export type DefaultRequestMultipartBody = Record< string, @@ -38,14 +38,20 @@ export interface RequestHandlerInternalInfo { export type ResponseResolverReturnType< ResponseBodyType extends DefaultBodyType = undefined, > = + // If ResponseBodyType is a union, and one of the types is `undefined`, + // allow plain Response as the type. | ([ResponseBodyType] extends [undefined] - ? Response - : StrictResponse) + ? Response & { [bodyType]?: undefined } + : // If ResponseBodyType is exactly `undefined`, + // accept only the plain Response type. + ResponseBodyType extends undefined + ? Response & { [bodyType]?: undefined } + : StrictResponse) | undefined | void export type MaybeAsyncResponseResolverReturnType< - ResponseBodyType extends DefaultBodyType, + ResponseBodyType extends DefaultBodyType = undefined, > = MaybePromise> export type AsyncResponseResolverReturnType<