Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My problem sorry! #15

Open
Javadebi opened this issue Apr 20, 2020 · 2 comments
Open

My problem sorry! #15

Javadebi opened this issue Apr 20, 2020 · 2 comments

Comments

@Javadebi
Copy link

Hi Ben.
I hope you are ok in these days.
i have a tiny problem with my school project. I want to make a user , post, comment with Graphql ,Express and Mongoose(Mongodb). I have wrote some code but i don't know why its not working. Can you please do me a favor and help me?

I can create user but when i want to create a post related to the user i get this error in playground:

ID cannot represent value: <Buffer 5e 9b f1 3e e9 49 61 38 fc 1a 6f 59>

these are my codes:

TypeDefs:

import { gql } from 'apollo-server-express';

export const typeDefs = gql`

    type Query {
        users: [User]
        posts: [Post]
    }

    type Mutation {
        createUser(name: String,email: String, age: Int): User!
        createPost(title: String, body: String, published: Boolean, author: ID): Post!
    }

    type User {
        id: ID!
        name: String!
        email: String!
        age: Int
        posts: [Post!]!
        comments: [Comment!]!
    }

    type Post {
        id: ID!
        title: String!
        body: String!
        published: Boolean!
        author: User!
        comments: [Comment!]!
    }

    type Comment {
        id: ID!
        text: String!
        author: User!
        post: Post!
    }
`

Resolvers:

import Users from './models/User';
import Posts from './models/Post';
import Comments from './models/Comment';


export const resolvers = {
    Query: {
        users: () => {
            return Users.find();
        },
        posts: () => {
            return Posts.find();
        }
    },
    Mutation: {
        createUser: async (parent, args, context, info) => {
            const user = new Users(args);
            await user.save();

            return user;
        },
        createPost: async (parent, { title, body, published, author }, context, info) => {
            const user = await Users.findById(author);

            if (!user) {
                console.log("User not found")
            }
            console.log(user)

            const post = new Posts({ title, body, published, author: user.id });
            await post.save();

            user.posts.push(post);
            await user.save();

            return post;
        }
    }
}

UserSchema:

import mongoose, { mongo } from 'mongoose';

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    age: {
        type: Number,
        required: false
    },
    posts: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post'
        }
    ],
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Comment'
        }
    ]
});

module.exports = mongoose.model('User',userSchema);

PostSchema:

import mongoose from 'mongoose';

const postSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    },
    published: {
        type: Boolean,
        required: true
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Comment'
        }
    ]
});

module.exports = mongoose.model('Post',postSchema);

CommentSchema:

import mongoose from 'mongoose';

const commentSchema = new mongoose.Schema({
    text: {
        type: String,
        required: true
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    post: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Post'
    }
});

module.exports = mongoose.model('Comment',commentSchema);
@Javadebi
Copy link
Author

Sorry Ben i didn't know any other way to get your help

@benawad
Copy link
Owner

benawad commented Apr 21, 2020

I'm guessing you need to convert the id to a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants