我正在构建Apollo服务器。我有一个与Mongo通信的简单端点。有一系列的公告。
export const typeDefs = gql`
type Query {
announcements: [Announcement]
announcementsByAuthor(author: String!): [Announcement]
}
type Announcement {
_id: ID!
msg: String!
author: String!
title: String
}
`;
export const resolvers = {
Query: {
announcements: () => {
return new AnnouncementController().getAnnouncements();
},
announcementsByAuthor: (author: string) => {
console.log('RESOLVER: ', author);
return new AnnouncementController().getAnnouncementsByAuthor(author);
}
},
}
在我的graphiql界面中,公告查询正常工作:
{
announcements {
msg
author
}
}
announcesByAuthor查询似乎未从变量或将其硬编码到查询中时接受字符串参数。
query($author: String!){
announcementsByAuthor(author: $author) {
msg
author
}
}
变量:
{
"author":"Nate"
}
我已经从解析器注销,正在传递一个空字符串,而不是author变量的指定值。我是graphql的新手,我希望有人能启发我有关我确信这是一个简单的疏忽。
答案 0 :(得分:1)
尝试以下方法:
announcementsByAuthor: (doc, {author}) => {