Link data between two types GraphQl (apollo,express) How do I match the data by the title?

Issue

I’m trying to connect two types Manga and Chapter. Manga having a array called Chapters.

 type Manga {
   _id: ID!
   title: String!
   alternative: String
   description: String
   chapter_number: Int
   author: String
   artist: String
   genre: [String]
   type: String
   release_date: String
   status: String
   chapters: [Chapter]
 }

 type Chapter {
   _id: ID!
   manga: Manga
   title: String!
   chapter: Int
   chapters: [String]
 }

But when I try to do it in a resolver the Chapter has no data for me to link back to the Manga

const Manga = require("./models/Manga.model");
const Chapter = require("./models/Chapter.model");

const resolvers = {
  Manga: {
    chapters: async (parent) => {

        return Chapter.find((chapter) => Chapter.title === parent.title);
    }
  },

I’ve tried console logging Chapters.title but it returns nothing. Am I doing something wrong or is there another way of doing it? I’ve tried searching online but after countless hours I’m stuck

Here’s the rest of the code

const mongoose = require("mongoose");

const ChapterSchema = new mongoose.Schema({
   
   title: {
       type: String,
       required: true
   },
   chapter: {
       type: Number,
   },
   chapters: {
       type: [String],
   },

})

const Chapter = mongoose.model('Chapter', ChapterSchema);
module.exports = Chapter;
const mongoose = require("mongoose");

const MangaSchema = new mongoose.Schema({

   title: {
       type: String,
       required: true
   },
   alternative: {
       type: String,
   },
   description: {
       type: String,
   },
   chapter_number: {
       type: Number,
   },
   author: {
       type: String,
   },
   artist: {
       type: String,
   },
   genre: {
       type: [String],
   },
   type: {
       type: String,
   },
   release_date: {
       type: String,
   },
   status: {
       type: String,
   },
   description: {
       type: String,
   },
   Chapters: {
       type: [String],
   },
})

const Manga = mongoose.model('Manga', MangaSchema);
module.exports = Manga;
const Manga = require("./models/Manga.model");
const Chapter = require("./models/Chapter.model");

const resolvers = {
 Manga: {
   chapters: async (parent) => {

       return Chapter.find((chapter) => Chapter.title === parent.title);
   }
 },
 


 Query: {
   mangas: async () => {
     return await Manga.find();
   },
   manga: async (parent, { id }, context, info) => {
     return await Manga.findById(id);
   },
   chapters: async () => {
     return await Chapter.find();
   },
   chapter: async (parent, { id }, context, info) => {
     return await Chapter.findById(id);
   },
 },

 Mutation: {
   createManga: async (parent, args, context, info) => {
     const {
       title,
       alternative,
       description,
       chapter_number,
       author,
       artist,
       genre,
       type,
       release_date,
       status,
     } = args.manga;
     const manga = new Manga({
       title,
       alternative,
       description,
       chapter_number,
       author,
       artist,
       genre,
       type,
       release_date,
       status,
     });
     await manga.save();
     return manga;
   },
   deleteManga: async (parent, { id }, context, info) => {
     await Manga.findByIdAndDelete(id);
     return "Manga deleted";
   },
   updateManga: async (parent, args, context, info) => {
     const { id } = args;
     const {
       title,
       alternative,
       description,
       chapter_number,
       author,
       artist,
       genre,
       type,
       release_date,
       status,
     } = args.manga;
     const updates = {};
     if (title !== undefined) {
       updates.title = title;
     }
     if (alternative !== undefined) {
       updates.alternative = alternative;
     }
     if (chapter_number !== undefined) {
       updates.chapter_number = chapter_number;
     }
     if (author !== undefined) {
       updates.author = author;
     }
     if (artist !== undefined) {
       updates.artist = artist;
     }
     if (genre !== undefined) {
       updates.genre = genre;
     }
     if (description !== undefined) {
       updates.description = title;
     }
     if (type !== undefined) {
       updates.type = type;
     }
     if (release_date !== undefined) {
       updates.release_date = release_date;
     }
     if (status !== undefined) {
       updates.status = status;
     }
     const manga = await Manga.findByIdAndUpdate(id, updates, { new: true });
     return manga;
   },

   createChapter: async (parent, args, context, info) => {
     const { title, chapter, chapters } = args.chapter;
     const newchapter = new Chapter({ title, chapter, chapters });
     await newchapter.save();
     return newchapter;
   },
   deleteChapter: async (parent, { id }, context, info) => {
     await Chapter.findByIdAndDelete(id);
     return "Chapter deleted";
   },
   updateChapter: async (parent, args, context, info) => {
     const { id } = args;
     const { title, chapter, chapters } = args.chapter;
     const updates = {};
     if (title !== undefined) {
       updates.title = title;
     }
     if (chapter !== undefined) {
       updates.description = description;
     }
     if (chapters !== undefined) {
       updates.chapters = chapters;
     }
     const updatechapter = await Chapter.findByIdAndUpdate(id, updates, {
       new: true,
     });
     return updatechapter;
   },
 },
};

module.exports = resolvers;
const { gql } = require("apollo-server-express");

const typeDefs = gql`

 type Manga {
   _id: ID!
   title: String!
   alternative: String
   description: String
   chapter_number: Int
   author: String
   artist: String
   genre: [String]
   type: String
   release_date: String
   status: String
   chapters: [Chapter]
 }

 type Chapter {
   _id: ID!
   manga: Manga
   title: String!
   chapter: Int
   chapters: [String]
 }

 type GetChapters {
   getAllChapters (title:String): [Chapter]
 }


 type Query {
   manga(_id: ID!): Manga
   mangas: [Manga]
   chapter(_id: ID!): Chapter
   chapters: [Chapter]
 }

 input mangaInput {
   title: String
   alternative: String
   description: String
   chapter_number: Int
   author: String
   artist: String
   genre: [String]
   type: String
   release_date: String
   status: String
 }
 input chapterInput {
   title: String
   chapter: Int
   chapters: [String]
 }

 type Mutation {
   createManga(manga: mangaInput): Manga
   deleteManga(id: ID): String
   updateManga(id: ID, manga: mangaInput): Manga

   createChapter(chapter: chapterInput): Chapter
   deleteChapter(id: ID): String
   updateChapter(id: ID, chapter: chapterInput): Chapter
 }
`;

module.exports = typeDefs;
const express = require('express')
const { ApolloServer} = require('apollo-server-express')
const typeDefs = require('./typeDefs')
const resolvers = require('./resolvers')
const mongoose = require('mongoose')



async function startServer () {
    const app = express()
    const apolloServer = new ApolloServer({
        typeDefs,
        resolvers,
    })

    await apolloServer.start()
    apolloServer.applyMiddleware({app: app})

    app.use((req,res) => {
        res.send("hello from express apollo server")
    })
    
    await mongoose.connect("help pls", {
        useUnifiedTopology: true,
        useNewUrlParser: true,
    });
    
    console.log("Mongoose connected...")

    app.listen(4000, () => console.log('running on port 4000'))
};
startServer();

Solution

Found out what was happening, most tutorials that I was watching were using a static db in their folder, however I was using mongodb(mongoose) which meant that i could not use the same way to access the data I needed but I found a website and this is the solution.

Manga: {
  async chapters(parent) {
    return await Chapter.find({title: parent.title})
  }
},

Answered By – kahuds

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published