Class: Reports::ExternalGraphqlClient

Inherits:
Object
  • Object
show all
Defined in:
app/commands/reports/external_graphql_client.rb

Constant Summary collapse

GRAPHQL_REPORTS_BACKEND_END_POINT =
ENV.fetch("GRAPHQL_REPORTS_BACKEND_END_POINT", "http://127.0.0.1:3005/graphql")
APP_NAME_PREFIX =
ENV.fetch("APP_NAME_PREFIX", "ReadyGop::")

Class Method Summary collapse

Class Method Details

.create_resource(mutation_name: nil, input_name: nil, attributes: nil) ⇒ Object



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
# File 'app/commands/reports/external_graphql_client.rb', line 10

def self.create_resource(mutation_name: nil, input_name: nil, attributes: nil)
  return if mutation_name.nil? || attributes.nil?
  capitalized_mutation = mutation_name.camelize(:lower)

  # Add the prefix to the name to differentiate between the resources in differente projects for RaaS
  if mutation_name == "createResource" && attributes[:name].present? && !attributes[:name].to_s.start_with?(APP_NAME_PREFIX)
    inflection_name = attributes[:name] == "DwDate" ? "DWDate" : attributes[:name]
    attributes[:name] = "#{APP_NAME_PREFIX}#{inflection_name}"
  end

  # Define the GraphQL mutation
  # This is a simple example; adjust the mutation according to your schema
  mutation = <<~GRAPHQL
    mutation #{mutation_name}($input: #{input_name}!) {
      #{capitalized_mutation}(attributes: $input) {
        resource {
          id
        }
        errors
      }
    }
  GRAPHQL
  variables = {
    input: {
      **attributes
    }
  }
  faraday_post(query: mutation, variables: variables)
end

.faraday_post(query:, variables: {}, auth_header: nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/commands/reports/external_graphql_client.rb', line 81

def self.faraday_post(query:, variables: {}, auth_header: nil)
  conn = Faraday.new(GRAPHQL_REPORTS_BACKEND_END_POINT, ssl: { verify: false })
  response = conn.post do |req|
    req.headers["Content-Type"] = "application/json"
    req.headers["Authorization"] = auth_header if auth_header.present?
    req.body = {
      query: query,
      variables: variables
    }.to_json
  end

  if response.success?
    parsed_response = JSON.parse(response.body)
  else
    parsed_response = { error: "Request failed", status: response.status, body: response.body }
  end

  # remove the ReadyGop:: prefix from the name in the response to avoid .constantize issues
  # also set DWDate correctly to avoid inflection issues
  if parsed_response&.dig("data", "resourceName", "name")&.start_with?(APP_NAME_PREFIX)
    parsed_response["data"]["resourceName"]["name"] = parsed_response["data"]["resourceName"]["name"].remove(APP_NAME_PREFIX)
    parsed_response["data"]["resourceName"]["name"] = "DwDate" if parsed_response["data"]["resourceName"]["name"] == "DWDate"
    # same but for the query_all_resources response
  elsif parsed_response&.dig("data", "resources", "edges").is_a?(Array)
    parsed_response["data"]["resources"]["edges"].each do |edge|
      next if edge["node"].nil?

      if edge["node"]["name"].start_with?(APP_NAME_PREFIX)
        edge["node"]["name"] = edge["node"]["name"].remove(APP_NAME_PREFIX)
        edge["node"]["name"] = "DwDate" if edge["node"]["name"] == "DWDate"
      end
    end
  end
  parsed_response
end

.faraday_post_with_auth(query:, variables: {}, auth_header: nil) ⇒ Object



77
78
79
# File 'app/commands/reports/external_graphql_client.rb', line 77

def self.faraday_post_with_auth(query:, variables: {}, auth_header: nil)
  faraday_post(query: query, variables: variables, auth_header: auth_header)
end

.query_all_resourcesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/commands/reports/external_graphql_client.rb', line 59

def self.query_all_resources
  query = <<~GRAPHQL
    query getAllResources {
      resources {
        edges {
          node {
            id
            name
            type
          }
        }
      }
    }
  GRAPHQL

  faraday_post(query: query, variables: nil)
end

.query_resource_by_name(name: nil, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/commands/reports/external_graphql_client.rb', line 40

def self.query_resource_by_name(name: nil, **kwargs)
  name ||= kwargs[:name] # compatibility with hash
  name = name == "DwDate" ? "DWDate" : name
  raise ArgumentError, "missing keyword: :name" if name.nil? || name.to_s.empty?
  prefixed_name = name&.to_s&.start_with?(APP_NAME_PREFIX) ? name : "#{APP_NAME_PREFIX}#{name}"

  query = <<~GRAPHQL
    query getResourceByName($name: String!) {
      resourceName(name: $name)
    }
  GRAPHQL

  variables = {
    name: prefixed_name
  }

  faraday_post(query: query, variables: variables)
end