如何在服务器端的 Relay Mutations 中实现对另一个模型的引用?
How to implement reference to another model in Relay Mutations on the server side?
我被 Relay Mutation 和 globalIdField 中的 Refs 困住了。
所以假设评论可以有父评论 ID,道具中必须有 post ID,并且我有一个使用以下模式定义的 Mutation:
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
}
如果我可以在post中添加globalIdField()会很方便,但是Relay不能传递这个,因为inputFields中的字段不能有globalIdField有的解析函数。
到目前为止我找不到比下面更好的解决方案:
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);
},
如果有人能提供更好的体验,我会很高兴。
我被 Relay Mutation 和 globalIdField 中的 Refs 困住了。
所以假设评论可以有父评论 ID,道具中必须有 post ID,并且我有一个使用以下模式定义的 Mutation:
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
}
如果我可以在post中添加globalIdField()会很方便,但是Relay不能传递这个,因为inputFields中的字段不能有globalIdField有的解析函数。
到目前为止我找不到比下面更好的解决方案:
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);
},
如果有人能提供更好的体验,我会很高兴。