From a05dc128aa2a47ade407c2424925cc4af5abe74f Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 12 May 2019 18:49:37 +0200 Subject: [PATCH] graphql connections for researchers. #258 --- app/graphql/types/researcher_type.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/graphql/types/researcher_type.rb b/app/graphql/types/researcher_type.rb index bda45f5f6..53ef52411 100644 --- a/app/graphql/types/researcher_type.rb +++ b/app/graphql/types/researcher_type.rb @@ -9,4 +9,32 @@ 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, DatasetConnectionWithMetaType, null: false, description: "Authored datasets", connection: true, max_page_size: 100 do + argument :first, Int, required: false, default_value: 25 + end + + field :publications, DatasetConnectionWithMetaType, null: false, description: "Authored publications", connection: true, max_page_size: 100 do + argument :first, Int, required: false, default_value: 25 + end + + def datasets(**args) + ids = Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Person").fetch(:data, []).map do |e| + doi_from_url(e[:subj_id]) + end.join(",") + Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a + end + + def publications(**args) + ids = Event.query(nil, obj_id: object[:id], citation_type: "Person-ScholarlyArticle").fetch(:data, []).map do |e| + doi_from_url(e[:subj_id]) + end.join(",") + Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a + end + + def doi_from_url(url) + if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match?(url) + uri = Addressable::URI.parse(url) + uri.path.gsub(/^\//, "").downcase + end + end end