2017-12-25 13 views
0

私はリレーを使用していません。graphql-ruby。変異(DRYではない)を使用する。 GraphQL :: Functionの有無にかかわらず?

私はいくつかのチュートリアルを読んでいます。多くの変異のために使用この方法:

アプリ/ graphql/graphql_tutorial_schema.rb

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

アプリ/ graphql /レゾルバ/ 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)に固有のファイルが必要な場合はどうすればよいですか?

アプリ/ graphql /変異/ 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 

アプリ/ graphql /変異/ 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 

その他のチュートリアル(http://tech.eshaiju.in/blog/2017/05/15/graphql-mutation-query-implementation-ruby-on-rails/)は、この使用します

だから、私も追加できます:

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でちょうど良い作品:私はリレーを使用していませんよ!

あなたのドキュメントで私はこの問題に関連するものは何も見つかりませんでした。

私は常に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 

をこれは別の方法ですか?

blah_schema:鉱山は現在、どのように見えるか相続人

答えて

0

。RB

BlahSchema = GraphQL::Schema.define do 
    ... 
    query(Types::QueryType) 

mutation_type.rb

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


    field :comment, !Types::CommentType do 
    argument :resource_type, !types.String 
    argument :resource_id, !types.ID 
    argument :comment, !types.String 

    resolve ResolverErrorHandler.new ->(obj, args, ctx) do 
     ctx[:current_user].comments. 
     create!(resource_id: args[:resource_id], 
      resource_type: args[:resource_type], 
      comment: args[:comment]) 
    end 
    end 

    field :destroy_comment, !Types::CommentType do 
    argument :id, !types.ID 
    resolve ResolverErrorHandler.new ->(obj, args, ctx) do 
     comment = ctx[:current_user].comments.where(id: args[:id]).first 
     if !comment 
     raise ActiveRecord::RecordNotFound.new(
      "couldn't find comment for id #{args[:id]} belonging to #{current_user.id}") 
     end 

     comment.destroy! 
     comment 
    end 
    end 
end 

resolver_error_handler.rb

class ResolverErrorHandler 

    def initialize(resolver) 
    @r = resolver 
    end 

    def call(obj, args, ctx) 
    @r.call(obj, args, ctx) 
    rescue ActiveRecord::RecordNotFound => e 
    GraphQL::ExecutionError.new("Missing Record: #{e.message}") 
    rescue AuthorizationError => e 
    GraphQL::ExecutionError.new("sign in required") 
    rescue ActiveRecord::RecordInvalid => e 
    # return a GraphQL error with validation details 
    messages = e.record.errors.full_messages.join("\n") 
    GraphQL::ExecutionError.new("Validation failed: #{messages}") 
    rescue StandardError => e 
    # handle all other errors 
    Rails.logger.error "graphql exception caught: #{e} \n#{e.backtrace.join("\n")}" 
    Raven.capture_exception(e) 

    GraphQL::ExecutionError.new("Unexpected error!") 
    end 
end 

はそうです、それは異なっている - 私はそれが良いでしょうわからないんだけど、それは私が思いついたものだけです。私のmutation_type.rbは、私が好きではないより豊かです。

あなたは具体的な回答を得るのに役立つように、目標や問題を明確に記入していませんでした。

0

最近私が使っている別の方法があります。また、Reactを使用していないため、GraphQL::Relay::Mutation.defineを使用して変異を説明することは奇妙に思えました。

代わりにfieldsについて説明します。 (例:app/graphql/mutations/create_owner.rb)で次に

Mutations::CreateOwner = GraphQL::Field.define do 
    name 'CreateOwner' 
    type Types::OwnerType 
    description 'Update owner attributes' 

    argument :name, !types.String 
    argument :description, types.String 

    resolve ->(_obj, args, _ctx) do 
    Owner.create!(args.to_h) 
    end 
end 

あなたapp/graphql/types/mutation_type.rb追加:

field :createOwner, Mutations::CreateOwner 

これは、独自のリゾルバクラスにリゾルバを抽出することにより、さらにリファクタリングすることができます。

私が見つけた定義済みのベストプラクティスがないと、これはこの問題を処理するきわめてきれいな方法でした。

関連する問題