Skip to content

Commit

Permalink
feat(types): add type param for variables (#143)
Browse files Browse the repository at this point in the history
closes #177

Co-authored-by: Jason Kuhrt <[email protected]>
  • Loading branch information
polRk and jasonkuhrt authored Aug 3, 2020
1 parent 49187dd commit 5ceb388
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
34 changes: 11 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from 'cross-fetch'
import { ClientError, GraphQLError, Variables } from './types'
import { Request, RequestInit, Response } from './types.dom'
import { RequestInit, Response } from './types.dom'

export { ClientError } from './types'

Expand All @@ -13,16 +13,10 @@ export class GraphQLClient {
this.options = options || {}
}

async rawRequest<T = any>(
async rawRequest<T = any, V = Variables>(
query: string,
variables?: Variables
): Promise<{
data?: T
extensions?: any
headers: Request['headers']
status: number
errors?: GraphQLError[]
}> {
variables?: V
): Promise<{ data?: T; extensions?: any; headers: Headers; status: number; errors?: GraphQLError[] }> {
const { headers, ...others } = this.options

const body = JSON.stringify({
Expand Down Expand Up @@ -51,7 +45,7 @@ export class GraphQLClient {
}
}

async request<T = any>(query: string, variables?: Variables): Promise<T> {
async request<T = any, V = Variables>(query: string, variables?: V): Promise<T> {
const { headers, ...others } = this.options

const body = JSON.stringify({
Expand Down Expand Up @@ -96,26 +90,20 @@ export class GraphQLClient {
}
}

export function rawRequest<T = any>(
export async function rawRequest<T = any, V = Variables>(
url: string,
query: string,
variables?: Variables
): Promise<{
data?: T
extensions?: any
headers: Request['headers']
status: number
errors?: GraphQLError[]
}> {
variables?: V
): Promise<{ data?: T; extensions?: any; headers: Headers; status: number; errors?: GraphQLError[] }> {
const client = new GraphQLClient(url)

return client.rawRequest<T>(query, variables)
return client.rawRequest<T, V>(query, variables)
}

export function request<T = any>(url: string, query: string, variables?: Variables): Promise<T> {
export async function request<T = any, V = Variables>(url: string, query: string, variables?: V): Promise<T> {
const client = new GraphQLClient(url)

return client.request<T>(query, variables)
return client.request<T, V>(query, variables)
}

export default request
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export interface GraphQLError {
path: string[]
}

export interface GraphQLResponse {
data?: any
export interface GraphQLResponse<T = any> {
data?: T
errors?: GraphQLError[]
extensions?: any
status: number
[key: string]: any
}

export interface GraphQLRequestContext {
export interface GraphQLRequestContext<V = Variables> {
query: string
variables?: Variables
variables?: V
}

export class ClientError extends Error {
Expand Down

0 comments on commit 5ceb388

Please sign in to comment.