Skip to content

Commit

Permalink
graphql for funder. #257
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 12, 2019
1 parent 60f302a commit e072fb0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
13 changes: 0 additions & 13 deletions app/graphql/types/dataset_connection_with_meta_type.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/graphql/types/funder_dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class FunderDatasetConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(EventEdgeType, edge_class: EventEdge)

field :total_count, Integer, null: false

def total_count
Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Funder").fetch(:meta, "total")
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/funder_publication_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class FunderPublicationConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(EventEdgeType, edge_class: EventEdge)

field :total_count, Integer, null: false

def total_count
Event.query(nil, obj_id: object[:id], citation_type: "Funder-JournalArticle").fetch(:meta, "total")
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/funder_software_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class FunderSoftwareConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(EventEdgeType, edge_class: EventEdge)

field :total_count, Integer, null: false

def total_count
Event.query(nil, obj_id: object[:id], citation_type: "Funder-SoftwareSourceCode").fetch(:meta, "total")
end
end
15 changes: 13 additions & 2 deletions app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ class FunderType < BaseObject
field :alternate_name, [String], null: true, description: "Alternate funder names"
field :country, String, null: true, description: "Country where funder is located"
field :date_modified, String, null: false, description: "Date information was last updated"
field :datasets, DatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do
field :datasets, FunderDatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

field :publications, [PublicationType], null: false, description: "Funded publications" do
field :publications, FunderPublicationConnectionWithMetaType, null: false, description: "Funded publications", connection: true do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

field :softwares, FunderSoftwareConnectionWithMetaType, null: false, description: "Funded software", 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-Funder").fetch(:data, []).map do |e|
doi_from_url(e[:subj_id])
Expand All @@ -31,6 +35,13 @@ def publications(**args)
Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a
end

def softwares(**args)
ids = Event.query(nil, obj_id: object[:id], citation_type: "Funder-SoftwareSourceCode").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)
Expand Down

0 comments on commit e072fb0

Please sign in to comment.