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)


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

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

#after_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.

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
115
116
# File 'lib/graphql/schema.rb', line 104

def after_lazy(value)
  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) do |final_result|
        yield(final_result) if block_given?
      end
    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



138
139
140
# File 'lib/graphql/schema.rb', line 138

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 wtih #lazy_resolve.

Returns:

  • (Symbol, nil)

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



133
134
135
# File 'lib/graphql/schema.rb', line 133

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



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

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