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

NetworkError: response.text is not a function #3051

Closed
mbrochh opened this issue Feb 21, 2018 · 8 comments
Closed

NetworkError: response.text is not a function #3051

mbrochh opened this issue Feb 21, 2018 · 8 comments

Comments

@mbrochh
Copy link

mbrochh commented Feb 21, 2018

Today, I ran an innocent little deployment, like every day, and suddenly my website was gone (thank god, staging server). All my apollo requests during SSR would return with the above mentioned NetworkError.

Here is what I had in my package.json:

    "apollo-client-preset": "^1.0.3",
    "apollo-fetch": "^0.6.0",
    "apollo-link-batch-http": "^1.0.1",
    "apollo-link-error": "^1.0.1",
    "apollo-cache-inmemory": "^1.1.1",
    "react-apollo": "^2.0.1",

After four hours of frustratingly digging through nginx & uwsgi logs and handcrafting gql requests with curl, I dawned on me that maybe my webserver and my backend are fine and somehow my node_modules environment might be messed up.

So I compared my local packages to the ones on the server via yarn list:

Here is what I had on my local machine, which works:

├─ [email protected]
│  ├─ apollo-cache@^1.0.1
│  ├─ apollo-utilities@^1.0.2
│  └─ graphql-anywhere@^4.0.1
├─ [email protected]
│  └─ apollo-utilities@^1.0.2
├─ [email protected]
│  ├─ apollo-cache-inmemory@^1.1.1
│  ├─ apollo-client@^2.0.3
│  ├─ [email protected]
│  ├─ [email protected]
│  └─ graphql-tag@^2.4.2
├─ [email protected]
│  ├─ @types/[email protected]
│  ├─ @types/zen-observable@^0.5.3
│  ├─ apollo-cache@^1.0.1
│  ├─ apollo-link-dedup@^1.0.0
│  ├─ apollo-link@^1.0.0
│  ├─ apollo-utilities@^1.0.2
│  ├─ symbol-observable@^1.0.2
│  └─ zen-observable@^0.6.0
├─ [email protected]
│  └─ isomorphic-fetch@^2.2.1
├─ [email protected]
│  ├─ apollo-fetch@^0.6.0
│  ├─ apollo-link-batch@^1.0.1
│  └─ graphql@^0.11.0
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
│  ├─ apollo-utilities@^0.2.0-beta.0
│  ├─ [email protected]
│  └─ zen-observable@^0.6.0
├─ [email protected]

And here is what I had on the server, which does not work:

├─ [email protected]
│  ├─ apollo-cache@^1.1.4
│  ├─ apollo-utilities@^1.0.8
│  └─ graphql-anywhere@^4.1.5
├─ [email protected]
│  └─ apollo-utilities@^1.0.8
├─ [email protected]
│  ├─ apollo-cache-inmemory@^1.1.7
│  ├─ apollo-client@^2.2.2
│  ├─ apollo-link-http@^1.3.1
│  ├─ apollo-link@^1.0.6
│  └─ graphql-tag@^2.4.2
├─ [email protected]
│  ├─ @types/[email protected]
│  ├─ @types/zen-observable@^0.5.3
│  ├─ apollo-cache@^1.1.4
│  ├─ apollo-link-dedup@^1.0.0
│  ├─ apollo-link@^1.0.0
│  ├─ apollo-utilities@^1.0.8
│  ├─ symbol-observable@^1.0.2
│  └─ zen-observable@^0.7.0
├─ [email protected]
│  └─ isomorphic-fetch@^2.2.1
├─ [email protected]
│  ├─ apollo-link-batch@^1.1.0
│  ├─ apollo-link-http-common@^0.2.1
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  ├─ apollo-link-http-common@^0.2.1
│  └─ apollo-link@^1.2.0
├─ [email protected]
│  ├─ @types/node@^9.4.6
│  ├─ @types/[email protected]
│  ├─ apollo-utilities@^1.0.0
│  ├─ zen-observable@^0.8.0
│  └─ [email protected]
├─ [email protected]

To fix this:

I simply pinned all the working versions in my package.json to the exact versions (I added the packages that were not in my package.json and I removed the ^ from all version numbers). Then I wiped node_modules on the server and made a new deployment.

What I don't understand:

Both, on my MacBook and on the Ubuntu server I have:

  • node version is 9.0.4
  • yarn version is 1.3.2
  • npm version is 5.6.0

How can it be that yarn install installs packages with vastly different version numbers on both machines? This is the fist time I ended up in dependency hell like this, I never had to remove the ^ from a package.json, until now.

@mbrochh
Copy link
Author

mbrochh commented Feb 21, 2018

Alright. I made the final edit for the initial post. For me, the problem is solved, but only because I reverted to old versions. Anyone who wants to be on the bleeding edge of all things Apollo will probably run into this issue as well.

The bug must be in one of those packages above that has a higher version number than what I had on my local machine, I suspect apollo-link-http-common or apollo-link-http or apollo-link-batch-http, but really don't know what exactly causes the issue.

@pencilcheck
Copy link

Getting the same issue as well, not sure what is causing it.

However the versions that worked for me

    "apollo-cache-inmemory": "1.1.7",
    "apollo-client": "2.0.4",
    "apollo-link": "1.1.0",
    "apollo-link-http": "1.3.3",
    "apollo-link-context": "1.0.6",
    "react-apollo": "2.0.4",

Also, removed custom fetch for the apollo link

@TiE23
Copy link

TiE23 commented Apr 17, 2018

I'm sorry that my story won't help OP but might assist those with this error experiencing issues trying to write a custom fetch for Apollo Link.

I was having the same error Network error: response.text is not a function when I was simply trying to create a custom fetch for apollo link (inspired by this article). I solved it by making a simple change to the customFetch, where the article writer was dealing with a REST api that returned JSON in the response, whereas GraphQL returns text.

const customFetch = (uri, options) => {
  const initialRequest = fetch(uri, options);

  return initialRequest.then(response => (
    response.text()
  )).then((text) => {
    // ... If there is an error do something special HERE
    // ... Just passing things through as "OK"
    const result = {};
    result.ok = true;
    result.json = () => new Promise((resolve, reject) => {
      resolve(text);
    });
    return result;
  });
};

const httpLink = createHttpLink({
  uri: "http://localhost:4000",
  fetch: customFetch,
});

@jhd124
Copy link

jhd124 commented May 19, 2018

look at this: https://developer.mozilla.org/en-US/docs/Web/API/Body/text.
the response of fetcher should own a text method.
In the module "apollo-link-http"

return new apolloLink.Observable(function (observer) {
              console.log('options', options)
                fetcher(chosenURI, options)
                    .then(function (response) {
                      console.log('before', response)
                    operation.setContext({ response: response });
                    console.log('after', operation)
                    return response;
                })
                    .then(apolloLinkHttpCommon.parseAndCheckHttpResponse(operation))
                    .then(function (result) {
                      console.log('result',result)
                    // we have data and can send it to back up the link chain
                    observer.next(result);
                    observer.complete();
                    return result;
                })
...

the error occurs at .then(apolloLinkHttpCommon.parseAndCheckHttpResponse(operation))
which calls the method response.text

@hinok
Copy link

hinok commented Jun 11, 2018

Maybe it will help someone so in my case it was matter of downgrading apollo-link-batch-http to 1.0.4.

$ npm ls --depth=0 | grep apollo
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

@hwillson
Copy link
Member

hwillson commented Aug 5, 2018

It sounds like the common thread here is apollo-link-batch-http. There have been many new versions released since the versions tried here however, so if anyone is still encountering this issue and is using a recent version of apollo-link-batch-http, please let us know. Closing for now - thanks!

@hwillson hwillson closed this as completed Aug 5, 2018
@siowyisheng
Copy link

Upgrading apollo-link-batch-http to 1.2.2 continues to give me this error.

@mbrochh
Copy link
Author

mbrochh commented Jan 14, 2019

I'm also still getting this error. Just tried to upgrade all apollo related packages to the latest versions (according to yarn outdated) and then I had to downgrade apollo-link-batch-http back to 1.0.4 to get rid of this error.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants