Skip to content

Commit

Permalink
fix: batchRequests type constraint (#462)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
renovate[bot] committed Feb 25, 2023
1 parent 0fb7062 commit d5a4012
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ Minimal GraphQL client supporting Node and browsers for scripts or simple apps
- [Ignore](#ignore)
- [All](#all)
- [FAQ](#faq)
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
- [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)
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
- [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)
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)

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

Expand Down
2 changes: 1 addition & 1 deletion examples/typed-document-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLClient,request } from '../src/index.js'
import { GraphQLClient, request } from '../src/index.js'
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { parse } from 'graphql'
;(async function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"prettier": "^2.8.3",
"type-fest": "^3.5.3",
"typescript": "^4.9.4",
"vitest": "^0.28.2",
"vitest": "^0.29.0",
"ws": "^8.12.0"
},
"prettier": "@prisma-labs/prettier-config"
Expand Down
118 changes: 116 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/graphql-ws.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolveRequestDocument } from './resolveRequestDocument.js'
import type * as Dom from './types.dom.js'
import type { RequestDocument, Variables } from './types.js';
import type { RequestDocument, Variables } from './types.js'
import { ClientError } from './types.js'
// import type WebSocket from 'ws'

Expand Down
61 changes: 39 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { defaultJsonSerializer } from './defaultJsonSerializer.js'
import { HeadersInstanceToPlainObject, uppercase } from './helpers.js'
import {
parseBatchRequestArgs,
parseBatchRequestsExtendedArgs,
parseRawRequestArgs,
parseRawRequestExtendedArgs,
parseRequestArgs,
Expand Down Expand Up @@ -33,6 +32,7 @@ import {
} from './types.js'
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import crossFetch, * as CrossFetch from 'cross-fetch'
import type { T, V } from 'vitest/dist/types-7cd96283.js'

export {
BatchRequestDocument,
Expand Down Expand Up @@ -310,12 +310,12 @@ export class GraphQLClient {
/**
* Send GraphQL documents in batch to the server.
*/
batchRequests<T = unknown, V extends Variables = Variables>(
documents: BatchRequestDocument<V>[],
requestHeaders?: Dom.RequestInit['headers']
): Promise<T>
batchRequests<T = unknown, V extends Variables = Variables>(options: BatchRequestsOptions<V>): Promise<T>
batchRequests<T = unknown, V extends Variables = Variables>(
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsOptions<V>): Promise<T>
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(
documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T> {
Expand Down Expand Up @@ -573,22 +573,39 @@ export async function request<T, V extends Variables = Variables>(
* await batchRequests('https://foo.bar/graphql', [{ query: gql`...` }])
* ```
*/
export async function batchRequests<T, V extends Variables = Variables>(
url: string,
documents: BatchRequestDocument<V>[],
requestHeaders?: Dom.RequestInit['headers']
): Promise<T>
export async function batchRequests<T, V extends Variables = Variables>(
options: BatchRequestsExtendedOptions<V>
): Promise<T>
export async function batchRequests<T, V extends Variables = Variables>(
urlOrOptions: string | BatchRequestsExtendedOptions<V>,
documents?: BatchRequestDocument<V>[],
requestHeaders?: Dom.RequestInit['headers']
): Promise<T> {
const params = parseBatchRequestsExtendedArgs<V>(urlOrOptions, documents, requestHeaders)
export const batchRequests: BatchRequests = async (...args: BatchRequestsArgs) => {
const params = parseBatchRequestsArgsExtended(args)
const client = new GraphQLClient(params.url)
return client.batchRequests<T, V>(params)
return client.batchRequests(params)
}

interface Result<Data extends object = object> {
data: Data
}

type BatchResult = [Result, ...Result[]]

// prettier-ignore
interface BatchRequests {
<T extends BatchResult, V extends Variables = Variables>(url: string, documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>
<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsExtendedOptions<V>): Promise<T>
}

type BatchRequestsArgs =
| [url: string, documents: BatchRequestDocument[], requestHeaders?: Dom.RequestInit['headers']]
| [options: BatchRequestsExtendedOptions]

const parseBatchRequestsArgsExtended = (args: BatchRequestsArgs): BatchRequestsExtendedOptions => {
if (args.length === 1) {
return args[0]
} else {
return {
url: args[0],
documents: args[1],
requestHeaders: args[2],
signal: undefined,
}
}
}

export default request
Expand Down
15 changes: 0 additions & 15 deletions src/parseArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,3 @@ export const parseRawRequestExtendedArgs = <V extends Variables = Variables>(
signal: undefined,
} as unknown as RawRequestExtendedOptions<V>)
}

export const parseBatchRequestsExtendedArgs = <V extends Variables = Variables>(
urlOrOptions: string | BatchRequestsExtendedOptions<V>,
documents?: BatchRequestDocument<V>[],
requestHeaders?: Dom.RequestInit['headers']
): BatchRequestsExtendedOptions<V> => {
return (urlOrOptions as BatchRequestsExtendedOptions<V>).documents
? (urlOrOptions as BatchRequestsExtendedOptions<V>)
: {
url: urlOrOptions as string,
documents: documents as BatchRequestDocument<V>[],
requestHeaders: requestHeaders,
signal: undefined,
}
}
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export type RequestOptions<V extends Variables = Variables, T = unknown> = {
? { variables?: V }
: { variables: V })

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

export type BatchRequestsExtendedOptions<V extends Variables = Variables> = {
export interface BatchRequestsExtendedOptions<V extends Variables = Variables>
extends BatchRequestsOptions<V> {
url: string
} & BatchRequestsOptions<V>
}

export type RequestMiddleware<V extends Variables = Variables> = (
request: RequestExtendedInit<V>
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/document-node.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`accepts graphql DocumentNode as alternative to raw string 1`] = `
{
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/gql.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`gql > passthrough allowing benefits of tooling for gql template tag 1`] = `
{
Expand Down
7 changes: 3 additions & 4 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLClient, rawRequest, request } from '../src/index.js'
import type * as Dom from '../src/types.dom.js'
import type { RequestConfig } from '../src/types.js'
import { setupMockServer } from './__helpers.js'
import { gql } from 'graphql-tag'
Expand Down Expand Up @@ -203,7 +202,7 @@ describe(`middleware`, () => {
})

it(`batchRequests`, async () => {
const requestPromise = client.batchRequests<{ result: number }>([{ document: `x` }])
const requestPromise = client.batchRequests([{ document: `x` }])
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
expect(responseMiddleware).toBeCalledTimes(1)
Expand Down Expand Up @@ -250,7 +249,7 @@ describe(`middleware`, () => {
})

it(`batchRequests`, async () => {
const requestPromise = client.batchRequests<{ result: number }>([{ document: `x` }])
const requestPromise = client.batchRequests([{ document: `x` }])
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
})
Expand Down Expand Up @@ -295,7 +294,7 @@ describe(`middleware`, () => {
})

it(`batchRequests`, async () => {
const requestPromise = client.batchRequests<{ result: number }>([{ document: `x` }])
const requestPromise = client.batchRequests([{ document: `x` }])
expect(requestMiddleware).toBeCalledTimes(1)
await expect(requestPromise).rejects.toThrowError()
expect(responseMiddleware).toBeCalledTimes(1)
Expand Down

0 comments on commit d5a4012

Please sign in to comment.