2021-01-29 00:20:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Common
|
|
|
|
module Extractors
|
|
|
|
class GraphqlExtractor
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(options = {})
|
|
|
|
@query = options[:query]
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def extract(context)
|
2021-02-22 17:27:13 +05:30
|
|
|
client = graphql_client(context)
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
response = client.execute(
|
2021-03-08 18:12:59 +05:30
|
|
|
client.parse(query.to_s),
|
2021-03-11 19:13:27 +05:30
|
|
|
query.variables(context)
|
2021-03-08 18:12:59 +05:30
|
|
|
).original_hash.deep_dup
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
BulkImports::Pipeline::ExtractedData.new(
|
|
|
|
data: response.dig(*query.data_path),
|
|
|
|
page_info: response.dig(*query.page_info_path)
|
|
|
|
)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
attr_reader :query
|
|
|
|
|
|
|
|
def graphql_client(context)
|
2021-01-29 00:20:46 +05:30
|
|
|
@graphql_client ||= BulkImports::Clients::Graphql.new(
|
2021-02-22 17:27:13 +05:30
|
|
|
url: context.configuration.url,
|
|
|
|
token: context.configuration.access_token
|
2021-01-29 00:20:46 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|