尽管在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
}
]
}
}
编辑
使用GraphiQL我得到了预期的结果:
EDIT2
我试图比较我的请求和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"}
因此,它们几乎相同,有什么想法吗?
最佳答案
对于可能正在搜索此问题的其他任何人,请确保您是从 ApolloClient
软件包而不是其他软件包中导入 apollo-client
的。