简体   繁体   中英

How to represent an array of objectID of mongoose in Graphql Schema

I'm trying to convert a rest backend with node and mongoose to use GraphQL but i dont know how to represent something like an array of objectID of mongoose schema in graphql schema. My doubt, how do i make this think be a list of Chapter ObjectIds? Didn't find anything alike

mongoose schema

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 Schema:

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
}

This is the error that i got.

{
  "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})
        },
    },

}

Found the answer that fits my problem, this is the final archives

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM