Skip to content

Instantly share code, notes, and snippets.

@nickpoorman
Created November 26, 2016 14:24
Show Gist options
  • Save nickpoorman/41d431a9be23838a1d9bba5f5f83dfdb to your computer and use it in GitHub Desktop.
Save nickpoorman/41d431a9be23838a1d9bba5f5f83dfdb to your computer and use it in GitHub Desktop.
# app/controllers/graphql_controller.rb
class GraphqlController < ApplicationController
def create
query_string = params[:query]
query_variables = ensure_hash(params[:variables])
context = { current_user: current_user, pundit: self }
result = ApplicationSchema.execute(query_string, variables: query_variables, context: context)
render json: result
end
private
def ensure_hash(query_variables)
if query_variables.blank? || query_variables == 'null'
{}
elsif query_variables.is_a?(String)
JSON.parse(query_variables)
else
query_variables
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment