Skip to content

Commit

Permalink
fix(typed-document-node): accurate typings for client.request() and…
Browse files Browse the repository at this point in the history
… do not enforce strict variable typing for `string` document (#390)
  • Loading branch information
charlypoly authored Sep 22, 2022
1 parent 6246ffe commit d5599b9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
20 changes: 19 additions & 1 deletion examples/typed-document-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { parse } from 'graphql'

import { request } from '../src'
import { request, GraphQLClient } from '../src'
;(async function () {
const endpoint = 'https://graphql-yoga.com/api/graphql'

Expand All @@ -17,3 +17,21 @@ import { request } from '../src'

console.log(data.greetings)
})().catch(console.error)
;(async function () {
const endpoint = 'https://graphql-yoga.com/api/graphql'

const client = new GraphQLClient(endpoint)

const query: TypedDocumentNode<{ greetings: string }, never | Record<any, never>> = parse(/* GraphQL */ `
query greetings {
greetings
}
`)

const variables = {}

const data = await client.request({ document: query })
// const data = await client.request({ document: query, variables: { a: 1 } })

console.log(data.greetings)
})().catch(console.error)
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,19 @@ export class GraphQLClient {
* Send a GraphQL document to the server.
*/
request<T = any, V = Variables>(
document: RequestDocument | TypedDocumentNode<T, V>,
document: string,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T>
request<T = any, V = Variables>(
document: DocumentNode | TypedDocumentNode<T, V>,
..._variablesAndRequestHeaders: V extends Record<any, never> // do we have explicitly no variables allowed?
? [variables?: V, requestHeaders?: Dom.RequestInit['headers']]
: keyof RemoveIndex<V> extends never // do we get an empty variables object?
? [variables?: V, requestHeaders?: Dom.RequestInit['headers']]
: [variables: V, requestHeaders?: Dom.RequestInit['headers']]
): Promise<T>
request<T = any, V = Variables>(options: RequestOptions<V>): Promise<T>
request<T = any, V = Variables>(options: RequestOptions<V, T>): Promise<T>
request<T = any, V = Variables>(
documentOrOptions: RequestDocument | TypedDocumentNode<T, V> | RequestOptions<V>,
...variablesAndRequestHeaders: V extends Record<any, never> // do we have explicitly no variables allowed?
Expand Down Expand Up @@ -562,13 +567,19 @@ export async function rawRequest<T = any, V = Variables>(
*/
export async function request<T = any, V = Variables>(
url: string,
document: RequestDocument | TypedDocumentNode<T, V>,
document: DocumentNode | TypedDocumentNode<T, V>,
..._variablesAndRequestHeaders: V extends Record<any, never> // do we have explicitly no variables allowed?
? [variables?: V, requestHeaders?: Dom.RequestInit['headers']]
: keyof RemoveIndex<V> extends never // do we get an empty variables object?
? [variables?: V, requestHeaders?: Dom.RequestInit['headers']]
: [variables: V, requestHeaders?: Dom.RequestInit['headers']]
): Promise<T>
export async function request<T = any, V = Variables>(
url: string,
document: string,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T>
export async function request<T = any, V = Variables>(options: RequestExtendedOptions<V, T>): Promise<T>
export async function request<T = any, V = Variables>(
urlOrOptions: string | RequestExtendedOptions<V, T>,
Expand Down
6 changes: 6 additions & 0 deletions tests/typed-document-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ test('typed-document-node code should TS compile', async () => {
variables: { str: 1 },
})

await request({
url: ctx.url,
document: 'a graphql query',
variables: { whatever: 'not typed' },
})

await request({
url: ctx.url,
document,
Expand Down

0 comments on commit d5599b9

Please sign in to comment.