Skip to content

Commit d5a4012

Browse files
fix: batchRequests type constraint (#462)
Also update dependency vitest to ^0.29.0 Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jason Kuhrt <[email protected]>
1 parent 0fb7062 commit d5a4012

File tree

11 files changed

+170
-54
lines changed

11 files changed

+170
-54
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Minimal GraphQL client supporting Node and browsers for scripts or simple apps
4141
- [Ignore](#ignore)
4242
- [All](#all)
4343
- [FAQ](#faq)
44-
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
45-
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
46-
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
44+
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
45+
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
46+
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
4747

4848
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4949

examples/typed-document-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLClient,request } from '../src/index.js'
1+
import { GraphQLClient, request } from '../src/index.js'
22
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
33
import { parse } from 'graphql'
44
;(async function () {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"prettier": "^2.8.3",
101101
"type-fest": "^3.5.3",
102102
"typescript": "^4.9.4",
103-
"vitest": "^0.28.2",
103+
"vitest": "^0.29.0",
104104
"ws": "^8.12.0"
105105
},
106106
"prettier": "@prisma-labs/prettier-config"

pnpm-lock.yaml

Lines changed: 116 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/graphql-ws.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolveRequestDocument } from './resolveRequestDocument.js'
22
import type * as Dom from './types.dom.js'
3-
import type { RequestDocument, Variables } from './types.js';
3+
import type { RequestDocument, Variables } from './types.js'
44
import { ClientError } from './types.js'
55
// import type WebSocket from 'ws'
66

src/index.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defaultJsonSerializer } from './defaultJsonSerializer.js'
33
import { HeadersInstanceToPlainObject, uppercase } from './helpers.js'
44
import {
55
parseBatchRequestArgs,
6-
parseBatchRequestsExtendedArgs,
76
parseRawRequestArgs,
87
parseRawRequestExtendedArgs,
98
parseRequestArgs,
@@ -33,6 +32,7 @@ import {
3332
} from './types.js'
3433
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
3534
import crossFetch, * as CrossFetch from 'cross-fetch'
35+
import type { T, V } from 'vitest/dist/types-7cd96283.js'
3636

3737
export {
3838
BatchRequestDocument,
@@ -310,12 +310,12 @@ export class GraphQLClient {
310310
/**
311311
* Send GraphQL documents in batch to the server.
312312
*/
313-
batchRequests<T = unknown, V extends Variables = Variables>(
314-
documents: BatchRequestDocument<V>[],
315-
requestHeaders?: Dom.RequestInit['headers']
316-
): Promise<T>
317-
batchRequests<T = unknown, V extends Variables = Variables>(options: BatchRequestsOptions<V>): Promise<T>
318-
batchRequests<T = unknown, V extends Variables = Variables>(
313+
// prettier-ignore
314+
batchRequests<T extends BatchResult, V extends Variables = Variables>(documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>
315+
// prettier-ignore
316+
batchRequests<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsOptions<V>): Promise<T>
317+
// prettier-ignore
318+
batchRequests<T extends BatchResult, V extends Variables = Variables>(
319319
documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>,
320320
requestHeaders?: Dom.RequestInit['headers']
321321
): Promise<T> {
@@ -573,22 +573,39 @@ export async function request<T, V extends Variables = Variables>(
573573
* await batchRequests('https://foo.bar/graphql', [{ query: gql`...` }])
574574
* ```
575575
*/
576-
export async function batchRequests<T, V extends Variables = Variables>(
577-
url: string,
578-
documents: BatchRequestDocument<V>[],
579-
requestHeaders?: Dom.RequestInit['headers']
580-
): Promise<T>
581-
export async function batchRequests<T, V extends Variables = Variables>(
582-
options: BatchRequestsExtendedOptions<V>
583-
): Promise<T>
584-
export async function batchRequests<T, V extends Variables = Variables>(
585-
urlOrOptions: string | BatchRequestsExtendedOptions<V>,
586-
documents?: BatchRequestDocument<V>[],
587-
requestHeaders?: Dom.RequestInit['headers']
588-
): Promise<T> {
589-
const params = parseBatchRequestsExtendedArgs<V>(urlOrOptions, documents, requestHeaders)
576+
export const batchRequests: BatchRequests = async (...args: BatchRequestsArgs) => {
577+
const params = parseBatchRequestsArgsExtended(args)
590578
const client = new GraphQLClient(params.url)
591-
return client.batchRequests<T, V>(params)
579+
return client.batchRequests(params)
580+
}
581+
582+
interface Result<Data extends object = object> {
583+
data: Data
584+
}
585+
586+
type BatchResult = [Result, ...Result[]]
587+
588+
// prettier-ignore
589+
interface BatchRequests {
590+
<T extends BatchResult, V extends Variables = Variables>(url: string, documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>
591+
<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsExtendedOptions<V>): Promise<T>
592+
}
593+
594+
type BatchRequestsArgs =
595+
| [url: string, documents: BatchRequestDocument[], requestHeaders?: Dom.RequestInit['headers']]
596+
| [options: BatchRequestsExtendedOptions]
597+
598+
const parseBatchRequestsArgsExtended = (args: BatchRequestsArgs): BatchRequestsExtendedOptions => {
599+
if (args.length === 1) {
600+
return args[0]
601+
} else {
602+
return {
603+
url: args[0],
604+
documents: args[1],
605+
requestHeaders: args[2],
606+
signal: undefined,
607+
}
608+
}
592609
}
593610

594611
export default request

src/parseArgs.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,3 @@ export const parseRawRequestExtendedArgs = <V extends Variables = Variables>(
8888
signal: undefined,
8989
} as unknown as RawRequestExtendedOptions<V>)
9090
}
91-
92-
export const parseBatchRequestsExtendedArgs = <V extends Variables = Variables>(
93-
urlOrOptions: string | BatchRequestsExtendedOptions<V>,
94-
documents?: BatchRequestDocument<V>[],
95-
requestHeaders?: Dom.RequestInit['headers']
96-
): BatchRequestsExtendedOptions<V> => {
97-
return (urlOrOptions as BatchRequestsExtendedOptions<V>).documents
98-
? (urlOrOptions as BatchRequestsExtendedOptions<V>)
99-
: {
100-
url: urlOrOptions as string,
101-
documents: documents as BatchRequestDocument<V>[],
102-
requestHeaders: requestHeaders,
103-
signal: undefined,
104-
}
105-
}

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export type RequestOptions<V extends Variables = Variables, T = unknown> = {
9696
? { variables?: V }
9797
: { variables: V })
9898

99-
export type BatchRequestsOptions<V extends Variables = Variables> = {
99+
export interface BatchRequestsOptions<V extends Variables = Variables> {
100100
documents: BatchRequestDocument<V>[]
101101
requestHeaders?: Dom.RequestInit['headers']
102102
signal?: Dom.RequestInit['signal']
@@ -110,9 +110,10 @@ export type RawRequestExtendedOptions<V extends Variables = Variables> = {
110110
url: string
111111
} & RawRequestOptions<V>
112112

113-
export type BatchRequestsExtendedOptions<V extends Variables = Variables> = {
113+
export interface BatchRequestsExtendedOptions<V extends Variables = Variables>
114+
extends BatchRequestsOptions<V> {
114115
url: string
115-
} & BatchRequestsOptions<V>
116+
}
116117

117118
export type RequestMiddleware<V extends Variables = Variables> = (
118119
request: RequestExtendedInit<V>

tests/__snapshots__/document-node.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`accepts graphql DocumentNode as alternative to raw string 1`] = `
44
{

tests/__snapshots__/gql.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`gql > passthrough allowing benefits of tooling for gql template tag 1`] = `
44
{

0 commit comments

Comments
 (0)