Module: Insights::API::Common::GraphQL::Generator
- Defined in:
- lib/insights/api/common/graphql/generator.rb
Constant Summary collapse
- PARAMETERS_PATH =
"/components/parameters".freeze
- SCHEMAS_PATH =
"/components/schemas".freeze
Class Method Summary collapse
- .app_name ⇒ Object
- .base_init_schema(request, graphql_options, schema_overlay = {}) ⇒ Object
- .collection_field_resolvers(schema_overlay, collection) ⇒ Object
- .collection_schema_overlay(schema_overlay, collection) ⇒ Object
- .graphql_type(property_name, property_format, property_type) ⇒ Object
- .init_schema(request, schema_overlay = {}) ⇒ Object
- .init_schema_v2(request, schema_overlay = {}) ⇒ Object
- .openapi_schema(openapi_doc, klass_name) ⇒ Object
- .path_parts(openapi_path) ⇒ Object
- .pluggable_template_file_by(type) ⇒ Object
- .resource_associations(openapi_content, collection) ⇒ Object
- .root_dir ⇒ Object
- .template(type) ⇒ Object
- .template_file_by(type, root_dir = __dir__) ⇒ Object
- .template_path_by(type) ⇒ Object
Class Method Details
.app_name ⇒ Object
31 32 33 |
# File 'lib/insights/api/common/graphql/generator.rb', line 31 def self.app_name Rails.application.class.parent.name.underscore end |
.base_init_schema(request, graphql_options, schema_overlay = {}) ⇒ Object
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/insights/api/common/graphql/generator.rb', line 113 def self.base_init_schema(request, , = {}) api_version = ::Insights::API::Common::GraphQL.version(request) version_namespace = "V#{api_version.tr('.', 'x')}" openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance[api_version] openapi_content = openapi_doc.content graphql_namespace = if ::Insights::API::Common::GraphQL::Api.const_defined?(version_namespace, false) ::Insights::API::Common::GraphQL::Api.const_get(version_namespace) else ::Insights::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(, 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) unless graphql_namespace.const_defined?("#{klass_name}AggregateType", false) graphql_aggregate_model_type_template = ERB.new(template("aggregate_model_type"), nil, '<>').result(binding) graphql_namespace.module_eval(graphql_aggregate_model_type_template) end end graphql_aggregate_type_template = ERB.new(template("aggregate_type"), nil, '<>').result(binding) graphql_namespace.module_eval(graphql_aggregate_type_template) 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 |
.collection_field_resolvers(schema_overlay, collection) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/insights/api/common/graphql/generator.rb', line 87 def self.collection_field_resolvers(, collection) field_resolvers = {} .keys.each do |collection_regex| next unless collection.match(collection_regex) field_resolvers.merge!(.fetch_path(collection_regex, "field_resolvers") || {}) end field_resolvers end |
.collection_schema_overlay(schema_overlay, collection) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/insights/api/common/graphql/generator.rb', line 97 def self.(, collection) .keys.each_with_object({}) do |collection_regex, | next unless collection.match?(collection_regex) .merge!([collection_regex] || {}) end end |
.graphql_type(property_name, property_format, property_type) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/insights/api/common/graphql/generator.rb', line 51 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" ? "::Insights::API::Common::GraphQL::Types::DateTime" : "types.String" when "number" "types.Float" when "boolean" "types.Boolean" when "integer" "::Insights::API::Common::GraphQL::Types::BigInt" end end |
.init_schema(request, schema_overlay = {}) ⇒ Object
105 106 107 |
# File 'lib/insights/api/common/graphql/generator.rb', line 105 def self.init_schema(request, = {}) base_init_schema(request, { :use_pagination_v2 => false }, ) end |
.init_schema_v2(request, schema_overlay = {}) ⇒ Object
109 110 111 |
# File 'lib/insights/api/common/graphql/generator.rb', line 109 def self.init_schema_v2(request, = {}) base_init_schema(request, { :use_pagination_v2 => true }, ) end |
.openapi_schema(openapi_doc, klass_name) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/insights/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/insights/api/common/graphql/generator.rb', line 19 def self.path_parts(openapi_path) openapi_path.split("/")[1..-1] end |
.pluggable_template_file_by(type) ⇒ Object
35 36 37 38 39 |
# File 'lib/insights/api/common/graphql/generator.rb', line 35 def self.pluggable_template_file_by(type) templates_relative_path = "lib/#{app_name}/api/graphql/templates" template_path = File.(templates_relative_path, root_dir) Pathname.new(root_dir).join(template_path, "#{type}.erb") end |
.resource_associations(openapi_content, collection) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/insights/api/common/graphql/generator.rb', line 66 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 |
.root_dir ⇒ Object
27 28 29 |
# File 'lib/insights/api/common/graphql/generator.rb', line 27 def self.root_dir Rails.root end |
.template(type) ⇒ Object
47 48 49 |
# File 'lib/insights/api/common/graphql/generator.rb', line 47 def self.template(type) File.read(template_path_by(type)) end |
.template_file_by(type, root_dir = __dir__) ⇒ Object
23 24 25 |
# File 'lib/insights/api/common/graphql/generator.rb', line 23 def self.template_file_by(type, root_dir = __dir__) Pathname.new(root_dir).join(File.("templates", root_dir), "#{type}.erb") end |
.template_path_by(type) ⇒ Object
41 42 43 44 45 |
# File 'lib/insights/api/common/graphql/generator.rb', line 41 def self.template_path_by(type) template_path_pluggable = pluggable_template_file_by(type) template_path_default = template_file_by(type) template_path_pluggable.exist? ? template_path_pluggable : template_path_default end |