Skip to content

Commit

Permalink
Addresses error where author aggregation in GraphQL response sometime…
Browse files Browse the repository at this point in the history
…s returns nil when creator has multiple nameIdentifiers
  • Loading branch information
codycooperross committed Jul 1, 2022
1 parent 8f309b8 commit 1c289f1
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ def facet_by_authors(arr)
arr.map { |hsh|
orcid_id = %r{\A(?:(http|https)://(orcid.org)/)(.+)\z}.match?(hsh["key"]) && hsh["key"]

if orcid_id.nil?
next
end

# The aggregation query should only return 1 hit, so hence the index
# into first element
creators = hsh.dig("authors", "hits", "hits")[0].dig("_source", "creators")
Expand Down
28 changes: 16 additions & 12 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,33 @@ def facet_by_language(arr)
end

def facet_by_authors(arr)
arr.map do |hsh|
orcid_id = hsh["key"]
arr.map { |hsh|
orcid_id = %r{\A(?:(http|https)://(orcid.org)/)(.+)\z}.match?(hsh["key"]) && hsh["key"]

if orcid_id.nil?
next
end

# The aggregation query should only return 1 hit, so hence the index
# into first element
creators = hsh.dig("authors", "hits", "hits")[0].dig("_source", "creators")

# Filter through creators to find creator that matches the key
matched_creator = creators.select do |creator|
if creator.key?("nameIdentifiers")
creator["nameIdentifiers"].each do |ni|
break ni["nameIdentifier"] == orcid_id
end
creator["nameIdentifiers"].any? { |ni| ni["nameIdentifier"] == orcid_id }
end
end

title = matched_creator[0]["name"]
if matched_creator.any?
title = matched_creator[0]["name"]

{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
{
"id" => orcid_id,
"title" => title,
"count" => hsh["doc_count"],
}
end
}.compact
end
end
150 changes: 150 additions & 0 deletions spec/graphql/types/work_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1051,4 +1051,154 @@
)
end
end

describe "get author aggregations when creators have multiple nameIdentifiers", elasticsearch: true do
let!(:work_one) do
create(
:doi,
aasm_state: "findable",
creators: [
{
"name" => "Garza, Kristian",
"nameType" => "Personal",
"nameIdentifiers" => [
{
"nameIdentifier" => "https://orcid.org/0000-0003-3484-6875",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
{
"nameIdentifier" => "http://id.loc.gov/authorities/names/n90722093",
"nameIdentifierScheme" => "LCNAF",
"schemeUri" => "http://id.loc.gov/authorities/names",
},
],
},
{
"familyName" => "Ross",
"givenName" => "Cody",
"name" => "Ross, Cody",
"nameIdentifiers" => [
{
"nameIdentifier" => "http://id.loc.gov/authorities/names/no90016802",
"nameIdentifierScheme" => "LCNAF",
"schemeUri" => "http://id.loc.gov/authorities/names",
},
{
"nameIdentifier" => "https://orcid.org/0000-0002-4684-9769",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
],
},
{
"name" => "Cody Ross",
"nameType" => "Personal",
},
],
)
end

let!(:work_two) do
create(
:doi,
aasm_state: "findable",
creators: [
{
"name" => "Garza, Kristian",
"nameType" => "Personal",
"nameIdentifiers" => [
{
"nameIdentifier" => "http://id.loc.gov/authorities/names/n90722093",
"nameIdentifierScheme" => "LCNAF",
"schemeUri" => "http://id.loc.gov/authorities/names",
},
{
"nameIdentifier" => "https://orcid.org/0000-0003-3484-6875",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
],
},
{
"familyName" => "Ross",
"givenName" => "Cody",
"name" => "Ross, Cody",
"nameIdentifiers" => [
{
"nameIdentifier" => "https://orcid.org/0000-0002-4684-9769",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
],
},
{
"name" => "Cody Ross",
"nameType" => "Personal",
},
{
"name" => "Department of Psychoceramics, University of Cambridge",
"nameIdentifiers" => [
{
"nameIdentifier" => "https://ror.org/013meh722",
"nameIdentifierScheme" => "ROR",
"schemeUri" => "https://ror.org",
},
],
"nameType" => "Organizational",
},
],
)
end

before do
Doi.import
sleep 2
end

let(:query_works) do
'query {
works(query:"") {
authors {
count
id
title
}
nodes {
creators {
id
name
givenName
familyName
affiliation {
id
name
}
}
}
}
}'
end

it "returns author aggregation that is an array of authors with ORCID nameIdentifiers" do
response = LupoSchema.execute(query_works).as_json

expect(response.dig("data", "works", "authors").count).to eq(2)

expect(response.dig("data", "works", "authors")).to eq(
[
{
"count" => 2,
"id" => "https://orcid.org/0000-0002-4684-9769",
"title" => "Ross, Cody"
},
{
"count" => 2,
"id" => "https://orcid.org/0000-0003-3484-6875",
"title" => "Garza, Kristian"
},
]
)
end
end
end

0 comments on commit 1c289f1

Please sign in to comment.