Skip to content

Commit 1417366

Browse files
authored
Merge pull request #5 from hoverinc/readme-update
Update README.md
2 parents 255e4e3 + f833259 commit 1417366

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Graphql::EagerLoad
22

3-
This gem traverses your graphql query looking for fields on types that match
4-
associations on those types. For each field found to be an association, this
3+
This gem assumes you are using [Rails](http://rubyonrails.org) and [ActiveRecord](https://guides.rubyonrails.org/active_record_querying.html). And that your GraphQL types and fields map as closely to your data model as possible. It uses those assumptions, or conventions, to handle a N+1 prevention for you.
4+
5+
It does so by traversing your graphql query looking for fields on types that match
6+
associations on those types' corresponding models. For each field found to be an association, this
57
gem adds those associations to an [ActiveRecord::QueryMethods#includes](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-includes) hash. That hash can then be used to
68
eager load the associations of records returned in your graphql resolver.
79

@@ -35,6 +37,43 @@ end
3537

3638
The `.eager_load_model` and `#associations_to_eager_load` methods are provided by this gem.
3739

40+
41+
If your type defines a custom method for a field that maps to an ActiveRecord association, this gem will ignore that association and let your method handle it. If you still want to eager load the association in that case you need to let the gem know.
42+
43+
```ruby
44+
module Types
45+
class User < Types::Base
46+
field :estimates, [Types::Estimate], null: false
47+
field :profile_photo, Types::File, null: true
48+
49+
def profile_photo
50+
object.profile_photo if object.profile_photo.attached?
51+
end
52+
53+
def self.allow_include_builder_fields
54+
[:profile_photo]
55+
end
56+
end
57+
end
58+
```
59+
60+
Given this query:
61+
62+
```javascript
63+
users {
64+
id
65+
estimates {
66+
id
67+
}
68+
profilePhoto {
69+
redirectUrl
70+
filename
71+
}
72+
}
73+
```
74+
75+
The output of the `#associations_to_eager_load` helper method would be `{estimates: {}, profile_photo: {blob: {}}`. Without the `.allow_include_builder_fields` class method the output would be `{estimates: {}}`.
76+
3877
## Development
3978

4079
After checking out the repo, run `bundle` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bundle exec bin/console` for an interactive prompt that will allow you to experiment.

0 commit comments

Comments
 (0)