Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: remove duplicate code #271

Merged
merged 1 commit into from
Jun 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class GraphQLClient {
query: string,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<{ data?: T; extensions?: any; headers: Dom.Headers; status: number; }> {
): Promise<{ data: T; extensions?: any; headers: Dom.Headers; status: number }> {
let { headers, fetch: localFetch = crossFetch, ...others } = this.options
const body = createRequestBody(query, variables)

Expand All @@ -57,7 +57,7 @@ export class GraphQLClient {
...resolveHeaders(requestHeaders)
},
body,
...others,
...others
})

const result = await getResult(response)
Expand All @@ -82,32 +82,9 @@ export class GraphQLClient {
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T> {
let { headers, fetch: localFetch = crossFetch, ...others } = this.options
const resolvedDoc = resolveRequestDocument(document)
const body = createRequestBody(resolvedDoc, variables)

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

const result = await getResult(response)

if (response.ok && !result.errors && result.data) {
return result.data
} else {
const errorResult = typeof result === 'string' ? { error: result } : result
throw new ClientError(
{ ...errorResult, status: response.status, headers: response.headers },
{ query: resolvedDoc, variables }
)
}
const query = resolveRequestDocument(document)
const { data } = await this.rawRequest<T, V>(query, variables, requestHeaders)
return data
}

setHeaders(headers: Dom.RequestInit['headers']): GraphQLClient {
Expand Down Expand Up @@ -141,7 +118,7 @@ export async function rawRequest<T = any, V = Variables>(
query: string,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<{ data?: T; extensions?: any; headers: Dom.Headers; status: number; }> {
): Promise<{ data: T; extensions?: any; headers: Dom.Headers; status: number }> {
const client = new GraphQLClient(url)
return client.rawRequest<T, V>(query, variables, requestHeaders)
}
Expand Down Expand Up @@ -210,7 +187,6 @@ function getResult(response: Dom.Response): Promise<any> {

function resolveRequestDocument(document: RequestDocument): string {
if (typeof document === 'string') return document

return print(document)
}

Expand Down