无法从 Apollo 服务器获取数据
Can not get data from Apollo server
我卡住了。我无法从服务器获取数据。 (服务器正在 100% 工作)。但我在请求数据时遇到了问题。
这是最小化代码:
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>
)
}
存储库列表:
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 <></>
}
控制台给出未定义的数据,错误是:网络连接失败。
我尝试继续 http://localhost:4000/graphql 和 运行 查询。一切正常
尝试将 http://localhost:4000
更改为 http://10.0.2.2:4000/
const httpLink = createHttpLink({
uri: 'http://10.0.2.2:4000/graphql',
})
我卡住了。我无法从服务器获取数据。 (服务器正在 100% 工作)。但我在请求数据时遇到了问题。
这是最小化代码:
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>
)
}
存储库列表:
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 <></>
}
控制台给出未定义的数据,错误是:网络连接失败。 我尝试继续 http://localhost:4000/graphql 和 运行 查询。一切正常
尝试将 http://localhost:4000
更改为 http://10.0.2.2:4000/
const httpLink = createHttpLink({
uri: 'http://10.0.2.2:4000/graphql',
})