From 9395f52f11f418ea92345b35515c57ce31d7e2b0 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Thu, 16 Jan 2020 08:45:05 +0100 Subject: [PATCH] creativework for person https://github.com/datacite/bracco/issues/270 --- ..._creative_work_connection_with_meta_type.rb | 18 ++++++++++++++++++ app/graphql/types/person_type.rb | 11 +++++++++++ 2 files changed, 29 insertions(+) create mode 100644 app/graphql/types/person_creative_work_connection_with_meta_type.rb diff --git a/app/graphql/types/person_creative_work_connection_with_meta_type.rb b/app/graphql/types/person_creative_work_connection_with_meta_type.rb new file mode 100644 index 000000000..f8b020801 --- /dev/null +++ b/app/graphql/types/person_creative_work_connection_with_meta_type.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class PersonCreativeWorkConnectionWithMetaType < BaseConnection + edge_type(EventDataEdgeType, edge_class: EventDataEdge) + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true + + def total_count + Event.query(nil, obj_id: https_to_http(object.parent.id)).results.total + end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" if uri.present? + uri.to_s + end +end diff --git a/app/graphql/types/person_type.rb b/app/graphql/types/person_type.rb index e4c7dc811..b892815d1 100644 --- a/app/graphql/types/person_type.rb +++ b/app/graphql/types/person_type.rb @@ -24,6 +24,10 @@ class PersonType < BaseObject argument :first, Int, required: false, default_value: 25 end + field :creative_works, PersonCreativeWorkConnectionWithMetaType, null: true, description: "Authored creative works", connection: true do + argument :first, Int, required: false, default_value: 25 + end + def type "Person" end @@ -49,6 +53,13 @@ def software_source_codes(**_args) ElasticsearchLoader.for(Doi).load_many(ids) end + def creative_works(**_args) + ids = Event.query(nil, obj_id: https_to_http(object[:id])).results.to_a.map do |e| + doi_from_url(e.subj_id) + end + ElasticsearchLoader.for(Doi).load_many(ids) + end + def citation_count(**_args) dois = Event.query(nil, page: { size: 500 }, obj_id: https_to_http(object[:id])).results.to_a.map do |e| doi_from_url(e.subj_id)