Class: ChaskiqRubyClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chaskiqRubyClient/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, token) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/chaskiqRubyClient/client.rb', line 8

def client
  @client
end

#schemaObject

Returns the value of attribute schema.



8
9
10
# File 'lib/chaskiqRubyClient/client.rb', line 8

def schema
  @schema
end

#sizeObject

Returns the value of attribute size.



8
9
10
# File 'lib/chaskiqRubyClient/client.rb', line 8

def size
  @size
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/chaskiqRubyClient/client.rb', line 8

def token
  @token
end

Instance Method Details

#query(type, variables = {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/chaskiqRubyClient/client.rb', line 45

def query(type, variables={})
  self.client.query( type, {
    context: {
      token: self.token
    },
    variables: variables
  })
end