Module: ManageIQ::API::Common::GraphQL::Generator

Defined in:
lib/manageiq/api/common/graphql/generator.rb

Constant Summary collapse

PARAMETERS_PATH =
"/components/parameters".freeze
SCHEMAS_PATH =
"/components/schemas".freeze

Class Method Summary collapse

Class Method Details

.collection_field_resolvers(schema_overlay, collection) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/manageiq/api/common/graphql/generator.rb', line 63

def self.collection_field_resolvers(schema_overlay, collection)
  field_resolvers = {}
  schema_overlay.keys.each do |collection_regex|
    next unless collection.match(collection_regex)

    field_resolvers.merge!(schema_overlay.fetch_path(collection_regex, "field_resolvers") || {})
  end
  field_resolvers
end

.collection_schema_overlay(schema_overlay, collection) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/manageiq/api/common/graphql/generator.rb', line 73

def self.collection_schema_overlay(schema_overlay, collection)
  schema_overlay.keys.each_with_object({}) do |collection_regex, collection_schema_overlay|
    next unless collection.match?(collection_regex)

    collection_schema_overlay.merge!(schema_overlay[collection_regex] || {})
  end
end

.graphql_type(property_name, property_format, property_type) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/manageiq/api/common/graphql/generator.rb', line 27

def self.graphql_type(property_name, property_format, property_type)
  return "!types.ID" if property_name == "id"

  case property_type
  when "string"
    property_format == "date-time" ? "::ManageIQ::API::Common::GraphQL::Types::DateTime" : "types.String"
  when "number"
    "types.Float"
  when "boolean"
    "types.Boolean"
  when "integer"
    "::ManageIQ::API::Common::GraphQL::Types::BigInt"
  end
end

.init_schema(request, schema_overlay = {}) ⇒ 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/manageiq/api/common/graphql/generator.rb', line 81

def self.init_schema(request, schema_overlay = {})
  api_version       = ::ManageIQ::API::Common::GraphQL.version(request)
  version_namespace = "V#{api_version.tr('.', 'x')}"
  openapi_doc       = ::ManageIQ::API::Common::OpenApi::Docs.instance[api_version]
  openapi_content   = openapi_doc.content

  graphql_namespace = if ::ManageIQ::API::Common::GraphQL::Api.const_defined?(version_namespace, false)
                        ::ManageIQ::API::Common::GraphQL::Api.const_get(version_namespace)
                      else
                        ::ManageIQ::API::Common::GraphQL::Api.const_set(version_namespace, Module.new)
                      end

  return graphql_namespace.const_get("Schema") if graphql_namespace.const_defined?("Schema", false)

  resources = openapi_content["paths"].keys.sort
  collections = []
  resources.each do |resource|
    next unless openapi_content.dig("paths", resource, "get") # we only care for queries

    rmatch = resource.match("^/(.*/)?([^/]*)/{[[a-z]*_]*id}$")
    next unless rmatch

    collection = rmatch[2]
    klass_name = collection.camelize.singularize
    next if graphql_namespace.const_defined?("#{klass_name}Type", false)

    _schema_name, this_schema = openapi_schema(openapi_doc, klass_name)
    next if this_schema.nil? || this_schema["type"] != "object" || this_schema["properties"].nil?

    collections << collection

    model_class = klass_name.constantize
    model_encrypted_columns_set = (model_class.try(:encrypted_columns) || []).to_set

    model_properties = []
    properties = this_schema["properties"]
    properties.keys.sort.each do |property_name|
      next if model_encrypted_columns_set.include?(property_name)

      property_schema = properties[property_name]
      property_schema = openapi_content.dig(*path_parts(property_schema["$ref"])) if property_schema["$ref"]
      property_format = property_schema["format"] || ""
      property_type   = property_schema["type"]
      description     = property_schema["description"]

      property_graphql_type = graphql_type(property_name, property_format, property_type)
      model_properties << [property_name, property_graphql_type, description] if property_graphql_type
    end

    field_resolvers = collection_field_resolvers(schema_overlay, klass_name)
    model_is_associated, model_associations = resource_associations(openapi_content, collection)

    graphql_model_type_template = ERB.new(template("model_type"), nil, '<>').result(binding)
    graphql_namespace.module_eval(graphql_model_type_template)
  end

  graphql_query_type_template = ERB.new(template("query_type"), nil, '<>').result(binding)
  graphql_namespace.module_eval(graphql_query_type_template)

  graphql_schema_template = ERB.new(template("schema"), nil, '<>').result(binding)
  graphql_namespace.module_eval(graphql_schema_template)
  graphql_namespace.const_get("Schema")
end

.openapi_schema(openapi_doc, klass_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/manageiq/api/common/graphql/generator.rb', line 11

def self.openapi_schema(openapi_doc, klass_name)
  schemas = openapi_doc.content.dig(*path_parts(SCHEMAS_PATH))
  [klass_name, "#{klass_name}Out"].each do |name|
    schema = schemas[name]
    return [name, schema] if schema
  end
end

.path_parts(openapi_path) ⇒ Object



19
20
21
# File 'lib/manageiq/api/common/graphql/generator.rb', line 19

def self.path_parts(openapi_path)
  openapi_path.split("/")[1..-1]
end

.resource_associations(openapi_content, collection) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/manageiq/api/common/graphql/generator.rb', line 42

def self.resource_associations(openapi_content, collection)
  collection_is_associated = openapi_content["paths"].keys.any? do |path|
    path.match?("^/[^/]*/{[[a-z]*_]*id}/#{collection}$") &&
      openapi_content.dig("paths", path, "get").present?
  end
  collection_associations = []
  openapi_content["paths"].keys.each do |path|
    subcollection_match = path.match("^/#{collection}/{[[a-z]*_]*id}/([^/]*)$")
    next unless subcollection_match

    subcollection = subcollection_match[1]
    next unless openapi_content["paths"].keys.any? do |subcollection_path|
      subcollection_path.match?("^/#{subcollection}/{[[a-z]*_]*id}$") &&
      openapi_content.dig("paths", subcollection_path, "get").present?
    end

    collection_associations << subcollection
  end
  [collection_is_associated ? true : false, collection_associations.sort]
end

.template(type) ⇒ Object



23
24
25
# File 'lib/manageiq/api/common/graphql/generator.rb', line 23

def self.template(type)
  File.read(Pathname.new(__dir__).join(File.expand_path("templates", __dir__), "#{type}.erb").to_s)
end