You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Execution::Next` supports field configuration shorthands for common dataloader usage. Under the hood, these make sure data fetching is batched and cached.
141
+
142
+
#### Sources
143
+
144
+
Use a custom dataloader source from your application:
145
+
146
+
```ruby
147
+
classTypes::CommentType
148
+
# Equivalent to `dataload(Sources::CommentRating, object)`
149
+
field :rating, Integer, dataload:Sources::CommentRating
150
+
151
+
# `using:`: A method to call to get a value to pass to dataloader
152
+
# `by: [...]`: An array of arguments to pass on to dataloader
153
+
#
154
+
# Equivalent to `dataload(Sources::ReadingDuration, :comment, object.body)
155
+
field :reading_duration, Integer, dataload: { with:Sources::ReadingDuration, using::body, by: [:comment] }
156
+
```
157
+
158
+
#### Rails Associations
159
+
160
+
Load ActiveRecord associations using {{ "GraphQL::Dataloader::ActiveRecordAssociationSource" | api_doc }}:
161
+
162
+
```ruby
163
+
classTypes::CommentType < Types::BaseObject
164
+
# Equivalent to `dataload_association(:post)`
165
+
field :post, Types::Post, dataload: { association:true }
166
+
# Equivalent to `dataload_association(:user)
167
+
field :author, Types::Post, dataload: { association::user }
168
+
end
169
+
```
170
+
171
+
#### Rails Records
172
+
173
+
Load ActiveRecord associations using {{ "GraphQL::Dataloader::ActiveRecordSource" | api_doc }}.
174
+
175
+
```ruby
176
+
classTypes::SearchResult < Types::BaseObject
177
+
# Equivalent to `dataload_record(Post, object.post_id)`
178
+
field :post, Types::Post, dataload: { model:Post, using::post_id }
179
+
# Equivalent to `dataload_record(User, object.created_by_handle, find_by: :handle)`
180
+
field :author, Types::User, dataload: { model:User, using::created_by_handle, find_by::handle }
Copy file name to clipboardExpand all lines: lib/graphql/schema/field.rb
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -197,6 +197,7 @@ def method_conflict_warning?
197
197
# @param resolve_batch [Symbol, true, nil] Used by {Schema.execute_next} map `objects` to a same-sized Array of results. Called on the owner type class with `objects, context, **arguments`.
198
198
# @param resolve_each [Symbol, true, nil] Used by {Schema.execute_next} to get a value value for each item. Called on the owner type class with `object, context, **arguments`.
199
199
# @param resolve_legacy_instance_method [Symbol, true, nil] Used by {Schema.execute_next} to get a value value for each item. Calls an instance method on the object type class.
200
+
# @param dataload [Class, Hash] Shorthand for making dataloader calls
200
201
# @param max_page_size [Integer, nil] For connections, the maximum number of items to return from this field, or `nil` to allow unlimited results.
201
202
# @param default_page_size [Integer, nil] For connections, the default number of items to return from this field, or `nil` to return unlimited results.
202
203
# @param introspection [Boolean] If true, this field will be marked as `#introspection?` and the name may begin with `__`
@@ -219,7 +220,7 @@ def method_conflict_warning?
219
220
# @param relay_nodes_field [Boolean] (Private, used by GraphQL-Ruby)
220
221
# @param extras [Array<:ast_node, :parent, :lookahead, :owner, :execution_errors, :graphql_name, :argument_details, Symbol>] Extra arguments to be injected into the resolver for this field
221
222
# @param definition_block [Proc] an additional block for configuring the field. Receive the field as a block param, or, if no block params are defined, then the block is `instance_eval`'d on the new {Field}.
# Look up an associated record using a Rails association (via {Dataloader::ActiveRecordAssociationSource})
60
70
# @param association_name [Symbol] A `belongs_to` or `has_one` association. (If a `has_many` association is named here, it will be selected without pagination.)
61
71
# @param record [ActiveRecord::Base] The object that the association belongs to.
Copy file name to clipboardExpand all lines: lib/graphql/schema/member/has_fields.rb
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,7 @@ module HasFields
48
48
# @option kwargs [Boolean] :dynamic_introspection (Private, used by GraphQL-Ruby)
49
49
# @option kwargs [Boolean] :relay_node_field (Private, used by GraphQL-Ruby)
50
50
# @option kwargs [Boolean] :relay_nodes_field (Private, used by GraphQL-Ruby)
51
+
# @option kwargs [Class, Hash] :dataload Shorthand for dataloader lookups
51
52
# @option kwargs [Array<:ast_node, :parent, :lookahead, :owner, :execution_errors, :graphql_name, :argument_details, Symbol>] :extras Extra arguments to be injected into the resolver for this field
52
53
# @param kwargs [Hash] Keywords for defining the field. Any not documented here will be passed to your base field class where they must be handled.
53
54
# @param definition_block [Proc] an additional block for configuring the field. Receive the field as a block param, or, if no block params are defined, then the block is `instance_eval`'d on the new {Field}.
0 commit comments