Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphql-ruby. Use (not Relay) mutations DRY. With or without GraphQL::Function?

I'M NOT USING RELAY.

I have read some tutorials. Many use this way for mutations:

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

and finally they have:

app/graphql/types/mutation_type.rb

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

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

So they are using a GraphQL::Function.

Is this the way to go? If I'm not using Relay this is just the only way to go?

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

Other tutorials (http://tech.eshaiju.in/blog/2017/05/15/graphql-mutation-query-implementation-ruby-on-rails/) use this:

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

and 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

so I can add also:

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

But this works good just with Create = GraphQL::Relay::Mutation.define: I'm not using Relay!

In your docs I find nothing related to this problem.

I have to always use GraphQL::Functions?

Or maybe I can use it this way:

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

and have this (code is an example):

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

Is this another way?


1 Answers

You should try https://github.com/samesystem/graphql_rails gem. It has MVC structure on graphql side, so your GraphQL will be almost the same as your RoR code.

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

GraphqlRails has controllers instead of resolvers. You could have something like this:

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
like image 170
Povilas Jurčys Avatar answered Mar 16 '26 09:03

Povilas Jurčys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!