mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
Enables parsing of application/hal+json
as JSON in RESTDataSource
(#1853)
This commit is contained in:
parent
c1b0af18be
commit
458bc71ead
3 changed files with 27 additions and 1 deletions
|
@ -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)
|
- 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)
|
- 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
|
### v2.1.0
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,11 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
|
||||||
|
|
||||||
protected parseBody(response: Response): Promise<object | string> {
|
protected parseBody(response: Response): Promise<object | string> {
|
||||||
const contentType = response.headers.get('Content-Type');
|
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();
|
return response.json();
|
||||||
} else {
|
} else {
|
||||||
return response.text();
|
return response.text();
|
||||||
|
|
|
@ -392,6 +392,27 @@ describe('RESTDataSource', () => {
|
||||||
expect(data).toEqual({ foo: 'bar' });
|
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 () => {
|
it('returns data as a string when Content-Type is text/plain', async () => {
|
||||||
const dataSource = new class extends RESTDataSource {
|
const dataSource = new class extends RESTDataSource {
|
||||||
baseURL = 'https://api.example.com';
|
baseURL = 'https://api.example.com';
|
||||||
|
|
Loading…
Add table
Reference in a new issue