Created
October 18, 2017 21:24
-
-
Save graemeboyd/82d37179dd2c9f79801767362c4082ae to your computer and use it in GitHub Desktop.
Example ruby graphql controller
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
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