Created
October 2, 2016 10:28
-
-
Save gauravtiwari/c6ba8ada02e10f1f845fa1d44615a85c to your computer and use it in GitHub Desktop.
A minimal sinatra app to execute queries against GraphQL server.
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
class SinatraGraphqlErb < Sinatra::Base | |
set public_folder: 'public', static: true | |
use Rack::Session::Cookie, secret: 'super_secret_client_key' | |
use Rack::Protection | |
use Rack::Protection::RemoteReferrer | |
use Sass::Plugin::Rack | |
private | |
def query(definition, variables = {}) | |
response = API::Client.query( | |
definition, | |
variables: variables, | |
context: client_context | |
) | |
case response | |
when GraphQL::Client::SuccessfulResponse | |
response.data | |
when GraphQL::Client::FailedResponse | |
raise response.errors | |
end | |
end | |
def client_context | |
{ access_token: ENV['ACCESS_TOKEN'] } | |
end | |
get '/' do | |
data = query IndexQuery | |
erb :index, locals: { | |
posts: data.root.posts | |
} | |
end | |
get '/posts/:id' do | |
data = query ShowQuery, id: params["id"] | |
erb :show, locals: { | |
post: data.node, | |
comments: data.node.comments | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment