虽然我在 GraphiQL 成功测试后从 GraphiQL 工具复制并粘贴了 graphQL 查询,但当我在 reactJS 应用程序的 Apollo 客户端中尝试它时,查询返回错误:
[GraphQL error]: Message: Cannot query field "allStudents" on type "Query"., Location: [object Object], Path: undefined
这是我的实现:
const link = createHttpLink({
uri: 'http://localhost:8000/graphql',
fetchOptions: { method: "POST" }
});
const client = new ApolloClient({
link: link
});
const GET_STUDENTS = gql`
query getStudents($schoolID: Int!){
allStudents(schoolId: $schoolID){
pickUpLat
pickUpLng
}
}
`;
client
.query({
query: GET_STUDENTS,
variables: { schoolID: 1 }
})
.then(result => console.log(result));
有什么问题?这是我期望的正确响应:
{
"data": {
"allStudents": [
{
"pickUpLat": 31.9752942479727,
"pickUpLng": 35.8438429235775
},
{
"pickUpLat": 31.9754545979993,
"pickUpLng": 35.8437478537235
}
]
}
}
编辑2
我试图比较我的请求和 GraphiQL 请求之间的有效负载:
我的请求的有效负载:(它有 __typename
我不知道为什么)
{"operationName":"getStudents","variables":{"schoolID":1},"query":"query getStudents($schoolID: Int) {\n allStudents(schoolId: $schoolID) {\n pickUpLat\n pickUpLng\n __typename\n }\n}\n"}
GraphiQL 请求的负载:
{"query":"query getStudents($schoolID: Int!){\n allStudents(schoolId: $schoolID){\n pickUpLat\n pickUpLng\n }\n}","variables":{"schoolID":1},"operationName":"getStudents"}
所以,它们几乎是相同的,知道吗?
原文由 simo 发布,翻译遵循 CC BY-SA 4.0 许可协议
我查询的错误是我没有下载新模式。
您可以使用以下方式下载架构:
apollo schema:download --endpoint=http://localhost:8080/graphql schema.json
将 http://localhost:8080/graphql 替换为您的服务器端点
你可以在 https://www.apollographql.com/docs/ios/downloading-schema/ 看到更多