Skip to content

Instantly share code, notes, and snippets.

@graemeboyd
Created October 18, 2017 21:24
Show Gist options
  • Save graemeboyd/82d37179dd2c9f79801767362c4082ae to your computer and use it in GitHub Desktop.
Save graemeboyd/82d37179dd2c9f79801767362c4082ae to your computer and use it in GitHub Desktop.
Example ruby graphql controller
FloatAppSchema = GraphQL::Schema.define(
query: QueryType, // instance of GraphQL::ObjectType
mutation: MutationType // instance of GraphQL::ObjectType
)
class QueriesController < ApplicationController
include Skylight::Helpers
def create
query_string = params[:query]
query_variables = params[:variables] || {}
result = nil
Skylight.instrument title: "FloatAppSchema.execute" do
result = FloatAppSchema.execute(query_string, variables: query_variables, context: {current_user: current_user})
end
# Use OJ JSON encoder directly.
# mode: :strict ensures that only basic JSON types are encoded, anything
# ruby object related (which won't make sense to JavaScript) will raise an
# exception.
# escape_mode: :xss_safe provides an extra level of protection against XSS
# attacks.
render json: Oj.dump(result, mode: :strict, escape_mode: :xss_safe)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment