question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

apollo-datasource-rest parseBody better check json

See original GitHub issue

Hi. 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:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
abernixcommented, Nov 27, 2018

This has been discussed recently on https://github.com/apollographql/apollo-server/pull/1645#issuecomment-439370832.

As parseBody is a protected method (that is to say, it can be implemented by any class which extends RESTDataSource), developers are free to implement override its default behavior in any way they’d like — including and up to allowing any Content-Type which contains json as necessitated above:

class DogAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "https://dog.ceo/api/";
  }

  parseBody(response) {
    if (response.headers.get('Content-Type').includes('json')) {
      return response.json();
    } else {
      return response.text();
    }
  }

  /* ... custom RESTDataSource methods ... */
}

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.

0reactions
vincent-vadecommented, May 7, 2019

This has been discussed recently on #1645 (comment).

As parseBody is a protected method (that is to say, it can be implemented by any class which extends RESTDataSource), developers are free to implement override its default behavior in any way they’d like — including and up to allowing any Content-Type which contains json as necessitated above:

class DogAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "https://dog.ceo/api/";
  }

  parseBody(response) {
    if (response.headers.get('Content-Type').includes('json')) {
      return response.json();
    } else {
      return response.text();
    }
  }

  /* ... custom RESTDataSource methods ... */
}

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found