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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest data source did recieve response #1325

Merged
merged 1 commit into from
Jul 11, 2018
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
19 changes: 12 additions & 7 deletions packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export abstract class RESTDataSource<TContext = any> {
}
}

protected async didReceiveResponse<TResult = any>(
response: Response,
): Promise<TResult> {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json')) {
return response.json();
} else {
return response.text() as Promise<any>;
}
}

protected async didReceiveErrorResponse<TResult = any>(
response: Response,
): Promise<TResult> {
Expand Down Expand Up @@ -156,13 +167,7 @@ export abstract class RESTDataSource<TContext = any> {
return this.trace(`${options.method || 'GET'} ${url}`, async () => {
const response = await this.httpCache.fetch(request);
if (response.ok) {
const contentType = response.headers.get('Content-Type');

if (contentType && contentType.startsWith('application/json')) {
return response.json();
} else {
return response.text();
}
return this.didReceiveResponse(response);
} else {
return this.didReceiveErrorResponse(response);
}
Expand Down