Home > Software engineering >  Can not get data from Apollo server
Can not get data from Apollo server

Time:03-10

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:

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

CodePudding user response:

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

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