0

I work with an express graphql server, prepared for react-relay. Queries and createPost mutation works correctly in graphiql interface. There is a problem with removePost mutation. Trying to use it, I get this responce:

"Cast to ObjectId failed for value \"{ id: '5db0026a76376e0f7c82d431' }\" at path \"_id\" for model \"Post\".

Tell me please, what's wrong with removePost mutation. Thanks!

Post.js:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/relay-project", {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

const Schema = mongoose.Schema;
const postSchema = new Schema({
  title: String,
  content: String
});

var PostModel = mongoose.model("Post", postSchema);

module.exports = {
  getPosts: () => {
    return PostModel.find().sort({_id: -1});
  },
  getPost: id => {
    return PostModel.findOne({ _id: id });
  },
  createPost: post => {
    return PostModel(post).save();
  },
  removePost: id => {
    return PostModel.findByIdAndRemove(id);
  }
};

Mutation.js:

const {
  GraphQLObjectType,
  GraphQLNonNull,
  GraphQLString,
  GraphQLID
} = require('graphql');

const {mutationWithClientMutationId} = require('graphql-relay');
const {Post} = require('./Post');

const PostModel = require('../model/Post');

const CreatePostMutation = mutationWithClientMutationId({
  name: "CreatePost",
  inputFields: {
    title: {type: new GraphQLNonNull(GraphQLString)},
    content: {type: new GraphQLNonNull(GraphQLString)}
  },
  outputFields: {
    post: {
      type: Post
    }
  },
  mutateAndGetPayload: args => {
    return new Promise((resolve,reject)=>{
      PostModel.createPost({
        title: args.title,
        content: args.content
      })
      .then(post=>resolve({post}))
      .catch(reject);
    });
  }
});

const RemovePostMutation = mutationWithClientMutationId({
  name: "RemovePost",
  inputFields: {
    id: {type: GraphQLID}
  },
  outputFields: {
    post: {
      type: Post
    }
  },
  mutateAndGetPayload: args => {
    return new Promise((resolve,reject)=>{
      PostModel.removePost({
        id: args.id
      })
      .then(post=>resolve({post}))
      .catch(reject);
    });
  }
});

const Mutation = new GraphQLObjectType({
  name: "Mutation",
  description: "kjhkjhkjhkjh",
  fields: {
    createPost: CreatePostMutation,
    removePost: RemovePostMutation
  }
});

module.exports = Mutation;
Kiten
  • 985
  • 2
  • 17
  • 35

2 Answers2

1

you have to convert your id to object id as mongodb save i guess use below code for id

const toBase64 = (str: string) => {
    return new Buffer(str.toString()).toString('base64')
}


const fromBase64 = (str: string) => {
    return Buffer.from(str, 'base64').toString('ascii')
}
Mayank Pandav
  • 425
  • 5
  • 20
0

The working mutation is:

const RemovePostMutation = mutationWithClientMutationId({
  name: "RemovePost",
  inputFields: {
    id: { type: new GraphQLNonNull(GraphQLString) },
  },
  outputFields: {
    deleted: { type: GraphQLBoolean },
    deletedId: { type: GraphQLString }
  },
  mutateAndGetPayload: async ({ id }, { viewer }) =>{
    const { id: productId } = fromGlobalId(id);
    const result = await PostModel.removePost(productId);
    return { deletedId: id, deleted: true };
  }    
});

Cheers, Kiten

Kiten
  • 985
  • 2
  • 17
  • 35