Skip to content

Instantly share code, notes, and snippets.

@TayKangSheng
Last active May 14, 2019 09:24
Show Gist options
  • Save TayKangSheng/53c4527a5fc22a95657803d39c119aec to your computer and use it in GitHub Desktop.
Save TayKangSheng/53c4527a5fc22a95657803d39c119aec to your computer and use it in GitHub Desktop.
graphql-client
require "graphql/client"
require "graphql/client/http"
require "pry"
module GitHub
HTTP = GraphQL::Client::HTTP.new("https://api.github.com/graphql") do
def headers(context)
{ "Authorization": "Bearer #{ENV['personal_access_token']}" }
end
end
# Fetch latest schema on init, this will make a network request
Schema = GraphQL::Client.load_schema(HTTP)
# However, it's smart to dump this to a JSON file and load from disk
#
# Run it from a script or rake task
# GraphQL::Client.dump_schema(SWAPI::HTTP, "path/to/schema.json")
#
# Schema = GraphQL::Client.load_schema("path/to/schema.json")
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
end
BasicQuery = GitHub::Client.parse <<-'GRAPHQL'
query ($owner: String!, $name: String!){
viewer {
login
}
}
GRAPHQL
result = GitHub::Client.query(BasicQuery, variables: { owner: "taykangsheng", name: "taykangsheng.github.io" })
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment