Sha256: 0bb3e55a5714dbd9a8f59f571cf45aec974467d1adf79de407994a7effe3d850

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

require 'httparty'
require_relative 'response'

module StoryBranch
  module Graphql
    class Error < StandardError; end

    class Client
      def initialize(api_url:, api_key:)
        @api_url = api_url
        @auth = api_key
      end

      def get(graphql_query:, endpoint: 'graphql')
        body_json = query_params_json(graphql_query)

        gql_response = HTTParty.post(graphql_endpoint(endpoint), headers: headers, body: body_json)
        response = StoryBranch::Graphql::Response.new(response: gql_response)
        raise GraphqlClientError, response.full_error_messages unless response.success?

        response
      end

      private

      def graphql_endpoint(endpoint)
        "#{@api_url}#{endpoint}"
      end

      def headers
        {
          Authorization: @auth,
          'Content-Type': 'application/json'
        }
      end

      def query_params_json(graphql_query)
        { query: graphql_query }.to_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
story_branch-graphql-0.0.1 lib/story_branch/graphql/client.rb