Module: CanvasApi::GraphQLHelpers
- Included in:
- Render
- Defined in:
- lib/canvas_api/js_graphql_helpers.rb,
lib/canvas_api/rb_graphql_helpers.rb
Instance Method Summary collapse
- #canvas_name(type, input_type = false) ⇒ Object
- #enum_class_name(model, field_name) ⇒ Object
- #graphql_field_enums(model) ⇒ Object
- #graphql_fields(model, resource_name, argument = false, input_type = false) ⇒ Object
- #graphql_primitive(name, type, format) ⇒ Object
- #graphql_resolve(name, property) ⇒ Object
- #graphql_type(name, property, return_type = false, model = nil, input_type = false) ⇒ Object
- #is_basic_type(type) ⇒ Object
- #make_file_name(str) ⇒ Object
- #name_from_operation(operation) ⇒ Object
- #nested_arg(str) ⇒ Object
- #no_brackets(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
#canvas_name(type, input_type = false) ⇒ Object
63 64 65 66 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 63 def canvas_name(type, input_type = false) name = type.split('|').first.strip.singularize "LMSGraphQL::Types::Canvas::Canvas#{name}#{input_type ? 'Input' : ''}" end |
#enum_class_name(model, field_name) ⇒ Object
95 96 97 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 95 def enum_class_name(model, field_name) "#{model['id'].classify}#{field_name.classify}Enum" end |
#graphql_field_enums(model) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 99 def graphql_field_enums(model) 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 ") " class \#{enum_class_name(model, name)} < ::GraphQL::Schema::Enum\n \#{values}\n end\n CODE\n end\n end.compact\n if enums.length > 0\n enums.join(\"\\n \")\n else\n \"\"\n end\nend\n" |
#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_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
159 160 161 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 159 def is_basic_type(type) ["Int", "String", "Boolean", "LMSGraphQL::Types::DateTimeType", "Float", "ID"].include?(type) end |
#make_file_name(str) ⇒ Object
167 168 169 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 167 def make_file_name(str) str.underscore.split("/").last.split("|").first.gsub("canvas_", "").strip.singularize end |
#name_from_operation(operation) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 150 def name_from_operation(operation) type = no_brackets(type_from_operation(@operation)) if !is_basic_type(type) make_file_name(type) else "return_value" end end |
#nested_arg(str) ⇒ Object
200 201 202 203 204 205 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 200 def nested_arg(str) # TODO/HACK we are replacing values from the string here to get things to work for now. # However, removing these symbols means that the methods that use the arguments # generated herein will have bugs and be unusable. str.gsub("[", "_").gsub("]", "").gsub("*", "star").gsub("<", "_").gsub(">", "_") end |
#no_brackets(str) ⇒ Object
163 164 165 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 163 def no_brackets(str) str.gsub("[", "").gsub("]", "") end |
#params_as_string(parameters, paramTypes) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 207 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
171 172 173 174 175 176 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 171 def require_from_operation(operation) type = no_brackets(type_from_operation(@operation)) if !is_basic_type(type) "require_relative \"../../types/canvas/#{make_file_name(type)}\"" end end |
#require_from_properties(model) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 178 def require_from_properties(model) return unless model["properties"] requires = model["properties"].map do |name, property| type = no_brackets(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
193 194 195 196 197 198 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 193 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
146 147 148 |
# File 'lib/canvas_api/rb_graphql_helpers.rb', line 146 def type_from_operation(operation) type = graphql_type("operation", operation, true) end |