Sha256: c2f68f245f7fb2d531ef708036b0d7bc95b31719919264145a71f01bf8dca9bc

Contents?: true

Size: 992 Bytes

Versions: 1

Compression:

Stored size: 992 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 Error, 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.2 lib/story_branch/graphql/client.rb