Sha256: 88e435ec1f7b91ce3513a73e82f2689992be3c4f519caf4d35b4668bd8633b34

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'zenaton/exceptions'

module Zenaton
  # Collection of utility classes for the Zenaton library
  module Services
    # Service that:
    # - handles graphql calls
    # - translates exceptions into Zenaton specific ones
    class GraphQL
      CREATE_WORKFLOW_SCHEDULE = <<-'GRAPHQL'
        mutation ($createWorkflowScheduleInput: CreateWorkflowScheduleInput!) {
          createWorkflowSchedule(input: $createWorkflowScheduleInput) {
            schedule {
              id
            }
          }
        }
      GRAPHQL

      CREATE_TASK_SCHEDULE = <<-'GRAPHQL'
        mutation ($createTaskScheduleInput: CreateTaskScheduleInput!) {
          createTaskSchedule(input: $createTaskScheduleInput) {
            schedule {
              id
            }
          }
        }
      GRAPHQL

      def initialize(http:)
        @http = http
      end

      # Makes a GRAPHQL request with some data and sets the correct headers
      #
      # @param url [String] the url for the request
      # @param body [Hash] the payload to send with the request
      # @return [Hash] the parsed json response
      def request(url, query, variables = nil, headers = {})
        body = { 'query' => query }
        body['variables'] = variables if variables

        res_body = @http.post(url, body, headers)
        handle_response_body(res_body)
      end

      private

      def handle_response_body(response_body)
        if external_error?(response_body)
          raise Zenaton::ExternalError, format_external_error(response_body)
        end

        response_body && response_body['data']
      end

      def external_error?(response_body)
        response_body&.key?('errors')
      end

      def format_external_error(response_body)
        response_body['errors'].map do |error|
          path = error['path'] ? "- #{error['path']}: " : ''
          "#{path}#{error['message']}"
        end.join("\n")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zenaton-0.5.3 lib/zenaton/services/graphql.rb
zenaton-0.5.2 lib/zenaton/services/graphql.rb
zenaton-0.5.1 lib/zenaton/services/graphql.rb
zenaton-0.5.0 lib/zenaton/services/graphql.rb