-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3056 from alphagov/ministers-index-graphql-perfor…
…mance Use GraphQL-Ruby Dataloaders to improve Ministers Index performance
- Loading branch information
Showing
8 changed files
with
232 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Sources | ||
class LinkedToEditionsSource < GraphQL::Dataloader::Source | ||
# rubocop:disable Lint/MissingSuper | ||
def initialize(parent_object:) | ||
@object = parent_object | ||
@content_store = parent_object.content_store.to_sym | ||
end | ||
# rubocop:enable Lint/MissingSuper | ||
|
||
def fetch(link_types) | ||
all_links = @object.document.link_set_links | ||
.includes(target_documents: @content_store) # content_store is :live or :draft (a Document has_one Edition by that name) | ||
.where(link_type: link_types) | ||
.where(target_documents: { locale: "en" }) | ||
|
||
link_types_map = link_types.index_with { [] } | ||
|
||
all_links.each_with_object(link_types_map) { |link, hash| | ||
hash[link.link_type].concat(editions_for_link(link)) | ||
}.values | ||
end | ||
|
||
private | ||
|
||
def editions_for_link(link) | ||
link.target_documents.map { |document| document.send(@content_store) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Sources | ||
class PersonCurrentRolesSource < GraphQL::Dataloader::Source | ||
def fetch(person_content_ids) | ||
all_roles = Edition | ||
.live | ||
.includes( | ||
document: { | ||
reverse_links: { # role -> role_appointment | ||
link_set: { | ||
documents: [ | ||
:editions, # role_appointment | ||
:link_set_links, # role_appointment -> person | ||
], | ||
}, | ||
}, | ||
}, | ||
) | ||
.where( | ||
document_type: "ministerial_role", | ||
document: { locale: "en" }, | ||
reverse_links: { link_type: "role" }, | ||
documents: { locale: "en" }, # role_appointment Documents | ||
editions_documents: { document_type: "role_appointment" }, # role_appointment Editions | ||
link_set_links: { target_content_id: person_content_ids, link_type: "person" }, | ||
) | ||
.where("editions_documents.details ->> 'current' = 'true'") # editions_documents is the alias that Active Record gives to the role_appointment Editions in the SQL query | ||
.order(reverse_links: { position: :asc }) | ||
|
||
ids_map = person_content_ids.index_with { [] } | ||
|
||
all_roles.each_with_object(ids_map) { |role, hash| | ||
hash[person_content_id_for_role(role)] << role | ||
}.values | ||
end | ||
|
||
private | ||
|
||
def person_content_id_for_role(role) | ||
role_appointment_documents_for_role(role) | ||
.flat_map(&:link_set_links) | ||
.find { |link| link.link_type == "person" } # role_appointment -> person | ||
.target_content_id | ||
end | ||
|
||
def role_appointment_documents_for_role(role) | ||
role.document.reverse_links | ||
.select { |link| link.link_type == "role" } # role -> role_appointment | ||
.flat_map { |link| link.link_set.documents } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
RSpec.describe Sources::LinkedToEditionsSource do | ||
it "returns the specified link set links" do | ||
source_document = create(:edition) | ||
target_document_1 = create(:edition) | ||
target_document_2 = create(:edition) | ||
target_document_3 = create(:edition) | ||
link_set = create(:link_set, content_id: source_document.content_id) | ||
create(:link, link_set: link_set, target_content_id: target_document_1.content_id, link_type: "test_link") | ||
create(:link, link_set: link_set, target_content_id: target_document_2.content_id, link_type: "another_link_type") | ||
create(:link, link_set: link_set, target_content_id: target_document_3.content_id, link_type: "test_link") | ||
|
||
GraphQL::Dataloader.with_dataloading do |dataloader| | ||
request = dataloader.with(described_class, parent_object: source_document).request("test_link") | ||
|
||
expect(request.load).to eq([target_document_1, target_document_3]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
RSpec.describe Sources::PersonCurrentRolesSource do | ||
include MinistersIndexHelpers | ||
|
||
it "returns the current roles for a person" do | ||
person_1 = create_person("Person 1") | ||
role_1 = create_role("Role 1") | ||
role_2 = create_role("Role 2") | ||
role_3 = create_role("Role 3") | ||
appoint_person_to_role(person_1, role_1) | ||
appoint_person_to_role(person_1, role_2, current: false) | ||
appoint_person_to_role(person_1, role_3) | ||
|
||
GraphQL::Dataloader.with_dataloading do |dataloader| | ||
request = dataloader.with(described_class).request(person_1.content_id) | ||
|
||
expect(request.load).to eq([role_1, role_3]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.