Sha256: 63bf8850ed85e3d6f0a6ff85cbfc52ba0e36a3e3a253060f5f6fce93302d985e

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Waveapps
  class << self
    attr_accessor :access_token
  end
  class Api
    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

    # 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
    # rake schema:dump
    #
    # Schema = GraphQL::Client.load_schema("./tmp/schema.json")

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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
waveapps-0.1.7 lib/waveapps/api.rb
waveapps-0.1.6 lib/waveapps/api.rb
waveapps-0.1.5 lib/waveapps/api.rb