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

test: add type test for HttpResponse.error #2132

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BodyType extends DefaultBodyType>
extends Request {
Expand Down
14 changes: 10 additions & 4 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ResponseBodyType>)
? Response & { [bodyType]?: undefined }
: // If ResponseBodyType is exactly `undefined`,
// accept only the plain Response type.
ResponseBodyType extends undefined
? Response & { [bodyType]?: undefined }
: StrictResponse<ResponseBodyType>)
| undefined
| void

export type MaybeAsyncResponseResolverReturnType<
ResponseBodyType extends DefaultBodyType,
ResponseBodyType extends DefaultBodyType = undefined,
> = MaybePromise<ResponseResolverReturnType<ResponseBodyType>>

export type AsyncResponseResolverReturnType<
Expand Down
6 changes: 6 additions & 0 deletions test/typings/graphql.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading