lib/graphql/relay/global_node_identification.rb in graphql-0.18.4 vs lib/graphql/relay/global_node_identification.rb in graphql-0.18.5
- old
+ new
@@ -10,10 +10,14 @@
class GlobalNodeIdentification
include GraphQL::Define::InstanceDefinable
accepts_definitions(:object_from_id, :type_from_object, :to_global_id, :from_global_id, :description)
lazy_defined_attr_accessor :description
+ # Memoize the schema to support deprecated node_ident-level resolve functions
+ # TODO: remove after Schema.resolve_type is required
+ attr_accessor :schema
+
class << self
attr_accessor :id_separator
end
self.id_separator = "-"
@@ -26,16 +30,24 @@
# Returns `NodeInterface`, which all Relay types must implement
def interface
@interface ||= begin
ensure_defined
ident = self
- GraphQL::InterfaceType.define do
- name "Node"
- field :id, !types.ID
- resolve_type -> (obj, schema) {
- ident.type_from_object(obj)
- }
+ if @type_from_object_proc
+ # TODO: remove after Schema.resolve_type is required
+ GraphQL::InterfaceType.define do
+ name "Node"
+ field :id, !types.ID
+ resolve_type -> (obj, ctx) {
+ ident.type_from_object(obj)
+ }
+ end
+ else
+ GraphQL::InterfaceType.define do
+ name "Node"
+ field :id, !types.ID
+ end
end
end
end
# Returns a field for finding objects from a global ID, which Relay needs
@@ -88,25 +100,26 @@
@from_global_id_proc = proc
end
# Use the provided config to
# get a type for a given object
+ # TODO: remove after Schema.resolve_type is required
def type_from_object(object)
ensure_defined
- type_result = @type_from_object_proc.call(object)
- if type_result.nil?
- nil
- elsif !type_result.is_a?(GraphQL::BaseType)
- type_str = "#{type_result} (#{type_result.class.name})"
- raise "type_from_object(#{object}) returned #{type_str}, but it should return a GraphQL type"
- else
- type_result
+ warn("type_from_object(object) is deprecated; use Schema.resolve_type(object) instead")
+
+ if @type_from_object_proc
+ schema.resolve_type = @type_from_object_proc
+ @type_from_object_proc = nil
end
+
+ schema.resolve_type(object)
end
- def type_from_object=(proc)
+ def type_from_object=(new_type_from_object_proc)
ensure_defined
- @type_from_object_proc = proc
+ warn("type_from_object(object) is deprecated; use Schema.resolve_type(object) instead")
+ @type_from_object_proc = new_type_from_object_proc
end
# Use the provided config to
# get an object from a UUID
def object_from_id(id, ctx)