【发布时间】:2018-06-16 20:06:30
【问题描述】:
我有这个架构和相应的解析器:
const schema = buildSchema(
`
type Query {
posts(id: Int): [Post]
}
type Post {
id: Int!,
title: String,
date: String
}`
);
const resolvers = {
posts(root, { id }, context, info) {
console.log(id); // Undefined
return [
{
id: 0,
date: '21/04/2018',
title: 'Post 1'
},
{
id: 1,
date: '07/10/2018',
title: 'Post 2'
}
];
},
Post(postObj) {
return {
id: postObj.id,
title: postObj.title,
date: postObj.date
}
}
}
问题是当我查询具有指定 id 的帖子时,像这样:
query {
posts(id: 0) {
title
}
}
...我收到一条错误消息,提示我尚未定义此类参数 (id)。
我根据GraphQL Docs定义了id参数。关于可能导致此错误的原因以及如何解决它的任何建议?
【问题讨论】:
标签: graphql-js