Skip to content

Commit

Permalink
graphql for re3data. #292
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 16, 2019
1 parent 0902acb commit 713c0a0
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def index
registered: params[:registered],
provider_id: params[:provider_id],
client_id: params[:client_id],
repository_id: params[:repository_id],
prefix: params[:prefix],
person_id: params[:person_id],
resource_type_id: params[:resource_type_id],
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/api_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class ApiType < BaseObject
description "Information"

field :url, String, null: false, description: "URL"
field :type, String, null: true, description: "Type"
end
7 changes: 7 additions & 0 deletions app/graphql/types/name_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class NameType < BaseObject
description "Information"

field :name, String, null: false, description: "Information"
end
20 changes: 20 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ def organization(id:)
result
end

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

def repositories(query: nil, first: nil)
Repository.query(query, limit: first).fetch(:data, [])
end

field :repository, RepositoryType, null: false do
argument :id, ID, required: true
end

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

result
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
14 changes: 14 additions & 0 deletions app/graphql/types/repository_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class RepositoryConnectionWithMetaType < BaseConnection
edge_type(RepositoryEdgeType)
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments

Repository.query(args[:query], limit: 0).dig(:meta, "total").to_i
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/repository_dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class RepositoryDatasetConnectionWithMetaType < BaseConnection
edge_type(DatasetEdgeType)
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments

Doi.query(args[:query], repository_id: doi_from_url(object.parent[:id]), resource_type_id: "Dataset", state: "findable", page: { number: 1, size: args[:first] }).results.total
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/repository_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class RepositoryEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(RepositoryType)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class RepositoryPublicationConnectionWithMetaType < BaseConnection
edge_type(DatasetEdgeType)
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments

Doi.query(args[:query], repository_id: doi_from_url(object.parent[:id]), resource_type_id: "Text", state: "findable", page: { number: 1, size: args[:first] }).results.total
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/repository_software_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class RepositorySoftwareConnectionWithMetaType < BaseConnection
edge_type(DatasetEdgeType)
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments

Doi.query(args[:query], repository_id: doi_from_url(object.parent[:id]), resource_type_id: "Software", state: "findable", page: { number: 1, size: args[:first] }).results.total
end
end
52 changes: 52 additions & 0 deletions app/graphql/types/repository_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

class RepositoryType < BaseObject
description "Information about repository"

field :id, ID, null: false, description: "Repository ID"
field :re3data_id, String, null: false, description: "re3data ID"
field :name, String, null: false, description: "Repository name"
field :additional_names, [TextLanguageType], null: false, description: "Additional repository names"
field :url, String, null: true, description: "Repository URL"
field :contacts, [TextType], null: true, description: "Repository contact information"
field :description, String, null: true, description: "Repository description"
field :certificates, [TextType], null: true, description: "Repository certificates"
field :subjects, [SchemeType], null: true, description: "Subjects"
field :content_types, [SchemeType], null: true, description: "Content types"
field :provider_types, [TextType], null: true, description: "Provider types"
field :keywords, [TextType], null: true, description: "Keywords"
field :data_accesses, [TextRestrictionType], null: true, description: "Data accesses"
field :data_uploads, [TextRestrictionType], null: true, description: "Data uploads"
field :pid_systems, [TextType], null: true, description: "PID Systems"
field :apis, [ApiType], null: true, description: "APIs"
field :software, [NameType], null: true, description: "Software"

field :datasets, RepositoryDatasetConnectionWithMetaType, 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
end

field :publications, RepositoryPublicationConnectionWithMetaType, null: false, connection: true, max_page_size: 100, description: "Publications hosted by the repository" do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

field :softwares, RepositorySoftwareConnectionWithMetaType, null: false, connection: true, max_page_size: 100, description: "Software hosted by the repository" do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def datasets(**args)
logger = Logger.new(STDOUT)
logger.info doi_from_url(object[:id])
Doi.query(args[:query], repository_id: doi_from_url(object[:id]), resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
end

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

def softwares(**args)
Doi.query(args[:query], repository_id: doi_from_url(object[:id]), resource_type_id: "Software", page: { number: 1, size: args[:first] }).results.to_a
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/scheme_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class SchemeType < BaseObject
description "Information"

field :scheme, String, null: false, description: "Schema"
field :text, String, null: false, description: "Information"
end
8 changes: 8 additions & 0 deletions app/graphql/types/text_language_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class TextLanguageType < BaseObject
description "Information"

field :language, String, null: true, description: "Language"
field :text, String, null: false, description: "Information"
end
8 changes: 8 additions & 0 deletions app/graphql/types/text_restriction_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class TextRestrictionType < BaseObject
description "Information"

field :text, String, null: false, description: "Information"
field :restriction, [TextType], null: true, description: "Restriction"
end
7 changes: 7 additions & 0 deletions app/graphql/types/text_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class TextType < BaseObject
description "Information"

field :text, String, null: false, description: "Information"
end
2 changes: 2 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def query(query, options={})
elsif self.name == "Doi"
must << { terms: { aasm_state: options[:state].to_s.split(",") }} if options[:state].present?
must << { range: { registered: { gte: "#{options[:registered].split(",").min}||/y", lte: "#{options[:registered].split(",").max}||/y", format: "yyyy" }}} if options[:registered].present?
must << { term: { "client.repository_id": options[:repository_id].upcase }} if options[:repository_id].present?
must_not << { terms: { provider_id: ["crossref"] }} if options[:exclude_registration_agencies]
elsif self.name == "Event"
must << { term: { subj_id: options[:subj_id] }} if options[:subj_id].present?
must << { term: { obj_id: options[:obj_id] }} if options[:obj_id].present?
Expand Down
7 changes: 6 additions & 1 deletion app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ def self.parse_message(id: nil, message: nil)
subjects: message["subjects"],
content_types: message["contentTypes"],
provider_types: message["providerTypes"],
keywords: message["keywords"] }.compact
keywords: message["keywords"],
data_accesses: message["dataAccesses"],
data_uploads: message["dataUploads"],
pid_systems: message["pidSystems"],
apis: message["apis"],
software: message["software"] }.compact
end

def self.doi_from_url(url)
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/client_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ClientSerializer
set_id :uid
cache_options enabled: true, cache_length: 24.hours

attributes :name, :symbol, :year, :contact_name, :contact_email, :description, :domains, :url, :created, :updated
attributes :name, :symbol, :re3data, :year, :contact_name, :contact_email, :description, :domains, :url, :created, :updated

belongs_to :provider, record_type: :providers
belongs_to :repository, record_type: :repositories, if: Proc.new { |client| client.repository_id }
Expand Down

0 comments on commit 713c0a0

Please sign in to comment.