Module: CanvasApi::GraphQLHelpers
- Included in:
- Render
- Defined in:
- lib/canvas_api/js_graphql_helpers.rb,
lib/canvas_api/rb_graphql_helpers.rb
Constant Summary collapse
- CONFLICTING_NAMES =
list of names that conflict with built in methods, Instead of generating a regular field for these ones, generate a resolver method
["end", "context", "next", "module"]
Instance Method Summary collapse
- #argument_format(name, type, description = "") ⇒ Object
- #canvas_name(type, input_type = false) ⇒ Object
- #enum_class_name(model, field_name, input_type) ⇒ Object
- #field_format(name, type, description = "") ⇒ Object
- #field_resolver_format(name, type, description) ⇒ Object
- #graphql_field_enums(model, input_type = false) ⇒ Object
- #graphql_fields(model, resource_name, argument = false, input_type = false) ⇒ Object
- #graphql_primitive(name, type, format) ⇒ Object
- #graphql_resolve(name, property) ⇒ Object
- #graphql_resolver_class(name) ⇒ Object
- #graphql_type(name, property, return_type = false, model = nil, input_type = false) ⇒ Object
- #is_basic_type(type) ⇒ Object
- #make_bespoke_file_name(str) ⇒ Object
- #make_file_name(str) ⇒ Object
- #name_from_operation(operation) ⇒ Object
- #no_brackets_period(str) ⇒ Object
- #params_as_string(parameters, paramTypes) ⇒ Object
- #require_from_operation(operation) ⇒ Object
- #require_from_properties(model) ⇒ Object
- #safe_js(str) ⇒ Object
- #safe_rb(str) ⇒ Object
- #type_from_operation(operation) ⇒ Object
Instance Method Details
#argument_format(name, type, description = "") ⇒ Object
181 182 183 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 181 def argument_format(name, type, description="") "argument :#{name.underscore}, #{type}, \"#{description}\", required: false\n" end |
#canvas_name(type, input_type = false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 88 def canvas_name(type, input_type = false) # Remove chars and fix spelling errors name = type.split('|').first.strip.gsub(" ", "_").singularize.gsub("MediaTrackk", "MediaTrack") # Handle comment in type if name.include?("BlackoutDate_The_result_(which_should_match_the_input_with_maybe_some_different_IDs).") name = "BlackoutDate" end "LMSGraphQL::Types::Canvas::Canvas#{name}#{input_type ? 'Input' : ''}" end |
#enum_class_name(model, field_name, input_type) ⇒ Object
127 128 129 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 127 def enum_class_name(model, field_name, input_type) "#{model['id'].classify}#{input_type ? 'Input' : ''}#{field_name.classify}Enum" end |
#field_format(name, type, description = "") ⇒ Object
185 186 187 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 185 def field_format(name, type, description="") "field :#{name.underscore}, #{type}, \"#{description}\", null: true\n" end |
#field_resolver_format(name, type, description) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 189 def field_resolver_format(name, type, description) <<-CODE field :#{name.underscore}, #{type}, "#{description}", resolver_method: :resolve_#{name.underscore}, null: true def resolve_#{name.underscore} object[:#{name.underscore}] end CODE end |
#graphql_field_enums(model, input_type = false) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 131 def graphql_field_enums(model, input_type = false) return unless model["properties"] enums = model["properties"].map do |name, property| if property["allowableValues"] values = property["allowableValues"]["values"].map do |value| "value \"#{value}\"" end.join("\n ") <<-CODE class #{enum_class_name(model, name, input_type)} < ::GraphQL::Schema::Enum #{values} end CODE end end.compact if enums.length > 0 enums.join("\n ") else "" end end |
#graphql_fields(model, resource_name, argument = false, input_type = false) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 67 def graphql_fields(model, resource_name) model["properties"].map do |name, property| # HACK. This property doesn't have any metadata. Throw in a couple lines of code # specific to this field. if name == "created_source" && property == "manual|sis|api" "#{name}: new GraphQLEnumType({ name: '#{name}', values: { manual: { value: 'manual' }, sis: { value: 'sis' }, api: { value: 'api' } } })" else description = "" if property["description"].present? && property["example"].present? description << "#{safe_js(property['description'])}. Example: #{safe_js(property['example'])}".gsub("..", "").gsub("\n", " ") end if type = graphql_type(name, property) resolve = graphql_resolve(name, property) resolve = "#{resolve}, " if resolve.present? "#{name}: { type: #{type}, #{resolve}description: \"#{description}\" }" end end end.compact end |
#graphql_primitive(name, type, format) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 35 def graphql_primitive(type, format) case type when "integer" "GraphQLInt" when "number" if format == "float64" "GraphQLFloat" else # TODO many of the LMS types with 'number' don't indicate a type so we have to guess # Hopefully that changes. For now we go with float "GraphQLFloat" end when "string" "GraphQLString" when "boolean" "GraphQLBoolean" when "datetime" "GraphQLDateTime" else raise "Unable to match requested primitive '#{type}' to GraphQL Type." end end |
#graphql_resolve(name, property) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 58 def graphql_resolve(name, property) if property["$ref"] "resolve(model){ return model.#{name}; }" elsif property["type"] == "array" && property["items"] && property["items"]["$ref"] "resolve(model){ return model.#{name}; }" end "resolve(model){ return model.#{name}; }" end |
#graphql_resolver_class(name) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 97 def graphql_resolver_class(name) # HACK Some resolvers have both singular and plural versions, so keep the plural on those return name.camelize if name == "get_custom_colors" name.classify end |
#graphql_type(name, property, return_type = false, model = nil, input_type = false) ⇒ Object
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 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 5 def graphql_type(name, property) if property["$ref"] "#{property['$ref']}, resolve: function(model){ return model.#{name}; }" else type = property["type"] case type when "integer", "string", "boolean", "datetime", "number" graphql_primitive(type, property["format"]) when "array" begin type = if property["items"]["$ref"] property["items"]["$ref"] else graphql_primitive(property["items"]["type"], property["items"]["format"]) end rescue puts "Unable to discover list type for '#{name}' ('#{property}'). Defaulting to GraphQLString" type = "GraphQLString" end "new GraphQLList(#{type})" when "object" puts "Using string type for '#{name}' ('#{property}') of type object." "GraphQLString" else puts "Unable to match '#{name}' requested property '#{property}' to GraphQL Type." "GraphQLString" end end end |
#is_basic_type(type) ⇒ Object
212 213 214 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 212 def is_basic_type(type) ["Int", "String", "Boolean", "GraphQL::Types::ISO8601DateTime", "Float", "ID"].include?(type) end |
#make_bespoke_file_name(str) ⇒ Object
224 225 226 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 224 def make_bespoke_file_name(str) str.underscore.split("/").last.split("|").first.gsub(" ", "_").strip.singularize end |
#make_file_name(str) ⇒ Object
220 221 222 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 220 def make_file_name(str) str.underscore.split("/").last.split("|").first.gsub(/^canvas_?/, "").gsub(" ", "_").strip.singularize end |
#name_from_operation(operation) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 203 def name_from_operation(operation) type = no_brackets_period(type_from_operation(@operation)) if !is_basic_type(type) make_file_name(type) else "return_value" end end |
#no_brackets_period(str) ⇒ Object
216 217 218 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 216 def no_brackets_period(str) str.gsub("[", "").gsub("]", "").gsub(".", "") end |
#params_as_string(parameters, paramTypes) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 259 def params_as_string(parameters, paramTypes) filtered = parameters.select{ |p| paramTypes.include?(p["paramType"]) } if filtered && !filtered.empty? s = filtered. map{ |p| " \"#{p['name']}\": #{nested_arg(p['name'])}" }. join(",\n") " {\n#{s}\n }" else " {}" end end |
#require_from_operation(operation) ⇒ Object
228 229 230 231 232 233 234 235 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 228 def require_from_operation(operation) type = no_brackets_period(type_from_operation(@operation)) if type.include?("CanvasBespoke") "require_relative \"../../types/canvas_bespoke/#{make_bespoke_file_name(type)}\"" elsif !is_basic_type(type) "require_relative \"../../types/canvas/#{make_file_name(type)}\"" end end |
#require_from_properties(model) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 237 def require_from_properties(model) return unless model["properties"] requires = model["properties"].map do |name, property| type = no_brackets_period(graphql_type(name, property, true, model)) if !is_basic_type(type) && !property["allowableValues"] "require_relative \"#{make_file_name(type)}\"" end end.compact if requires.length > 0 requires.join("\n") else "" end end |
#safe_js(str) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/canvas_api/js_graphql_helpers.rb', line 90 def safe_js(str) str = str.join(", ") if str.is_a?(Array) str = str.map { |_k, v| v }.join(", ") if str.is_a?(Hash) return str unless str.is_a?(String) str.gsub('"', "'") end |
#safe_rb(str) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 252 def safe_rb(str) str = str.join(", ") if str.is_a?(Array) str = str.map { |_k, v| v }.join(", ") if str.is_a?(Hash) return str unless str.is_a?(String) str.gsub('"', "'") end |
#type_from_operation(operation) ⇒ Object
199 200 201 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 199 def type_from_operation(operation) type = graphql_type("operation", operation, true) end |