node.js - 如何在 Graphql Schema 中表示 mongoose 的 objectID 数组
问题描述
我正在尝试使用节点和猫鼬将休息后端转换为使用 GraphQL,但我不知道如何在 graphql 模式中表示类似于猫鼬模式的 objectID 数组的东西。我的疑问,我如何让这个想法成为章节 ObjectIds 的列表?没找到类似的
猫鼬模式
const mongoose = require('../database/db')
const BookSchema = new mongoose.Schema({
name: {
type: String,
require: true,
},
author: {
type: String
},
sinopsis: {
type: String
},
chapters:[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Chapter'
}],
}, {
timestamps: true,
})
const ChapterSchema = new mongoose.Schema({
name: {
type: String
},
number: {
type:Number
},
content: {
type: String
},
}, {
timestamps: true,
})
const Chapter = mongoose.model('Chapter', ChapterSchema)
const Book = mongoose.model('Book', BookSchema)
module.exports.Book = Book
module.exports.Chapter = Chapter
GraphQL 架构:
type Book {
id: ID!
name: String!
author: String
sinopsis: String
chapters: [ID] (here is my doubt, how do i make this think be a list of Chapter ObjectIds)
}
type Chapter {
id: ID!
name: String!
number: String!
content: String!
}
type Query{
books: [Book!]!
book(id: ID!): Book
bookChapters(id: ID!): [ID]
chapter(id: ID!): Chapter!
}
type Mutation {
createBook(name: String!, author: String!,
sinopsis: String!, chapters: [ID!]): Book
createChapter(name: String!, number:String!,
content:String!): Chapter
}
这是我得到的错误。
{
"error": {
"errors": [
{
"message": "Field \"chapters\" must not have a selection since type \"[ID]\" has no subfields.",
"locations": [
{
"line": 3,
"column": 14
}
]
}
]
}
}
BookResolver.js
const db = require('../models/Book')
module.exports = {
Query:{
books: () => db.Book.find(),
book: (_,{id}) => db.Book.findById(id),
bookChapters: (_,{id}) => {
const book = db.Book.findById(id)
/* console.log(book + "aaa") */
const chapterlist = db.Chapter.find(book)
return chapterlist
},
chapter: (_,{id}) => db.Chapter.findById(id),
},
Mutation:{
createBook: (_, {name, author, sinopsis}) => db.Book.create({name, author, sinopsis}),
createChapter: (_, {name, number, content }) => {
db.Chapter.create({name, number, content})
},
},
}
解决方案
找到适合我的问题的答案,这是最终档案
图书解析器:
const db = require('../models/Book')
const ObjectId = require('mongodb').ObjectID;
const prepare = (o) => {
o._id = o._id.toString()
return o
}
module.exports = {
Query:{
books: () => db.Book.find(),
book: async (_,{id}) => await db.Book.findById(id),
bookChapters: async (_,args) => {
const book = await db.Book.findById(args.id)
const chapterList = await db.Chapter.find( {_id: book.chapters})
return chapterList
},
chapter: (_,{id}) => db.Chapter.findById(id),
},
Mutation:{
createBook: (_, {name, author, sinopsis}) => db.Book.create({name, author, sinopsis}),
createChapter: async (_, args) => {
const chapt = await db.Chapter.create(args)
const book = await db.Book.findById(ObjectId(chapt.bookId))
let chapterList = book.chapters
chapterList.push(chapt._id)
const result = await db.Book.updateOne({
"_id": chapt.bookId
},{
"$set": {
"chapters": chapterList
}
})
return prepare(await db.Chapter.findById(chapt.id))
},
},
}
架构.graphql:
type Book {
id: ID!
name: String!
author: String
sinopsis: String
chapters: [ID]
}
type Chapter {
id: ID!
name: String!
bookId: ID!
number: String!
content: String!
}
type Query{
books: [Book]
book(id: ID!): Book
bookChapters(id: ID!): [Chapter]
chapter(id: ID!): Chapter
}
type Mutation {
createBook(name: String!, author: String!,
sinopsis: String!, chapters: [ID!]): Book
createChapter(name: String!, bookId: String!, number:String!,
content:String!): Chapter
}
推荐阅读
- python - 为什么 zip 急切地评估其论点的要素?
- sql - 我想删除oracle中的大量数据
- sql-server - 如何在存储过程中使用更新多行
- node.js - 使用 Selenium、Node.js、Lambda(AWS) 自动测试需要 OTP 的网站
- python - 加载 pb 文件:DecodeError: Error parsing message
- mysql - 数据库中的自定义循环数设计
- assembly - NASM 宏调用内部过程
- python - 我查询我的 sqlite 数据库并得到结果为 [ ](空白),即使我的数据库有我试图查询的数据
- sql-server - 如何使用 Inno Setup 中的 Exec() 函数检查程序是否正确安装?
- android-studio - IntelliJ 2020.1 版中未安装 Flutter 插件