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< diff --git a/test/typings/graphql.test-d.ts b/test/typings/graphql.test-d.ts index a4bdf2414..744f306f6 100644 --- a/test/typings/graphql.test-d.ts +++ b/test/typings/graphql.test-d.ts @@ -141,6 +141,12 @@ 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()) +}) + it("graphql variables cannot extract type from the runtime 'DocumentNode'", () => { /** * Supports `DocumentNode` as the GraphQL operation name.