0

我使用一个为 react-relay 准备的 express graphql 服务器。查询和 createPost 突变在 graphiql 界面中正常工作。removePost突变存在问题。尝试使用它,我得到了这个回应:

对于模型 \"Post\" 的路径 \"_id\" 中的值 \"{ id: '5db0026a76376e0f7c82d431' }\" 转换为 ObjectId 失败。

请告诉我, removePost突变有什么问题。谢谢!

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);
  }
};

突变.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;
4

2 回答 2

1

你必须将你的 id 转换为对象 id 作为 mongodb save 我猜使用下面的代码作为 id

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


const fromBase64 = (str: string) => {
    return Buffer.from(str, 'base64').toString('ascii')
}
于 2020-01-27T08:10:49.103 回答
0

工作突变是:

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 };
  }    
});

干杯,小猫

于 2019-11-14T05:11:06.930 回答