javascript - GraphQl null 从后端 Apollo 服务器检索数据
问题描述
嗨,我是 GraphQl 和 JavaScript 的新手。我有一个具有一些属性的书籍对象,包括每本书的作者。我想编写一个查询来返回由特定作者撰写的所有书籍,但我不能,从查询中得到的所有书籍都是空的,我搜索了几个小时似乎没有任何帮助。这是我的代码。谢谢你的帮助!。
let books = [
{
title: 'Clean Code',
published: 2008,
author: 'Robert Martin',
id: "afa5b6f4-344d-11e9-a414-719c6709cf3e",
genres: ['refactoring']
},
{
title: 'Agile software development',
published: 2002,
author: 'Robert Martin',
id: "afa5b6f5-344d-11e9-a414-719c6709cf3e",
genres: ['agile', 'patterns', 'design']
},
]
这是我的 GraphQl 架构
const typeDefs = gql`
type Book {
title: String!
published: Int!
author: String!
id: ID!
genres: [String!]!
}
type Query {
bookCount: Int!
allBooks(author: String!): Book
}
`
这是我的解析器,我试图从作者那里获取书籍。
const resolvers = {
Query: {
allBooks: (root, args) => {
books.map(book => book.author === args.author)
console.log(args.author);
},
bookCount: () => books.length,
}
}
最后,这就是我编写查询的方式。
query {
allBooks(author: "Joshua Kerievsky") {
title
}
}
解决方案
这就是我修复它的方法。
const resolvers = {
Query: {
allBooks: (root, args) => {
const booksToReturn = books.filter(book => book.author === args.author)
console.log(booksToReturn);
},
allAuthor: () => authors,
authorCount: () => authors.length,
bookCount: () => books.length,
}
}
推荐阅读
- php - 如何从 usnig Goutte Client for BotDetectCaptcha 获取 base64 中的媒体内容
- algorithm - 双向一对多映射的正确数据结构是什么?
- python - 如何在scrapy输出中保留换行符?
- javascript - 使用js和jq滚动播放和暂停两个或多个视频的代码
- html - 如何在调整 flexbox 上的浏览器高度时启用滚动
- android - Android Play 商店 - 将应用可用年龄从 3+ 更改为 18+
- shopware - 如何在 shopware 6 中的实体相关事件订阅者中访问 Shopware\Core\System\SalesChannel\SalesChannelContext?
- javascript - 错误:未捕获的类型错误:调用 storage.set 时出错(对象项,可选函数回调)Chrome 扩展
- r - 比较R中同一列中的值
- c - 如何在 Webots 中将无人机锁定在所需的高度?