Class: GraphQL::Schema::BuildFromDefinition::ResolveMap Private

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/build_from_definition/resolve_map.rb,
lib/graphql/schema/build_from_definition/resolve_map/default_resolve.rb
more...

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrap a user-provided hash of resolution behavior for easy access at runtime.

Coerce scalar values by: - Checking for a function in the map like { Date: { coerce_input: ->(val, ctx) { ... }, coerce_result: ->(val, ctx) { ... } } } - Falling back to a passthrough

Interface/union resolution can be provided as a resolve_type: key.

Defined Under Namespace

Modules: NullScalarCoerce Classes: DefaultResolve

Instance Method Summary collapse

Constructor Details

#initialize(user_resolve_hash) ⇒ ResolveMap

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ResolveMap.

[View source]

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/schema/build_from_definition/resolve_map.rb', line 23

def initialize(user_resolve_hash)
  @resolve_hash = Hash.new do |h, k|
    # For each type name, provide a new hash if one wasn't given:
    h[k] = Hash.new do |h2, k2|
      if k2 == "coerce_input" || k2 == "coerce_result"
        # This isn't an object field, it's a scalar coerce function.
        # Use a passthrough
        NullScalarCoerce
      else
        # For each field, provide a resolver that will
        # make runtime checks & replace itself
        h2[k2] = DefaultResolve.new(h2, k2)
      end
    end
  end
  @user_resolve_hash = user_resolve_hash
  # User-provided resolve functions take priority over the default:
  @user_resolve_hash.each do |type_name, fields|
    type_name_s = type_name.to_s
    case fields
    when Hash
      fields.each do |field_name, resolve_fn|
        @resolve_hash[type_name_s][field_name.to_s] = resolve_fn
      end
    when Proc
      # for example, "resolve_type"
      @resolve_hash[type_name_s] = fields
    else
      raise ArgumentError, "Unexpected resolve hash value for #{type_name.inspect}: #{fields.inspect} (#{fields.class})"
    end
  end

  # Check the normalized hash, not the user input:
  if @resolve_hash.key?("resolve_type")
    define_singleton_method :resolve_type do |type, obj, ctx|
      @resolve_hash.fetch("resolve_type").call(type, obj, ctx)
    end
  end
end

Instance Method Details

#call(type, field, obj, args, ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

63
64
65
66
# File 'lib/graphql/schema/build_from_definition/resolve_map.rb', line 63

def call(type, field, obj, args, ctx)
  resolver = @resolve_hash[type.graphql_name][field.graphql_name]
  resolver.call(obj, args, ctx)
end

#coerce_input(type, value, ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

68
69
70
# File 'lib/graphql/schema/build_from_definition/resolve_map.rb', line 68

def coerce_input(type, value, ctx)
  @resolve_hash[type.graphql_name]["coerce_input"].call(value, ctx)
end

#coerce_result(type, value, ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

72
73
74
# File 'lib/graphql/schema/build_from_definition/resolve_map.rb', line 72

def coerce_result(type, value, ctx)
  @resolve_hash[type.graphql_name]["coerce_result"].call(value, ctx)
end