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

Enables parsing of application/hal+json as JSON #1853

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Allow an optional function to resolve the `rootValue`, passing the `DocumentNode` AST to determine the value. [PR #1555](https://github.com/apollographql/apollo-server/pull/1555)
- Follow-up on the work in [PR #1516](https://github.com/apollographql/apollo-server/pull/1516) to also fix missing insertion cursor/caret when a custom GraphQL configuration is specified which doesn't specify its own `cursorShape` property. [PR #1607](https://github.com/apollographql/apollo-server/pull/1607)
- Allow JSON parsing in `RESTDataSource` of Content Type `application/hal+json`. [PR ##185](https://github.com/apollographql/apollo-server/pull/1853)

### v2.1.0

Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {

protected parseBody(response: Response): Promise<object | string> {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json')) {
if (
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
) {
return response.json();
} else {
return response.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ describe('RESTDataSource', () => {
expect(data).toEqual({ foo: 'bar' });
});

it('returns data as parsed JSON when Content-Type is application/hal+json', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';

getFoo() {
return this.get('foo');
}
}();

dataSource.httpCache = httpCache;

fetch.mockJSONResponseOnce(
{ foo: 'bar' },
{ 'Content-Type': 'application/hal+json' },
);

const data = await dataSource.getFoo();

expect(data).toEqual({ foo: 'bar' });
});

it('returns data as a string when Content-Type is text/plain', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';
Expand Down