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

Posted

技术标签:

【中文标题】如何在中继服务器中使用删除突变?【英文标题】:How to use remove mutation in Relay server? 【发布时间】:2020-02-28 21:12:30 【问题描述】:

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

“值 \" id: '5db0026a76376e0f7c82d431' 转换为 ObjectId 失败 \" 在路径 \"_id\" 中,用于模型 \"Post\"。

请告诉我,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);
  
;

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;

【问题讨论】:

【参考方案1】:

工作突变是:

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

干杯,小猫

【讨论】:

【参考方案2】:

您必须将您的 id 转换为对象 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')

【讨论】:

以上是关于如何在中继服务器中使用删除突变?的主要内容,如果未能解决你的问题,请参考以下文章

React native 的中继突变返回 400 错误请求?

中继突变:突变分页关联

如何乐观响应中继现代突变中的连接属性?

普通数组上的中继突变不起作用

使用相对路径而不是 API 服务器中继变异

relayjs:使用中继进行身份验证,使用哪个突变?