apollo-datasource-rest parseBody better check json
See original GitHub issueHi. I noticed that apollo-datasource-rest
parse my response’s body as text. I checked with the source code, and this is how the library is currently checking:
protected parseBody(response: Response): Promise<object | string> {
const contentType = response.headers.get('Content-Type');
if (
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
) {
return response.json();
} else {
return response.text();
}
}
I was able to override that by changing the if condition with contentType.includes("json")
. I wonder if we want to change the library itself, since there are APIs that respond with different content type header.
My use case is Contentful API, which return application/some-contentful-meta-data+json
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Fetching from REST - Apollo GraphQL Docs
Using RESTDataSource to fetch data from REST APIs. See the @apollo/datasource-rest page for the full details of the RESTDataSource API. The ...
Read more >apollo-datasource-rest | Yarn - Package Manager
This package exports a ( RESTDataSource ) class which is used for fetching data from a REST API and exposing it via GraphQL...
Read more >__tests__/OneRequest.DataSources.test.ts · skava ... - Gemfury
Learn more » Push, build, and install RubyGems npm packages Python packages Maven ... /packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts ...
Read more >Is it valid to return null from a didReceiveResponse callback?
parseBody (response) as any) as Promise<TResult>; } else { throw await this. ... import { RESTDataSource } from 'apollo-datasource-rest'; ...
Read more >apollo-datasource-rest - npm
See https://www.apollographql.com/docs/apollo-server/previous-versions/ for more details. apollo-datasource-rest. TypeScript icon, indicating ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This has been discussed recently on https://github.com/apollographql/apollo-server/pull/1645#issuecomment-439370832.
As
parseBody
is aprotected
method (that is to say, it can be implemented by any class whichextends RESTDataSource
), developers are free to implement override its default behavior in any way they’d like — including and up to allowing anyContent-Type
which containsjson
as necessitated above:Also, if we were to go the route of
jsonContentTypes
, we’d quickly end up with a long laundry list of other content-types (e.g.xmlContentTypes
,protobufContentTypes
, etc.).I believe this provides the best abstraction without overlooking specific implementation details (and often time, meaningful differences) between various content types which might appear similar on the surface.
Thank’s