Module: GraphQL::Schema::LazyHandlingMethods

Included in:
GraphQL::Schema, GraphQL::Schema
Defined in:
lib/graphql/schema.rb

Instance Method Summary collapse

Instance Method Details

#after_any_lazies(maybe_lazies) ⇒ 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.

Return a lazy if any of maybe_lazies are lazy, otherwise, call the block eagerly and return the result.

Parameters:

  • maybe_lazies (Array)

144
145
146
147
148
149
150
151
152
# File 'lib/graphql/schema.rb', line 144

def after_any_lazies(maybe_lazies)
  if maybe_lazies.any? { |l| lazy?(l) }
    GraphQL::Execution::Lazy.all(maybe_lazies).then do |result|
      yield result
    end
  else
    yield maybe_lazies
  end
end

#after_lazy(value, &block) ⇒ 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.

Call the given block at the right time, either:

  • Right away, if value is not registered with lazy_resolve
  • After resolving value, if it's registered with lazy_resolve (eg, Promise)

104
105
106
107
108
109
110
111
112
113
114
# File 'lib/graphql/schema.rb', line 104

def after_lazy(value, &block)
  if lazy?(value)
    GraphQL::Execution::Lazy.new do
      result = sync_lazy(value)
      # The returned result might also be lazy, so check it, too
      after_lazy(result, &block)
    end
  else
    yield(value) if block_given?
  end
end

#lazy?(obj) ⇒ Boolean

Returns True if this object should be lazily resolved.

Returns:

  • (Boolean)

    True if this object should be lazily resolved


136
137
138
# File 'lib/graphql/schema.rb', line 136

def lazy?(obj)
  !!lazy_method_name(obj)
end

#lazy_method_name(obj) ⇒ Symbol?

Returns The method name to lazily resolve obj, or nil if obj's class wasn't registered with #lazy_resolve.

Returns:

  • (Symbol, nil)

    The method name to lazily resolve obj, or nil if obj's class wasn't registered with #lazy_resolve.


131
132
133
# File 'lib/graphql/schema.rb', line 131

def lazy_method_name(obj)
  lazy_methods.get(obj)
end

#sync_lazy(value) ⇒ 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.

Override this method to handle lazy objects in a custom way.

Parameters:

Returns:

  • (Object)

    A GraphQL-ready (non-lazy) object


120
121
122
123
124
125
126
127
128
# File 'lib/graphql/schema.rb', line 120

def sync_lazy(value)
  lazy_method = lazy_method_name(value)
  if lazy_method
    synced_value = value.public_send(lazy_method)
    sync_lazy(synced_value)
  else
    value
  end
end