ECONN refused when doing apollo graphQL query from getInitialProps
See original GitHub issueBug 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.

To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- 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 || {})
})
- 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: {} }
})
}
- 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:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top 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 >
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 Free
Top 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
@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.
@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 useserviceName|IP:3000/graphql
in your network. o now I found next solution:Hard topic basically.