gpt4 book ai didi

ruby-on-rails - graphql- ruby 。使用(而不是中继)突变 DRY。有或没有 GraphQL::Function?

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:04 25 4
gpt4 key购买 nike

我没有使用中继。

我已经阅读了一些教程。许多人使用这种方式进行突变:

app/graphql/graphql_tutorial_schema.rb

GraphqlTutorialSchema = GraphQL::Schema.define do
query(Types::QueryType)
mutation(Types::MutationType)
end

app/graphql/resolvers/create_link.rb

class Resolvers::CreateLink < GraphQL::Function
argument :description, !types.String
argument :url, !types.String

type Types::LinkType

def call(_obj, args, _ctx)
Link.create!(
description: args[:description],
url: args[:url],
)
end
end

最后他们有:

app/graphql/types/mutation_type.rb

Types::MutationType = GraphQL::ObjectType.define do
name 'Mutation'

field :createLink, function: Resolvers::CreateLink.new
end

所以他们正在使用 GraphQL::Function

这是要走的路吗?如果我不使用中继,这是唯一的路吗?

如果我想要一个用于所有link 操作(CRUD)的唯一文件怎么办?

其他教程(http://tech.eshaiju.in/blog/2017/05/15/graphql-mutation-query-implementation-ruby-on-rails/)使用这个:

app/graphql/mutations/comment_mutations.rb

module CommentMutations
Create = GraphQL::Relay::Mutation.define do
name "AddComment"

# Define input parameters
input_field :articleId, !types.ID
input_field :userId, !types.ID
input_field :comment, !types.String

# Define return parameters
return_field :article, ArticleType
return_field :errors, types.String

resolve ->(object, inputs, ctx) {
article = Article.find_by_id(inputs[:articleId])
return { errors: 'Article not found' } if article.nil?

comments = article.comments
new_comment = comments.build(user_id: inputs[:userId], comment: inputs[:comment])
if new_comment.save
{ article: article }
else
{ errors: new_comment.errors.to_a }
end
}
end
end

app/graphql/mutations/mutation_type.rb

MutationType = GraphQL::ObjectType.define do
name "Mutation"
# Add the mutation's derived field to the mutation type
field :addComment, field: CommentMutations::Create.field
end

所以我还可以添加:

MutationType = GraphQL::ObjectType.define do
name "Mutation"
field :addComment, field: CommentMutations::Create.field
field :updateComment, field: CommentMutations::Update.field
field :deleteComment, field: CommentMutations::Delete.field
end

但这仅适用于 Create = GraphQL::Relay::Mutation.define:我没有使用 Relay!

在您的文档中,我没有发现任何与此问题相关的内容。

我必须始终使用 GraphQL::Functions 吗?

或者我可以这样使用它:

MutationType = GraphQL::ObjectType.define do
name "Mutation"
field :addComment, field: CommentMutations::Create
field :updateComment, field: CommentMutations::Update
field :deleteComment, field: CommentMutations::Delete
end

并有这个(代码是一个例子):

module Mutations::commentMutations
Createcomment = GraphQL::ObjectType.define do
name "Createcomment"

input_field :author_id, !types.ID
input_field :post_id, !types.ID

return_field :comment, Types::commentType
return_field :errors, types.String

resolve ->(obj, inputs, ctx) {
comment = comment.new(
author_id: inputs[:author_id],
post_id: inputs[:post_id]
)

if comment.save
{ comment: comment }
else
{ errors: comment.errors.to_a }
end
}
end

Updatecomment = GraphQL::ObjectType.define do
name "Updatecomment"

input_field :author_id, !types.ID
input_field :post_id, !types.ID

return_field :comment, Types::commentType
return_field :errors, types.String

resolve ->(obj, inputs, ctx) {
comment = comment.new(
author_id: inputs[:author_id],
post_id: inputs[:post_id]
)

if comment.update
{ comment: comment }
else
{ errors: comment.errors.to_a }
end
}
end
end

这是另一种方式吗?

最佳答案

你应该试试 https://github.com/samesystem/graphql_rails gem 。它在 graphql 端具有 MVC 结构,因此您的 GraphQL 将与您的 RoR 代码几乎相同。

And what if I want a unique file for all link operations (CRUD)?

GraphqlRails 有 Controller 而不是解析器。你可以有这样的东西:

class CommentsController < GraphqlRails::Controller
action(:create).permit(:article_id, :body).returns(!Types::CommentType)
action(:update).permit(:id, :body).returns(!Types::CommentType)

def create
Comment.create!(params)
end

def update
Comment.find(params[:id]).update!(params)
end
end

关于ruby-on-rails - graphql- ruby 。使用(而不是中继)突变 DRY。有或没有 GraphQL::Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47968504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com