Sha256: 8bc000e620cebb52647019781fbfe17a412431660b8291e024e06a987a70a24b

Contents?: true

Size: 1.48 KB

Versions: 25

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require_relative "version"
require "graphlient"

module Stigg
  OperationsNames = Hash.new
  class StiggHTTPAdapter < Graphlient::Adapters::HTTP::FaradayAdapter
    def execute(document:, operation_name:, variables:, context:)
      headers = context[:headers] || {}
      headers.merge!({'x-graphql-operations-name': Stigg::OperationsNames[operation_name]})
      context[:headers] = headers

      super
    end
  end

  class Client
    def initialize(client)
      @client = client
    end

    def request(query, variables = nil)
      operation_name_regex = /^(?:mutation|query) (.*)\(/
      operation_name, = query.match(operation_name_regex).captures

      query = query.sub(operation_name, "")
      query_obj = @client.parse(query)

      Stigg::OperationsNames[query_obj.operation_name] = operation_name

      @client.query(query_obj, variables)
    end
  end

  class << self
    # @param [String] api_key
    # @param [String api_url
    # @return [Client]
    def create_client(api_key, api_url = "https://api.stigg.io/graphql", request_timeout=30)
      schema_path = File.join(__dir__, 'generated/schema.json').to_s

      client = Graphlient::Client.new(
        api_url,
        http: StiggHTTPAdapter,
        headers: {
          "X-API-KEY": api_key
        },
        http_options: {
          read_timeout: request_timeout,
          write_timeout: request_timeout
        },
        schema_path: schema_path
      )

      Client.new(client)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
stigg-api-client-1.122.1 lib/stigg/client.rb
stigg-api-client-1.122.0 lib/stigg/client.rb
stigg-api-client-1.121.1 lib/stigg/client.rb
stigg-api-client-1.119.0 lib/stigg/client.rb
stigg-api-client-1.117.2 lib/stigg/client.rb