Skip to content

Commit

Permalink
bring person, data_catalog and funder into lupo graphql api
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Oct 11, 2019
1 parent cd0abbf commit 228b22a
Show file tree
Hide file tree
Showing 32 changed files with 1,299 additions and 55 deletions.
13 changes: 13 additions & 0 deletions app/graphql/types/data_catalog_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class DataCatalogConnectionWithMetaType < BaseConnection
edge_type(DataCatalogEdgeType)
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments
DataCatalog.query(args[:query], limit: 0).dig(:meta, "total").to_i
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/data_catalog_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class DataCatalogEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(DataCatalogType)
end
67 changes: 61 additions & 6 deletions app/graphql/types/data_catalog_type.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# frozen_string_literal: true

class DataCatalogType < BaseObject
extend_type

key fields: 'id'

field :id, ID, null: false, description: "Re3data identifier.", external: true
description "A collection of datasets."

field :id, ID, null: false, description: "The ID of the data catalog."
field :type, String, null: false, description: "The type of the item."
field :identifier, [IdentifierType], null: true, description: "re3data ID"
field :name, String, null: true, description: "The name of the data catalog."
field :alternate_name, [String], null: true, description: "An alias for the data catalog."
field :url, String, null: true, hash_key: "repositoryUrl", description: "URL of the data catalog."
# field :contacts, [String], null: true, description: "Repository contact information"
field :description, String, null: true, description: "A description of the data catalog."
field :certificates, [DefinedTermType], null: true, description: "Certificates of the data catalog."
field :subjects, [DefinedTermType], null: true, description: "Subject areas covered by the data catalog."
# field :types, [String], null: true, description: "Repository types"
# field :content_types, [SchemeType], null: true, description: "Content types"
# field :provider_types, [String], null: true, description: "Provider types"
field :in_language, [String], null: true, description: "The language of the content of the data catalog."
field :keywords, String, null: true, description: "Keywords or tags used to describe this data catalog. Multiple entries in a keywords list are typically delimited by commas."
# field :data_accesses, [TextRestrictionType], null: true, description: "Data accesses"
# field :data_uploads, [TextRestrictionType], null: true, description: "Data uploads"
# field :pid_systems, [String], null: true, description: "PID Systems"
# field :apis, [ApiType], null: true, description: "APIs"
field :software_application, [SoftwareApplicationType], null: true, description: "Software"
field :datasets, DataCatalogDatasetConnectionWithMetaType, null: false, connection: true, max_page_size: 100, description: "Datasets hosted by the repository" do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
Expand All @@ -22,8 +38,47 @@ class DataCatalogType < BaseObject
argument :first, Int, required: false, default_value: 25
end

def type
"DataCatalog"
end

def identifier
Array.wrap(object.re3data_id).map { |o| { "name" => "re3data", "value" => "r3d#{o}" } }
end

def alternate_name
Array.wrap(object["additional_names"]).map { |n| n["text"] }
end

def keywords
Array.wrap(object.keywords).map { |k| k["text"] }.join(", ")
end

def in_language
Array.wrap(object.repository_languages).map { |k| k["text"] }
end

def certificates
Array.wrap(object.certificates).map { |s| { "name" => s["text"] } }
end

def subjects
Array.wrap(object.subjects).map do |s|
term_code, name = s["text"].split(" ", 2)

{
"term_code" => term_code,
"name" => name,
"in_defined_term_set" => "DFG" }
end
end

def software_application
Array.wrap(object.software).map { |s| { "name" => s["name"] } }
end

def datasets(**args)
Doi.query(args[:query], re3data_id: doi_from_url(object[:id]), resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
Doi.query(args[:query], re3data_id: doi_from_url(object.id), resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
end

def publications(**args)
Expand Down
10 changes: 10 additions & 0 deletions app/graphql/types/defined_term_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class DefinedTermType < BaseObject
description "A word, name, acronym, phrase, etc. with a formal definition. Often used in the context of category or subject classification, glossaries or dictionaries, product or creative work types, etc."

field :term_code, String, null: true, description: "A code that identifies this DefinedTerm within a DefinedTermSet."
field :name, String, null: true, description: "The name of the item."
field :description, String, null: true, description: "A description of the item."
field :in_defined_term_set, String, null: true, description: "A DefinedTermSet that contains this term."
end
39 changes: 23 additions & 16 deletions app/graphql/types/funder_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
# frozen_string_literal: true

# class FunderConnectionWithMetaType < BaseConnection
class FunderConnectionWithMetaType < BaseConnection

# edge_type(FunderEdgeType)
# field_class GraphQL::Cache::Field
edge_type(FunderEdgeType)
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 :total_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 :software_connection_count, Integer, null: false, cache: true

# def publication_connection_count
# Event.query(nil, citation_type: "Funder-ScholarlyArticle").results.total
# end
def total_count
args = object.arguments

# def dataset_connection_count
# Event.query(nil, citation_type: "Dataset-Funder").results.total
# end
Funder.query(args[:query], limit: 0).dig(:meta, "total").to_i
end

def publication_connection_count
Event.query(nil, citation_type: "Funder-ScholarlyArticle").results.total
end

def dataset_connection_count
Event.query(nil, citation_type: "Dataset-Funder").results.total
end

# def software_connection_count
# Event.query(nil, citation_type: "Funder-SoftwareSourceCode").results.total
# end
# end
def software_connection_count
Event.query(nil, citation_type: "Funder-SoftwareSourceCode").results.total
end
end
21 changes: 16 additions & 5 deletions app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class FunderType < BaseObject
extend_type
# key fields: 'id'

# field :id, ID, null: false, description: "Crossref Funder ID", external: true

description "Information about funders"

field :id, ID, null: false, description: "Crossref Funder ID"
field :type, String, null: false, description: "The type of the item."
field :name, String, null: false, description: "The name of the funder."
field :alternate_name, [String], null: true, description: "An alias for the funder."
field :address, AddressType, null: true, description: "Physical address of the funder."
field :datasets, FunderDatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end
Expand All @@ -19,6 +21,15 @@ class FunderType < BaseObject
argument :first, Int, required: false, default_value: 25
end

def type
"Funder"
end

def address
{ "type" => "postalAddress",
"address_country" => object.country.to_h.fetch("name", nil) }
end

def datasets(**args)
ids = Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Funder").results.to_a.map do |e|
doi_from_url(e.subj_id)
Expand Down
47 changes: 27 additions & 20 deletions app/graphql/types/person_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
# frozen_string_literal: true

# class PersonConnectionWithMetaType < BaseConnection
# edge_type(PersonEdgeType)
# field_class GraphQL::Cache::Field
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
field :total_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 :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 total_count
args = object.arguments

# def dataset_connection_count
# Event.query(nil, citation_type: "Dataset-Person").results.total
# end
Person.query(args[:query], limit: 0).dig(:meta, "total").to_i
end

# def software_connection_count
# Event.query(nil, citation_type: "Person-SoftwareSourceCode").results.total
# end
def publication_connection_count
Event.query(nil, citation_type: "Person-ScholarlyArticle").results.total
end

# def organization_connection_count
# Event.query(nil, citation_type: "Organization-Person").results.total
# end
# 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
4 changes: 2 additions & 2 deletions app/graphql/types/person_dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class PersonDatasetConnectionWithMetaType < BaseConnection
field :total_count, Integer, null: false, cache: true

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

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

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

def https_to_http(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PersonSoftwareConnectionWithMetaType < BaseConnection
field :total_count, Integer, null: false, cache: true

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

def https_to_http(url)
Expand Down
13 changes: 10 additions & 3 deletions app/graphql/types/person_type.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

class PersonType < BaseObject
extend_type
# key fields: "id"
description "A person."

# field :id, ID, null: false, external: true, description: "The ORCID ID of the person."
field :id, ID, null: true, description: "The ORCID ID of the person."
field :type, String, null: false, description: "The type of the item."
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
Expand All @@ -18,6 +21,10 @@ class PersonType < BaseObject
argument :first, Int, required: false, default_value: 25
end

def type
"Person"
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|
doi_from_url(e.subj_id)
Expand Down
60 changes: 60 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,46 @@ def prefix(id:)
Prefix.where(prefix: id).first
end

field :funders, FunderConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def funders(query: nil, first: nil)
Funder.query(query, limit: first).fetch(:data, [])
end

field :funder, FunderType, null: false do
argument :id, ID, required: true
end

def funder(id:)
result = Funder.find_by_id(id).fetch(:data, []).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :data_catalog, DataCatalogType, null: false do
argument :id, ID, required: true
end

def data_catalog(id:)
result = DataCatalog.find_by_id(id).fetch(:data, []).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :data_catalogs, DataCatalogConnectionWithMetaType, null: false, connection: true, max_page_size: 1000 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def data_catalogs(query: nil, first: nil)
DataCatalog.query(query, limit: first).fetch(:data, [])
end

field :organizations, OrganizationConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
Expand All @@ -83,6 +123,26 @@ def organization(id:)
result
end

field :person, PersonType, null: true do
argument :id, ID, required: true
end

def person(id:)
result = Person.find_by_id(id).fetch(:data, []).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :people, PersonConnectionWithMetaType, null: false, connection: true, max_page_size: 1000 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def people(query: nil, first: nil)
Person.query(query, limit: first).fetch(:data, [])
end

field :datasets, DatasetConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
Expand Down
10 changes: 10 additions & 0 deletions app/graphql/types/software_application_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class SoftwareApplicationType < BaseObject
description "A software application."

field :name, String, null: true, description: "The name of the item."
field :description, String, null: true, description: "A description of the item."
field :software_version, String, null: true, description: "Version of the software instance."
field :url, String, null: true, description: "URL of the item."
end
Loading

0 comments on commit 228b22a

Please sign in to comment.