class ChaskiqRubyClient::Client

Attributes

client[RW]
schema[RW]
size[RW]
token[RW]

Public Class Methods

new(site, token) click to toggle source
# File lib/chaskiqRubyClient/client.rb, line 10
def initialize(site, token)
  
  self.token = token

  # Configure GraphQL endpoint using the basic HTTP network adapter.
  http = GraphQL::Client::HTTP.new(site) do
    def headers(context)
      # Optionally set any HTTP headers
      #{ "User-Agent": "My Client" }
      {"Authorization" => "Bearer #{context[:token]}"}
    end
  end  

  # Fetch latest schema on init, this will make a network request
  @schema = GraphQL::Client.load_schema( 
    GraphQL::Client.dump_schema(
      http, 
      nil, 
      context: { token: token }
    ) 
  )

  # 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

Public Instance Methods

headers(context) click to toggle source
# File lib/chaskiqRubyClient/client.rb, line 16
def headers(context)
  # Optionally set any HTTP headers
  #{ "User-Agent": "My Client" }
  {"Authorization" => "Bearer #{context[:token]}"}
end
query(type, variables={}) click to toggle source
# File lib/chaskiqRubyClient/client.rb, line 45
def query(type, variables={})
  self.client.query( type, {
    context: {
      token: self.token
    },
    variables: variables
  })
end