【发布时间】:2018-10-30 06:18:27
【问题描述】:
虽然我在 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
}
]
}
}
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"}
那么,它们几乎是相同的,你知道吗?
【问题讨论】:
-
你的变量是
schoolId并且查询它是$schoolID这可能是问题 -
我发现它应该是
schoolID而不是schoolId,我也试过$schoolID但得到了同样的错误,请参阅上面的编辑,它表明schoolID是正确的 var QUERY VARIABLES 部分中的名称.. 如何获取有关错误的更多详细信息? -
您使用的是哪个版本的
apollo-client?你应该为客户端配置一个缓存,比如apollo-cache-inmemory,如果你没有这样做就尝试使用它,通常你的客户端会抛出一个错误
标签: reactjs graphql apollo apollo-client