Jim*_*ker 10 graphql graphql-ruby
Graphql中删除突变的结果应该是什么?我正在使用graphql-ruby gem.这是我的变异的一个例子,但我只是不确定我应该作为回应返回什么.
Mutations::Brands::Delete = GraphQL::Relay::Mutation.define do
name "DeleteBrand"
description "Delete a brand"
input_field :id, types.ID
# return_field ??
resolve ->(object, inputs, ctx) {
brand = Brand.find(inputs[:id])
brand.destroy
}
end
Run Code Online (Sandbox Code Playgroud)
小智 18
我不认为2017年7月存在明确的事实标准,我看到实现之间存在很多差异(GitHub,Yelp,GraphCool,Shopify).
但是,如果你看一下最近出现的一些GraphQL API,似乎有一个共同的趋势.很大程度上,输入类型和响应类型是突变特有的.例如,对于updateBrand
您可能期望的突变UpdateBrandInput
,并返回UpdateBrandPayload
响应.请注意,输入不是BrandInput
,并以响应Brand
.您也不会使用标量布尔值(例如,true
如果成功)或id
已删除实体(在删除突变的情况下)进行响应.按照这个惯例,你可以有一个createBrand
变异,一个CreateBrandInput
和一个CreateBrandPayload
响应.
通过创建特定于突变input
和payload
类型,您可以在期望和响应的字段中具有很大的灵活性.每次删除,您可能会有一个DeleteBrandPayload
响应,不仅包括品牌的浅(例如,只有标量)字段,还包括其他相关数据(例如clientMutationId
)等.
说实话,我认为GraphQL规范提供了足够的绳索让自己挂起来,所以看看一些大家伙如何推动这一点是明智的.
您可以返回deleted_id 或消息。如果它是关联对象,您可以返回更新的对象,如下例所示。
Destroy = GraphQL::Relay::Mutation.define do
name 'DestroyComment'
description 'Delete a comment and return post and deleted comment ID'
# Define input parameters
input_field :id, !types.ID
# Define return parameters
return_field :deletedId, !types.ID
return_field :article, ArticleType
return_field :errors, types.String
resolve ->(_obj, inputs, ctx) {
comment = Comment.find_by_id(inputs[:id])
return { errors: 'Comment not found' } if comment.nil?
article = comment.article
comment.destroy
{ article: article.reload, deletedId: inputs[:id] }
}
Run Code Online (Sandbox Code Playgroud)
http://tech.eshaiju.in/blog/2017/05/15/graphql-mutation-query-implementation-ruby-on-rails/
归档时间: |
|
查看次数: |
6754 次 |
最近记录: |