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.

ECONN refused when doing apollo graphQL query from getInitialProps

See original GitHub issue

Bug report

Describe the bug

Following the with-apollo-auth example Successful behavior when I do client-side ApolloConsumer query. I pass the client through and query works.

Unsuccessful behavior when I attempt to do a server side query through getInitialProps. I pass the client through a handler that calls the same query and I get a ECONN refused error as shown below.

screen shot 2019-01-27 at 11 32 24 pm

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. I set up my ApolloClient in the init-apollo file as follows:
  const httpLink = createHttpLink({
    uri: 'http://localhost:8080/api/graphql',
    credentials: 'same-origin'
  })

  const authLink = setContext((_, { headers }) => {
    const token = getToken()
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : ''
      }
    }
  })

  return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    link: ApolloLink.from([ authLink, httpLink ]),
    cache: new InMemoryCache().restore(initialState || {})
  })

  1. I set up my query as follows:
import gql from 'graphql-tag'

export default apolloClient => {
  apolloClient
    .query({
      query: gql`
        {
          getUser {
            id
          }
        }
      `
    })
    .then(({data}) => {
      console.log(data);
      return { userDetails: data.getUser }
    })
    .catch((err) => {
      // Fail gracefully
      console.log(err);
      return { userDetails: {} }
    })
  }
  1. I set up my getInitialProps function in the index.js file within the pages directory:
  import getUser from '../shared/services/get-user';

  static async getInitialProps (context) {
    const results = await getUser(context.apolloClient)
    console.log(results);
    return {}
  }

Expected behavior

I expect to see a log of the user returned, however I get an ECONNREFUSED error as shown above.

Screenshots

Screenshot above.

System information

  • OS: macOS Mojave
  • Browser (if applies) Chrome
  • Version of Next.js: 7.0.2
  • Version of ApolloClient: 2.4.12l

Additional context

  • Using Absinthe (Elixir+Phoenix) GraphQL API, url specified: http://localhost:8080/api/graphq
  • Using docker-compose to spin up multiple containers. API and NextJS client are in separate containers sitting behind nginx server. Nginx server routes to relevant container depending on the url endpoint specified. If url contains api, API container will be called.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Jan 28, 2019

@timneutkens How can it lose connection the moment I put it in getInitialProps is my question. I also noticed when I do async await on any function, it loses connection. As soon as I remove that, the backend call proceeds to execute.

1reaction
wwwebmancommented, May 8, 2019

@timneutkens, @K-JHC is right. Unfortunately if you are using docker-compose, containers can talk to each other by using service name or ip. So instead of using localhost:3000/graphql you have to use serviceName|IP:3000/graphql in your network. o now I found next solution:

  1. .env + dynamic client.urldepends on browser/server request (localhost:3000 || serviceName:3000) -> sounds like workaround for me 😦
  2. Some proxy or mappping serviceName < - > localhost inside containers.

Hard topic basically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECONN refused when doing apollo graphQL query ... - GitHub
Unsuccessful behavior when I attempt to do a server side query through getInitialProps. I pass the client through a handler that calls the...
Read more >
ECONNREFUSED when using apollo client to do graphQL ...
Unsuccessful behavior when I attempt to do a server side query through getInitialProps. I pass the client through a handler that calls the...
Read more >
Econnrefused When Using Apollo Client To Do Graphql Query ...
Now that we've set up Apollo Client we can integrate it into our React app. Now we're ready to build React components that...
Read more >
FetchError: request to http://localhost/graphql failed, reason ...
FetchError: request to http://localhost/graphql failed, reason: connect ECONNREFUSED 127.0.0.1:80 ... I tried to mutate the mutation on API routes ...
Read more >
Getting Started With Apollo Client in Next.js
Now we have a GraphQL client we can use within the application, and we're ready to query for some data. However, before we...
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