summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/common/extractors/graphql_extractor.rb
blob: c0cef61d2b2b3b2028464c136131e540dd29f8dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

module BulkImports
  module Common
    module Extractors
      class GraphqlExtractor
        def initialize(query)
          @query = query[:query]
        end

        def extract(context)
          client = graphql_client(context)

          Enumerator.new do |yielder|
            result = client.execute(
              client.parse(query.to_s),
              query.variables(context.entity)
            )

            yielder << result.original_hash.deep_dup
          end
        end

        private

        attr_reader :query

        def graphql_client(context)
          @graphql_client ||= BulkImports::Clients::Graphql.new(
            url: context.configuration.url,
            token: context.configuration.access_token
          )
        end

        def parsed_query
          @parsed_query ||= graphql_client.parse(query.to_s)
        end
      end
    end
  end
end