Apollo-client: Check for bodyUsed before resolving

Created on 22 May 2017  路  2Comments  路  Source: apollographql/apollo-client

Hi,
I'm trying to implement an afterware that handles errors globally in my application. My backend is Ruby/Rails and on error I return an error key in the document. In order to check for the presence of errors, I need to parse the body stream using response.json()

Upon doing that, I get the following error:

Unhandled (in react-apollo) Error: Network error: Already read

I suspect it's because when the response is further channelled into the apollo handling, json() is called on it again.
I think checking bodyUsed on the response would solve this issue.

How to reproduce:

import { ApolloClient, createNetworkInterface } from 'react-apollo'
const networkInterface = createNetworkInterface({uri})

const handleErrors = ({ response }, next) => {
  response.json().then(json => {
      // do stuff
  })
  next()
}

networkInterface.useAfter([{
  applyAfterware: handleErrors,
}])

const client = new ApolloClient({networkInterface})

Thanks!

Most helpful comment

I like it, thanks!

All 2 comments

You have to clone the response in order to parse it 馃檪

const handleErrors = ({ response }, next) => {
  const responseClone = response.clone();
  responseClone.json().then(json => {});

I like it, thanks!

Was this page helpful?
0 / 5 - 0 ratings