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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for get request #277

Merged
merged 1 commit into from Jul 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 60 additions & 14 deletions src/index.ts
@@ -1,7 +1,7 @@
import crossFetch, * as CrossFetch from 'cross-fetch'
import { print } from 'graphql/language/printer'
import createRequestBody from './createRequestBody'
import { ClientError, GraphQLError, RequestDocument, Variables } from './types'
import { ClientError, RequestDocument, Variables } from './types'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed GraphQLError as it is not in use

import * as Dom from './types.dom'

export { ClientError } from './types'
Expand Down Expand Up @@ -29,6 +29,62 @@ const resolveHeaders = (headers: Dom.RequestInit['headers']): Record<string, str
return oHeaders
}

/**
* Fetch data using POST method
*/
const post = async <V = Variables>(
url: string,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the way you declare functions to keep consistency but I'm not a huge fan of using too much args here and I spend much time to think if I should use a typed object here. Tell me if it is okay for you or if you want me to change this

query: string,
fetch: any,
options: Dom.RequestInit,
variables?: V,
headers?: HeadersInit,
requestHeaders?: Dom.RequestInit['headers'],
) => {
const body = createRequestBody(query, variables)

return await fetch(url, {
method: 'POST',
headers: {
...(typeof body === 'string' ? { 'Content-Type': 'application/json' } : {}),
...resolveHeaders(headers),
...resolveHeaders(requestHeaders)
},
body,
...options
})
}

/**
* Fetch data using GET method
*/
const get = async <V = Variables>(
url: string,
query: string,
fetch: any,
options: Dom.RequestInit,
variables?: V,
headers?: HeadersInit,
requestHeaders?: Dom.RequestInit['headers'],
) => {
const search: string[] = [
`query=${encodeURIComponent(query.replace(/([\s,]|#[^\n\r]+)+/g, ' ').trim())}`,
]

if (variables) {
search.push(`variables=${encodeURIComponent(JSON.stringify(variables))}`)
}

return await fetch(`${url}?${search.join('&')}`, {
method: 'GET',
headers: {
...resolveHeaders(headers),
...resolveHeaders(requestHeaders)
},
...options
})
}

/**
* todo
*/
Expand All @@ -46,20 +102,10 @@ export class GraphQLClient {
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<{ data: T; extensions?: any; headers: Dom.Headers; status: number }> {
let { headers, fetch: localFetch = crossFetch, ...others } = this.options
const body = createRequestBody(query, variables)

const response = await localFetch(this.url, {
method: 'POST',
headers: {
...(typeof body === 'string' ? { 'Content-Type': 'application/json' } : {}),
...resolveHeaders(headers),
...resolveHeaders(requestHeaders)
},
body,
...others
})
let { headers, fetch: localFetch = crossFetch, method = 'POST', ...others } = this.options

const fetcher = method.toUpperCase() === 'POST' ? post : get
const response = await fetcher(this.url, query, localFetch, others, variables, headers, requestHeaders)
const result = await getResult(response)

if (response.ok && !result.errors && result.data) {
Expand Down