Class: Waveapps::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/waveapps/api.rb

Constant Summary collapse

API_URL =
'https://gql.waveapps.com/graphql/public'
HTTP =
GraphQL::Client::HTTP.new(API_URL) do
  def headers(_context)
    # Optionally set any HTTP headers
    {
      'Authorization' => "Bearer #{Waveapps.access_token}"
    }
  end
end
Schema =

Fetch latest schema on init, this will make a network request

GraphQL::Client.load_schema(HTTP)
Client =

However, it’s smart to dump this to a JSON file and load from disk

Run it from a script or rake task rake schema:dump

Schema = GraphQL::Client.load_schema(“./tmp/schema.json”)

GraphQL::Client.new(schema: Schema, execute: HTTP)

Class Method Summary collapse

Class Method Details

.handle_errors(response, mutation) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/waveapps/api.rb', line 30

def self.handle_errors(response, mutation)
  # Parse/validation errors will have `response.data = nil`. The top level
  # errors object will report these.
  if response.errors.any?
    # "Could not resolve to a node with the global id of 'abc'"
    message = response.errors[:data].join(", ")
    return ::Waveapps::Error.new(message)

  # The server will likely give us a message about why the node()
  # lookup failed.
  elsif data = response.data
    # "Could not resolve to a node with the global id of 'abc'"
    if data.errors[mutation].any?
      message = data.errors[mutation].join(", ")
      return ::Waveapps::Error.new(message)
    end
  else
    Waveapps::Error.new("Something went wrong!")
  end
end