我正在尝试将带有节点和猫鼬的其余后端转换为使用GraphQL,但我不知道如何在graphql模式中表示类似猫鼬模式的objectID数组的内容。我的疑问是,我如何使它成为Chapter ObjectId的列表?没有找到相似的东西
猫鼬模式
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})
},
},
}
答案 0 :(得分:0)
找到了适合我问题的答案,这是最终的档案文件
BookResolver:
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))
},
},
}
schema.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
}