From 995f51edd75befd4093ae9fbeb2c1d252d21b512 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 4 Oct 2019 20:47:45 +0200 Subject: [PATCH] add person fields to graphql --- Gemfile | 2 +- Gemfile.lock | 40 ++++++------- .../dataset_connection_with_meta_type.rb | 4 +- app/graphql/types/doi_item.rb | 2 +- .../organization_connection_with_meta_type.rb | 4 +- ...on_researcher_connection_with_meta_type.rb | 2 +- app/graphql/types/organization_type.rb | 18 +++--- .../types/person_connection_with_meta_type.rb | 27 +++++++++ app/graphql/types/person_edge_type.rb | 5 ++ app/graphql/types/person_type.rb | 58 +++++++++++++++++++ .../publication_connection_with_meta_type.rb | 4 +- app/graphql/types/researcher_type.rb | 19 ------ .../software_connection_with_meta_type.rb | 4 +- 13 files changed, 130 insertions(+), 59 deletions(-) create mode 100644 app/graphql/types/person_connection_with_meta_type.rb create mode 100644 app/graphql/types/person_edge_type.rb create mode 100644 app/graphql/types/person_type.rb delete mode 100644 app/graphql/types/researcher_type.rb diff --git a/Gemfile b/Gemfile index 63d6257c4..a4fe45fab 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ gem 'json', '~> 1.8', '>= 1.8.5' gem 'oj', '>= 2.8.3' gem 'jsonlint', '~> 0.2.0' gem 'equivalent-xml', '~> 0.6.0' -gem 'nokogiri', '~> 1.8.1' +gem "nokogiri", ">= 1.10.4" gem 'diffy', '~> 3.2', '>= 3.2.1' gem 'commonmarker', '~> 0.17.9' gem 'iso8601', '~> 0.9.0' diff --git a/Gemfile.lock b/Gemfile.lock index a549d1df9..fc7b1a619 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,7 +68,7 @@ GEM activerecord (>= 4.2, < 6.1) aws-eventstream (1.0.3) aws-partitions (1.220.0) - aws-sdk-core (3.68.0) + aws-sdk-core (3.68.1) aws-eventstream (~> 1.0, >= 1.0.2) aws-partitions (~> 1.0) aws-sigv4 (~> 1.1) @@ -109,7 +109,7 @@ GEM latex-decode (~> 0.0) binding_of_caller (0.8.0) debug_inspector (>= 0.0.1) - bolognese (1.3.16) + bolognese (1.3.19) activesupport (>= 4.2.5, < 6) benchmark_methods (~> 0.7) bibtex-ruby (~> 4.1) @@ -124,13 +124,13 @@ GEM json-ld-preloaded (= 3.0.2) jsonlint (~> 0.2.0) loofah (~> 2.0, >= 2.0.3) - maremma (>= 3.5.7, < 5) + maremma (>= 4.3, < 5) namae (~> 1.0) - nokogiri (~> 1.8.1) + nokogiri (~> 1.10.4) oj (>= 2.8.3) postrank-uri (~> 1.0, >= 1.0.18) - rdf-rdfxml (~> 2.0) - rdf-turtle (~> 2.2) + rdf-rdfxml (~> 2.2) + rdf-turtle (~> 3.0, >= 3.0.6) thor (~> 0.19) bootsnap (1.4.5) msgpack (~> 1.0) @@ -240,7 +240,7 @@ GEM git (1.5.0) globalid (0.4.2) activesupport (>= 4.2.0) - google-protobuf (3.9.2) + google-protobuf (3.10.0-universal-darwin) graphql (1.9.12) graphql-batch (0.4.1) graphql (>= 1.3, < 2) @@ -257,7 +257,7 @@ GEM htmlentities (4.3.4) http-cookie (1.0.3) domain_name (~> 0.5) - i18n (1.6.0) + i18n (1.7.0) concurrent-ruby (~> 1.0) i18n_data (0.8.0) iso8601 (0.9.1) @@ -311,7 +311,7 @@ GEM rest-client (~> 2.0.2) marcel (0.3.3) mimemagic (~> 0.3.2) - maremma (4.2.6) + maremma (4.3) activesupport (>= 4.2.5, < 6) addressable (>= 2.3.6) builder (~> 3.2, >= 3.2.2) @@ -320,7 +320,7 @@ GEM faraday-encoding (~> 0.0.4) faraday_middleware (~> 0.12.0) multi_json (~> 1.12) - nokogiri (~> 1.8.1) + nokogiri (~> 1.10.4) oj (>= 2.8.3) method_source (0.9.2) mime-types (3.3) @@ -329,7 +329,7 @@ GEM mimemagic (0.3.3) mini_magick (4.9.5) mini_mime (1.0.2) - mini_portile2 (2.3.0) + mini_portile2 (2.4.0) minitest (5.12.2) money (6.13.4) i18n (>= 0.6.4, <= 2) @@ -340,13 +340,13 @@ GEM namae (1.0.1) netrc (0.11.0) nio4r (2.5.2) - nokogiri (1.8.5) - mini_portile2 (~> 2.3.0) + nokogiri (1.10.4) + mini_portile2 (~> 2.4.0) oj (2.18.5) oj_mimic_json (1.0.1) pandoc-ruby (2.0.2) parallel (1.17.0) - parser (2.6.4.1) + parser (2.6.5.0) ast (~> 2.4.0) postrank-uri (1.0.24) addressable (>= 2.4.0) @@ -414,9 +414,9 @@ GEM rdf (>= 2.2, < 4.0) rdf-rdfa (>= 2.2, < 4.0) rdf-xsd (>= 2.2, < 4.0) - rdf-turtle (2.2.2) + rdf-turtle (3.0.6) ebnf (~> 1.1) - rdf (>= 2.2, < 4.0) + rdf (~> 3.0) rdf-xsd (3.0.1) rdf (~> 3.0) regexp_parser (1.6.0) @@ -436,10 +436,10 @@ GEM rspec (>= 3.0.0, < 4.0.0) rspec-core (3.8.2) rspec-support (~> 3.8.0) - rspec-expectations (3.8.4) + rspec-expectations (3.8.5) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) - rspec-mocks (3.8.1) + rspec-mocks (3.8.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-rails (3.8.2) @@ -450,7 +450,7 @@ GEM rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-support (~> 3.8.0) - rspec-support (3.8.2) + rspec-support (3.8.3) rubocop (0.68.1) jaro_winkler (~> 1.5.1) parallel (~> 1.10) @@ -593,7 +593,7 @@ DEPENDENCIES mini_magick (~> 4.8) multi_json mysql2 (~> 0.4.4) - nokogiri (~> 1.8.1) + nokogiri (>= 1.10.4) oj (>= 2.8.3) oj_mimic_json (~> 1.0, >= 1.0.1) premailer (~> 1.11, >= 1.11.1) diff --git a/app/graphql/types/dataset_connection_with_meta_type.rb b/app/graphql/types/dataset_connection_with_meta_type.rb index 49bfa1995..b022f65be 100644 --- a/app/graphql/types/dataset_connection_with_meta_type.rb +++ b/app/graphql/types/dataset_connection_with_meta_type.rb @@ -8,7 +8,7 @@ class DatasetConnectionWithMetaType < BaseConnection field :dataset_connection_count, Integer, null: false, cache: true field :publication_connection_count, Integer, null: false, cache: true field :software_connection_count, Integer, null: false, cache: true - field :researcher_connection_count, Integer, null: false, cache: true + field :person_connection_count, Integer, null: false, cache: true field :funder_connection_count, Integer, null: false, cache: true field :organization_connection_count, Integer, null: false, cache: true @@ -30,7 +30,7 @@ def software_connection_count Event.query(nil, citation_type: "Dataset-SoftwareSourceCode").results.total end - def researcher_connection_count + def person_connection_count Event.query(nil, citation_type: "Dataset-Person").results.total end diff --git a/app/graphql/types/doi_item.rb b/app/graphql/types/doi_item.rb index 00b0e228f..246164894 100644 --- a/app/graphql/types/doi_item.rb +++ b/app/graphql/types/doi_item.rb @@ -6,7 +6,7 @@ module DoiItem description "Information about DOIs" field :id, ID, null: false, hash_key: "identifier", description: "The persistent identifier for the resource" - field :creators, [ResearcherType], null: true, description: "The main researchers involved in producing the data, or the authors of the publication, in priority order" do + field :creators, [PersonType], null: true, description: "The main researchers involved in producing the data, or the authors of the publication, in priority order" do argument :first, Int, required: false, default_value: 20 end field :titles, [TitleType], null: true, description: "A name or title by which a resource is known" do diff --git a/app/graphql/types/organization_connection_with_meta_type.rb b/app/graphql/types/organization_connection_with_meta_type.rb index 750264043..b3dbb6986 100644 --- a/app/graphql/types/organization_connection_with_meta_type.rb +++ b/app/graphql/types/organization_connection_with_meta_type.rb @@ -5,7 +5,7 @@ class OrganizationConnectionWithMetaType < BaseConnection field_class GraphQL::Cache::Field field :total_count, Integer, null: false, cache: true - field :researcher_connection_count, Integer, null: false, cache: true + field :person_connection_count, Integer, null: false, cache: true def total_count args = object.arguments @@ -13,7 +13,7 @@ def total_count Organization.query(args[:query], limit: 0).dig(:meta, "total").to_i end - def researcher_connection_count + def person_connection_count Event.query(nil, citation_type: "Organization-Person").results.total end end diff --git a/app/graphql/types/organization_researcher_connection_with_meta_type.rb b/app/graphql/types/organization_researcher_connection_with_meta_type.rb index e82f12616..dedbd59e4 100644 --- a/app/graphql/types/organization_researcher_connection_with_meta_type.rb +++ b/app/graphql/types/organization_researcher_connection_with_meta_type.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class OrganizationResearcherConnectionWithMetaType < BaseConnection +class OrganizationPersonConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) field_class GraphQL::Cache::Field diff --git a/app/graphql/types/organization_type.rb b/app/graphql/types/organization_type.rb index a427b6002..f95f5f803 100644 --- a/app/graphql/types/organization_type.rb +++ b/app/graphql/types/organization_type.rb @@ -23,9 +23,9 @@ class OrganizationType < BaseObject argument :first, Int, required: false, default_value: 25 end - field :researchers, OrganizationResearcherConnectionWithMetaType, null: false, description: "Researchers associated with this organization", connection: true, max_page_size: 1000 do - argument :first, Int, required: false, default_value: 25 - end + # field :researchers, OrganizationResearcherConnectionWithMetaType, null: false, description: "Researchers associated with this organization", connection: true, max_page_size: 1000 do + # argument :first, Int, required: false, default_value: 25 + # end def alternate_name object.aliases + object.acronyms @@ -65,10 +65,10 @@ def softwares(**args) ElasticsearchLoader.for(Doi).load_many(ids) end - def researchers(**args) - ids = Event.query(nil, obj_id: object.id, citation_type: "Organization-Person").results.to_a.map do |e| - orcid_from_url(e.subj_id) - end - ElasticsearchLoader.for(Researcher).load_many(ids) - end + # def researchers(**args) + # ids = Event.query(nil, obj_id: object.id, citation_type: "Organization-Person").results.to_a.map do |e| + # orcid_from_url(e.subj_id) + # end + # ElasticsearchLoader.for(Researcher).load_many(ids) + # end end diff --git a/app/graphql/types/person_connection_with_meta_type.rb b/app/graphql/types/person_connection_with_meta_type.rb new file mode 100644 index 000000000..c6ad02fe6 --- /dev/null +++ b/app/graphql/types/person_connection_with_meta_type.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class PersonConnectionWithMetaType < BaseConnection + edge_type(PersonEdgeType) + field_class GraphQL::Cache::Field + + field :publication_connection_count, Integer, null: false, cache: true + field :dataset_connection_count, Integer, null: false, cache: true + field :software_connection_count, Integer, null: false, cache: true + field :organization_connection_count, Integer, null: false, cache: true + + def publication_connection_count + Event.query(nil, citation_type: "Person-ScholarlyArticle").results.total + end + + def dataset_connection_count + Event.query(nil, citation_type: "Dataset-Person").results.total + end + + def software_connection_count + Event.query(nil, citation_type: "Person-SoftwareSourceCode").results.total + end + + def organization_connection_count + Event.query(nil, citation_type: "Organization-Person").results.total + end +end \ No newline at end of file diff --git a/app/graphql/types/person_edge_type.rb b/app/graphql/types/person_edge_type.rb new file mode 100644 index 000000000..33e884400 --- /dev/null +++ b/app/graphql/types/person_edge_type.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class PersonEdgeType < GraphQL::Types::Relay::BaseEdge + node_type(PersonType) +end \ No newline at end of file diff --git a/app/graphql/types/person_type.rb b/app/graphql/types/person_type.rb new file mode 100644 index 000000000..adad2b935 --- /dev/null +++ b/app/graphql/types/person_type.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +class PersonType < BaseObject + description "A person." + + field :id, ID, null: true, description: "The ORCID ID of the person." + field :name, String, null: true, description: "The name of the person." + field :given_name, String, null: true, hash_key: "given_names", description: "Given name. In the U.S., the first name of a Person." + field :family_name, String, null: true, description: "Family name. In the U.S., the last name of an Person." + + field :datasets, PersonDatasetConnectionWithMetaType, null: true, description: "Authored datasets", connection: true, max_page_size: 100 do + argument :first, Int, required: false, default_value: 25 + end + + field :publications, PersonPublicationConnectionWithMetaType, null: true, description: "Authored publications", connection: true, max_page_size: 100 do + argument :first, Int, required: false, default_value: 25 + end + + field :softwares, PersonSoftwareConnectionWithMetaType, null: true, description: "Authored software", connection: true, max_page_size: 100 do + argument :first, Int, required: false, default_value: 25 + end + + def id + object.uid ? "https://orcid.org/#{object.uid}" : object.id + end + + def name + object.name + end + + def datasets(**args) + ids = Event.query(nil, obj_id: https_to_http(object.uid || 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.uid || 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.uid || 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) + end + + def https_to_http(url) + orcid = orcid_from_url(url) + return nil unless orcid.present? + + "https://orcid.org/#{orcid}" + end +end diff --git a/app/graphql/types/publication_connection_with_meta_type.rb b/app/graphql/types/publication_connection_with_meta_type.rb index b999963eb..6632d8f00 100644 --- a/app/graphql/types/publication_connection_with_meta_type.rb +++ b/app/graphql/types/publication_connection_with_meta_type.rb @@ -8,7 +8,7 @@ class PublicationConnectionWithMetaType < BaseConnection field :publication_connection_count, Integer, null: false, cache: true field :dataset_connection_count, Integer, null: false, cache: true field :software_connection_count, Integer, null: false, cache: true - field :researcher_connection_count, Integer, null: false, cache: true + field :person_connection_count, Integer, null: false, cache: true field :funder_connection_count, Integer, null: false, cache: true field :organization_connection_count, Integer, null: false, cache: true @@ -30,7 +30,7 @@ def software_connection_count Event.query(nil, citation_type: "ScholarlyArticle-SoftwareSourceCode").results.total end - def researcher_connection_count + def person_connection_count Event.query(nil, citation_type: "Person-ScholarlyArticle").results.total end diff --git a/app/graphql/types/researcher_type.rb b/app/graphql/types/researcher_type.rb deleted file mode 100644 index 4291dafbc..000000000 --- a/app/graphql/types/researcher_type.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -class ResearcherType < BaseObject - description "Information about researchers" - - field :id, ID, null: true, description: "ORCID ID" - field :name, String, null: true, description: "Researcher name" - field :name_type, String, null: true, hash_key: "nameType", description: "The type of name" - field :given_names, String, null: true, description: "User given names" - field :family_name, String, null: true, description: "Researcher family name" - - def id - object.uid ? "https://orcid.org/#{object.uid}" : object.id - end - - def name - object.name - end -end diff --git a/app/graphql/types/software_connection_with_meta_type.rb b/app/graphql/types/software_connection_with_meta_type.rb index b7625e51f..5f48b694b 100644 --- a/app/graphql/types/software_connection_with_meta_type.rb +++ b/app/graphql/types/software_connection_with_meta_type.rb @@ -8,7 +8,7 @@ class SoftwareConnectionWithMetaType < BaseConnection field :software_connection_count, Integer, null: false, cache: true field :publication_connection_count, Integer, null: false, cache: true field :dataset_connection_count, Integer, null: false, cache: true - field :researcher_connection_count, Integer, null: false, cache: true + field :person_connection_count, Integer, null: false, cache: true field :funder_connection_count, Integer, null: false, cache: true field :organization_connection_count, Integer, null: false, cache: true @@ -30,7 +30,7 @@ def dataset_connection_count Event.query(nil, citation_type: "Dataset-SoftwareSourceCode").results.total end - def researcher_connection_count + def person_connection_count Event.query(nil, citation_type: "Person-SoftwareSourceCode").results.total end