Created
November 5, 2019 14:40
-
-
Save harigopal/caba32c4f40f70c6ff8204def6281602 to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - Custom types for mutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/mutations/update_post.rb | |
module Mutations | |
class UpdatePost < GraphQL::Schema::Mutation | |
argument :post_id, ID, required: true | |
argument :description, String, required: true | |
description "Create a new post in a team" | |
field :post, Types::UpdatedPostType, null: true | |
field :errors, [String], null: false | |
def resolve(params) | |
mutator = UpdatePostMutator.new(context, params) | |
if mutator.valid? | |
{ post: mutator.update_post, errors: [] } | |
else | |
{ post: nil, errors: mutator.errors.full_messages } | |
end | |
end | |
end | |
end | |
# app/graphql/types/update_post_type.rb | |
module Types | |
class UpdatePostType < Types::BaseObject | |
field :comments, Integer, null: false | |
field :updated_at, Types::ISO8601DateTime, null: false | |
field :diff, String, null: false | |
def comments | |
object.comments.count | |
end | |
def diff | |
Posts::DiffService.new(object).last_diff | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment