Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not get data from Apollo server

I’m stuck. I can not get data from server. (server is working 100%). but I got issues with requesting data.

this is minimized code:

import { ApolloClient, InMemoryCache, createHttpLink, ApolloProvider } from '@apollo/client'
import RepositoryList from './RepositoryList'

const httpLink = createHttpLink({
  uri: 'http://localhost:4000/graphql',
})

const createApolloClient = () => {
  return new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache(),
  })
}

const apolloClient = createApolloClient()

export default App = () => {
  return(
    <ApolloProvider client={apolloClient}>
      <RepositoryList />
    </ApolloProvider>
  )
}

Repository List:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { useQuery, gql } from '@apollo/client'

const GET_REPOSITORIES = gql`
  query {
    repositories {
      fullName
      description
    }
  }
`

const RepositoryList = () => {
  const { data, error, loading } = useQuery(GET_REPOSITORIES)
  console.log('data', data) //log: undefined
  console.log('error', error) //log: [Error: Network request failed]
  console.log('loading', loading) //log: false
  return <></>
}

Console gives data undefined and Error is: Network connection failed.
I tried to go on http://localhost:4000/graphql and run query. Everything was working perfectly

>Solution :

Try changing changing http://localhost:4000 by http://10.0.2.2:4000/

const httpLink = createHttpLink({
  uri: 'http://10.0.2.2:4000/graphql',
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading