Home > Software design >  return null when checking on apollographql
return null when checking on apollographql

Time:06-21

I don't know why i can't get any data return from graphql I need help soon This is my code I have 3 files used in nodejs express which are schema, resolver and data. I have 3 files used in nodejs express namely schema, resolver and data. The first is the schema file with the types

const {gql} = require('apollo-server');
const typeDefs = gql`
    type Post {
        id: ID
        name: String
        content: String
        genre: String
        author: Author
    }

    type Author {
        id: ID
        name: String
        age: Int
        posts: [Post]
    }

# The query type, represents the current state of the system
    type Query {
        posts: [Post]
        authors: [Author]
        post(id: ID!): Post
        author(id: ID!): Author
    }
    type Mutation {
        createAuthor(name: String, age: Int): Author
        createPost(name: String, content: String, genre: String, authorId: ID!): Post
    }
`
module.exports = typeDefs;

File resolver returns the required data.

const mongoDataMethods = require('../data/data')
const resolvers = {
    Query: {
        posts : async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllPosts(),
        authors: async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllAuthors(),
        post: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getPostById(id),
        author: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(id),
    },

    Post :{
        author: async ({authorId},args,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(authorId)
    },

    Author:{
        posts: async ({id} , args , {mongoDataMethods}) => await mongoDataMethods.getAllPosts({authorId : id})
    },


// Mutations
    Mutation: {
        createPost: async (parent, args, {mongoDataMethods}) =>{
            await mongoDataMethods.createPost(args)
        },
        createAuthor : async (parent, args, {mongoDataMethods}) =>{
            await mongoDataMethods.createAuthor(args)
        }
    }
}

module.exports = resolvers;

Next is the part to get data from mongoDB to return to the resolver file

const Post = require('../model/Post')
const Author = require('../model/Author')

const mongoDataMethods = {
    getAllPosts: async (condition = null) => condition === null ?await Post.find() : await Post.find(condition),
    getAllAuthors: async () => await Author.find(),
    getAuthorById: async id => await Author.findById(id),
    getPostById: async id => await Post.findById(id),

    //Mutation
    createPost: async (args) => {
        const newPost = new Post(args)
        return await newPost.save()
    },
    createAuthor: async (args) =>{
        const newAuthor = new Author(args)
        return await newAuthor.save()
    }
}

module.exports = mongoDataMethods

the problem occurs in the next step. when i execute the server run and test the commands, when i run the mutation i don't get the return data i want. I get back a json with data as null and I want to get back the values I want but not null

mutation Mutation( $name: String, $content: String, $genre: String ,$authorId: ID!) {
  createPost(authorId: $authorId, name: $name, content: $content, genre: $genre) {
    id
    name
    content
  }
}

with variables

{
  
  "content": "Thanks you for that custom hook. I actually managed to figure it out but I didn't use a custom hook instead. How does your custom hook differ from passing in update and using mutationResult?",
  "name": "How do you get the return data from a mutation?",
  "genre": "action",
  "authorId": "62ae509be22091e690dea46a"
}

i don't get any data and it return

{
  "data": {
    "createPost": null
  }
}

need help and thanks!!

CodePudding user response:

I already know where I went wrong. I haven't returned it yet LOL Just remove the {} from the mutation section of the resolver file

  • Related