Skip to content

Instantly share code, notes, and snippets.

@nickpoorman
Created November 26, 2016 14:28
Show Gist options
  • Save nickpoorman/968aa61452b22e75a057e3d46414191b to your computer and use it in GitHub Desktop.
Save nickpoorman/968aa61452b22e75a057e3d46414191b to your computer and use it in GitHub Desktop.
# app/graph/mutations/post_mutations/create.rb
module PostMutations
Create = GraphQL::Relay::Mutation.define do
# Used to name derived types, eg `"CreatePostInput"`:
name 'CreatePost'
description 'Create post with a title and return a post'
# Define input parameters
# Accessible from `input` in the resolve function:
input_field :title, !types.String
# Define return parameters
# The result has access to these fields,
# resolve must return a hash with these keys
return_field :post, PostType
# Resolve block to create post and return hash of post
# The resolve proc is where you alter the system state.
resolve -> (_obj, inputs, ctx) {
# Pundit authorization
ctx[:pundit].authorize :post, :create?
post = ctx[:current_user].posts.create!(title: inputs[:title])
# The keys in this hash correspond to `return_field`s above:
{
post: post
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment