简体   繁体   English

如何在中继服务器中使用删除突变?

[英]How to use remove mutation in Relay server?

I work with an express graphql server, prepared for react-relay.我使用一个 Express graphql 服务器,为反应中继做准备。 Queries and createPost mutation works correctly in graphiql interface.查询和 createPost 突变在 graphiql 界面中正常工作。 There is a problem with removePost mutation. removePost突变存在问题。 Trying to use it, I get this responce:尝试使用它,我得到了这个回应:

"Cast to ObjectId failed for value \"{ id: '5db0026a76376e0f7c82d431' }\" at path \"_id\" for model \"Post\".对于 model \"Post\",路径 \"_id\" 处的值 \"{ id: '5db0026a76376e0f7c82d431' }\" 转换为 ObjectId 失败。

Tell me please, what's wrong with removePost mutation.请告诉我, removePost突变有什么问题。 Thanks!谢谢!

Post.js: 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:突变.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;

you have to convert your id to object id as mongodb save i guess use below code for id您必须将您的 id 转换为 object id 为 mongodb 保存我猜使用下面的代码作为 id

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


const fromBase64 = (str: string) => {
    return Buffer.from(str, 'base64').toString('ascii')
}

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干杯,小猫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM