Skip to content

Commit

Permalink
graphql support for researchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 29, 2019
1 parent 5acdbe4 commit 49502fd
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
18 changes: 14 additions & 4 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ def funder(id:)
result
end

field :researcher, ResearcherType, null: false do
field :researcher, ResearcherType, null: true do
argument :id, ID, required: true
end

def researcher(id:)
result = Researcher.find_by_id(id).fetch(:data, []).first
fail ActiveRecord::RecordNotFound if result.nil?
ElasticsearchLoader.for(Researcher).load(orcid_from_url(id))
end

result
field :researchers, ResearcherConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def researchers(query: nil, first: nil)
Researcher.query(query, page: { number: 1, size: first }).results.to_a
end

field :organizations, OrganizationConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
Expand Down Expand Up @@ -428,4 +434,8 @@ def doi_from_url(url)
uri.path.gsub(/^\//, "").downcase
end
end

def orcid_from_url(url)
Array(/\A(http|https):\/\/orcid\.org\/(.+)/.match(url)).last
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/researcher_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class ResearcherConnectionWithMetaType < BaseConnection
edge_type(ResearcherEdgeType)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true

def total_count
args = object.arguments

Researcher.query(args[:query], page: { number: 1, size: 0 }).results.total
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class ResearcherDatasetConnectionWithMetaType < BaseConnection
field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Dataset-Person").results.total
Event.query(nil, obj_id: https_to_http(object.parent.uid ? "https://orcid.org/#{object.parent.uid}" : nil || object.parent[:id]), citation_type: "Dataset-Person").results.total
end

def https_to_http(url)
uri = Addressable::URI.parse(url)
uri.scheme = "http"
uri.scheme = "http" if uri.present?
uri.to_s
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/researcher_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ResearcherEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(ResearcherType)
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class ResearcherPublicationConnectionWithMetaType < BaseConnection
field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Person-ScholarlyArticle").results.total
Event.query(nil, obj_id: https_to_http(object.parent.uid ? "https://orcid.org/#{object.parent.uid}" : nil || object.parent[:id]), citation_type: "Person-ScholarlyArticle").results.total
end

def https_to_http(url)
uri = Addressable::URI.parse(url)
uri.scheme = "http"
uri.scheme = "http" if uri.present?
uri.to_s
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class ResearcherSoftwareConnectionWithMetaType < BaseConnection
field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Person-SoftwareSourceCode").results.total
Event.query(nil, obj_id: https_to_http(object.parent.uid ? "https://orcid.org/#{object.parent.uid}" : nil || object.parent[:id]), citation_type: "Person-SoftwareSourceCode").results.total
end

def https_to_http(url)
uri = Addressable::URI.parse(url)
uri.scheme = "http"
uri.scheme = "http" if uri.present?
uri.to_s
end
end
26 changes: 19 additions & 7 deletions app/graphql/types/researcher_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,50 @@ class ResearcherType < BaseObject
field :given_name, String, null: true, hash_key: "givenName", description: "Researcher given name"
field :family_name, String, null: true, hash_key: "familyName", description: "Researcher family name"
field :affiliation, [String], null: true, description: "Researcher affiliation"
field :datasets, ResearcherDatasetConnectionWithMetaType, null: false, description: "Authored datasets", connection: true, max_page_size: 100 do
field :datasets, ResearcherDatasetConnectionWithMetaType, null: true, description: "Authored datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

field :publications, ResearcherPublicationConnectionWithMetaType, null: false, description: "Authored publications", connection: true, max_page_size: 100 do
field :publications, ResearcherPublicationConnectionWithMetaType, null: true, description: "Authored publications", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

field :softwares, ResearcherSoftwareConnectionWithMetaType, null: false, description: "Authored software", connection: true, max_page_size: 100 do
field :softwares, ResearcherSoftwareConnectionWithMetaType, null: true, description: "Authored software", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

def id
object.fetch(:id, nil) || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil)
object.uid ? "https://orcid.org/#{object.uid}" : nil || object.fetch(:id, nil) || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil)
end

def name
object.name || object.fetch("name", nil)
end

def given_name
object.given_names || object.fetch("givenName", nil)
end

def family_name
object.family_name || object.fetch("familyName", nil)
end

def datasets(**args)
ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Dataset-Person").results.to_a.map do |e|
ids = Event.query(nil, obj_id: https_to_http(object.uid ? "https://orcid.org/#{object.uid}" : nil || object[:id]), citation_type: "Dataset-Person").results.to_a.map do |e|
doi_from_url(e.subj_id)
end
ElasticsearchLoader.for(Doi).load_many(ids)
end

def publications(**args)
ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Person-ScholarlyArticle").results.to_a.map do |e|
ids = Event.query(nil, obj_id: https_to_http(object.uid ? "https://orcid.org/#{object.uid}" : nil || object[:id]), citation_type: "Person-ScholarlyArticle").results.to_a.map do |e|
doi_from_url(e.subj_id)
end
ElasticsearchLoader.for(Doi).load_many(ids)
end

def softwares(**args)
ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Person-SoftwareSourceCode").results.to_a.map do |e|
ids = Event.query(nil, obj_id: https_to_http(object.uid ? "https://orcid.org/#{object.uid}" : nil || object[:id]), citation_type: "Person-SoftwareSourceCode").results.to_a.map do |e|
doi_from_url(e.subj_id)
end
ElasticsearchLoader.for(Doi).load_many(ids)
Expand Down

0 comments on commit 49502fd

Please sign in to comment.