Skip to content

Commit 7230a9d

Browse files
authored
Merge pull request #8 from hoverinc/custom-associations-for-fields
Add custom associations for fields
2 parents 4837b9e + 65e0548 commit 7230a9d

File tree

7 files changed

+64
-3
lines changed

7 files changed

+64
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
graphql-eager_load (0.2.2)
4+
graphql-eager_load (0.2.3)
55

66
GEM
77
remote: https://rubygems.org/

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ users {
7474

7575
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: {}}`.
7676

77+
If you have a field that is derived from an association, but the association is not included in the query, you can define a `#custom_associations_for_fields` method to specify which associations to include for a specific field.
78+
79+
```ruby
80+
module Types
81+
class User < Types::Base
82+
field :estimates, [Types::Estimate], null: false
83+
field :org_names, [String], null: true
84+
85+
def org_names
86+
object.orgs.map(&:name)
87+
end
88+
89+
def self.custom_associations_for_fields
90+
{
91+
org_names: [:org]
92+
}
93+
end
94+
end
95+
end
96+
```
97+
7798
## Development
7899

79100
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.

lib/graphql/eager_load/builder.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def self.call(selections:, model:)
4242
else
4343
includes.merge!(builder.includes)
4444
end
45+
46+
includes.deep_merge!(builder.custom_associations_for_selection)
4547
end
4648
end
4749

@@ -66,6 +68,16 @@ def active_storage_attachment?
6668
model.reflect_on_attachment(field_name).present?
6769
end
6870

71+
def custom_associations_for_selection
72+
return {} unless field_owner.respond_to?(:custom_associations_for_fields)
73+
74+
custom_associations = field_owner.custom_associations_for_fields[field_name.to_sym]
75+
76+
return {} unless custom_associations
77+
78+
custom_associations.map { |association| [association, {}] }.to_h
79+
end
80+
6981
private
7082

7183
attr_reader :selection, :model

lib/graphql/eager_load/resolver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def associations_to_include
1212
graphql_eager_load_options(model: self.class.class_variable_get(:@@eager_load_model))
1313
end
1414

15-
def graphql_eager_load_options(selections: context.query.lookahead.selections, model:)
15+
def graphql_eager_load_options(model:, selections: context.query.lookahead.selections)
1616
Builder.call(selections: selections, model: model)
1717
end
1818

lib/graphql/eager_load/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Graphql
44
module EagerLoad
5-
VERSION = '0.2.2'
5+
VERSION = '0.2.3'
66
end
77
end

spec/graphql/eager_load_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,26 @@
9595
)
9696
end
9797
end
98+
99+
describe 'custom_associations_for_fields' do
100+
let(:model) { ::User }
101+
let(:query_string) do
102+
<<-QUERY
103+
query {
104+
users {
105+
nodes {
106+
email
107+
}
108+
}
109+
}
110+
QUERY
111+
end
112+
113+
it 'includes the specified associations' do
114+
expect(options).to eq(
115+
jobs: {}
116+
)
117+
end
118+
end
98119
end
99120
end

spec/internal/app/graphql/types/user.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class User < GraphQL::Schema::Object
77
field :proposal_documents, [ProposalDocument], null: false
88
field :order, Order, null: false
99
field :photo, Types::File, null: true
10+
field :email, String, null: true
1011

1112
def order
1213
{ code: SecureRandom.uuid }
@@ -19,5 +20,11 @@ def photo
1920
def self.allow_include_builder_fields
2021
[:photo]
2122
end
23+
24+
def self.custom_associations_for_fields
25+
{
26+
email: [:jobs]
27+
}
28+
end
2229
end
2330
end

0 commit comments

Comments
 (0)