2017-06-06 15 views
0

我在Relay Mutation和globalIdField中使用Refs。如何在服务器端实现对Relay Mutations中另一个模型的引用?

所以,让我们说,注释可以有父评论ID,必须在道具帖子ID,我有以下模式定义的突变:

const createComment = mutationWithClientMutationId({ 
    name: 'CreateComment', 
    description: 'Create comment of the post', 
    inputFields: { 
    content: { 
     type: new GraphQLNonNull(GraphQLString), 
     description: 'Content of the comment', 
    }, 
    parent: { 
     type: GraphQLID, 
     description: 'Parent of the comment', 
    }, 
    post: { 
     type: new GraphQLNonNull(GraphQLID), 
     description: 'Post of the comment', 
    }, 
    }, 
    outputFields: { 
    comment: { type: commentType, resolve: comment => comment }, 
    }, 
    mutateAndGetPayload: (input, context) => (context.user 
    ? Comment.create(Object.assign(input, { author: context.user._id })) 
    : new Error('Only logged in user can create new comment')), 
}); 

我的评论有globalIdField,postType了。当我将从客户端查询突变时,我将使用无处不在的globalIds而不是此对象的实际mongo _id。在这里它更好的办法,而不是这片mutateAndGetPayload:

mutateAndGetPayload: (input, context) => { 
    if (input.parent) input.parent = fromGlobalId(input.parent).id; 
    if (input.post) input.post = fromGlobalId(input.post).id; 
    // And other logic 
} 

它可以十分便利,如果我可以只在后期添加globalIdField(),但继电器不能通过这一点,因为在inputFields场均能” t有一个globalIdField具有的解析器功能。

回答

0

到目前为止,我无法找到解决办法比低于更好:

mutateAndGetPayload: ({ content, post, parent }, context) => { 
    if (!context.user) throw new Error('Only logged in user can create new comment'); 
    const newComment = { author: context.user._id, content }; 
    if (post) newComment.post = fromGlobalId(post).id; 
    if (parent) newComment.parent = fromGlobalId(parent).id; 
    return Comment.create(newComment); 
    }, 

会很高兴,如果有人将提供这个更好的体验。

相关问题