3

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?

3 Answers3

1

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
0

Heres what mine currently look like:

blah_schema.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

So yes it is different - I'm not sure it's better, it's just what i came up with. My mutation_type.rb is a lot fatter which i don't like.

You didn't clearly spell out any goals or problems so that might help you get a more specific answer.

hattenn
  • 4,371
  • 9
  • 41
  • 80
Michael Economy
  • 608
  • 6
  • 21
0

There is another method I've been utilizing recently. We also do not use React, and it seemed odd to be using GraphQL::Relay::Mutation.define for describing mutations.

Instead we describe the fields. (for example: 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

Then in your app/graphql/types/mutation_type.rb you add:

field :createOwner, Mutations::CreateOwner

This can be refactored further by extracting resolvers into their own resolver classes.

Without some defined best practices that I've been able to locate, this has been a pretty clean way of dealing with this issue.

Pelted
  • 81
  • 4