const httpLink = createHttpLink({
uri: 'http://localhost:3090/'
})
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache()
})
client.query({
query: gql`
query users {
email
}
`,
})
.then(data => console.log(data))
.catch(error => console.error(error));
从客户端代码获取时,此查询给出了错误,但是当我在http:// localhost:3090 / graphql的浏览器中执行此查询时,它将正确获取数据
你要向其发布查询的graphql端点缺少/graphql
。所以,你的服务器可能返回一个包含开头的404错误消息的html文件<
的<html...
。Apollo尝试将其解析为查询结果,但没有这样做。
检查httpLink
是否确实如此localhost:3090/graphql
。
查询的语法也可以是:
{
users {
email
}
}
或者,如果你想命名查询:
query Users {
users {
email
}
}
@MukeshKumar,请在您的问题中添加完整的错误消息。
添加/ graphql ..之后?
@MukeshKumar是的。我不能告诉你为什么它没有消息就会失败。
POST本地主机:3090 / graphql 400(错误请求)index.js:1452错误:网络错误:响应不成功:在QueryManager.js:522在QueryManager.js的新ApolloError(ApolloError.js:58)处收到状态代码400: 982在Array.forEach(<anonymous>)在QueryManager.js:981在Map.forEach(<anonymous>)在QueryManager.broadcastQueries(QueryManager.js:977)在QueryManager.js:477
我很抱歉,@ MukeshKumar不包含任何有用的信息。但我认为您查询的语法也不正确。它必须是公正
gql`{ users { email } }
或qgl`query Users { users { email } }`
。