Module: GraphQL::Rails::Types
Overview
Type system responsible for resolving GraphQL types. Delegates creation of GraphQL types to ORM-specific extensions.
Instance Method Summary collapse
-
#add_extension(extension) ⇒ Object
Add an extension to the type system.
-
#clear ⇒ Object
Clear internal state, probably due to a Rails reload.
-
#explicit ⇒ Object
Array of types that should be explicitly included in the schema.
-
#lookup(type_name, id) ⇒ Object
Lookup an arbitrary object from its GraphQL type name and ID.
-
#resolve(type, required = false) ⇒ Object
Resolve an arbitrary type to a GraphQL type.
-
#to_field_name(name) ⇒ Object
Convert a field name to a string with the correct convention.
-
#to_type_name(name, namespace = '') ⇒ Object
Convert a type name to a string with the correct convention, applying an optional namespace.
-
#use_namespaces? ⇒ Boolean
Should extensions namespace their type names? This is necessary if multiple extensions are loaded, so as to avoid collisions in the shared type namespace.
Instance Method Details
#add_extension(extension) ⇒ Object
Add an extension to the type system. Generally, each ORM will have its own extension.
63 64 65 |
# File 'lib/graphql/rails/types.rb', line 63 def add_extension(extension) extensions.push extension end |
#clear ⇒ Object
Clear internal state, probably due to a Rails reload.
9 10 11 12 13 14 15 |
# File 'lib/graphql/rails/types.rb', line 9 def clear @types = nil @explicit = nil extensions.each do |extension| extension.clear end end |
#explicit ⇒ Object
Array of types that should be explicitly included in the schema. Useful for ensuring that interface implmentations are included.
45 46 47 |
# File 'lib/graphql/rails/types.rb', line 45 def explicit @explicit ||= [] end |
#lookup(type_name, id) ⇒ Object
Lookup an arbitrary object from its GraphQL type name and ID.
50 51 52 |
# File 'lib/graphql/rails/types.rb', line 50 def lookup(type_name, id) try_extensions(:lookup, type_name, id) end |
#resolve(type, required = false) ⇒ Object
Resolve an arbitrary type to a GraphQL type. Lists can be specified with single-element arrays; for example:
- String
-
resolves to a list of GraphQL::STRING_TYPE objects.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/graphql/rails/types.rb', line 20 def resolve(type, required = false) if type.nil? raise 'Cannot resolve nil type' elsif required resolve(type).to_non_null_type elsif type.is_a?(GraphQL::BaseType) type elsif type.is_a?(Array) unless type.length == 1 raise 'Lists must be specified with single-element arrays' end resolve(type.first).to_list_type elsif types.include?(type) resolve(types[type]) else resolve(try_extensions(:resolve, type) || begin # TODO: Decide whether to use String as a fallback, or raise. Rails.logger.warn "Unable to resolve type: #{type.name}" String end) end end |
#to_field_name(name) ⇒ Object
Convert a field name to a string with the correct convention.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/graphql/rails/types.rb', line 78 def to_field_name(name) # camelize strips leading underscores, which is undesirable. if name.to_s.starts_with?('_') "_#{to_field_name(name.to_s[1..-1])}" elsif Rails.config.camel_case name.to_s.camelize(:lower) else name.to_s end end |
#to_type_name(name, namespace = '') ⇒ Object
Convert a type name to a string with the correct convention, applying an optional namespace.
69 70 71 72 73 74 75 |
# File 'lib/graphql/rails/types.rb', line 69 def to_type_name(name, namespace = '') return namespace + to_type_name(name) unless namespace.blank? name = name.to_s name = name.camelize(:upper) if Rails.config.camel_case name = name.gsub(/\W/, '_') name end |
#use_namespaces? ⇒ Boolean
Should extensions namespace their type names? This is necessary if multiple extensions are loaded, so as to avoid collisions in the shared type namespace.
57 58 59 |
# File 'lib/graphql/rails/types.rb', line 57 def use_namespaces? extensions.count > 1 end |