diff --git a/app/graphql/types/dataset_connection_with_meta_type.rb b/app/graphql/types/dataset_connection_with_meta_type.rb new file mode 100644 index 000000000..bd2cf02bf --- /dev/null +++ b/app/graphql/types/dataset_connection_with_meta_type.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class DatasetConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection + edge_type(DatasetEdgeType) + + field :total_count, Integer, null: false + + def total_count + args = object.arguments + + Doi.query(args[:query], resource_type_id: "Dataset", state: "findable", page: { number: 1, size: args[:first] }).results.total + end +end diff --git a/app/graphql/types/dataset_edge_type.rb b/app/graphql/types/dataset_edge_type.rb new file mode 100644 index 000000000..5c6684dd3 --- /dev/null +++ b/app/graphql/types/dataset_edge_type.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class DatasetEdgeType < GraphQL::Types::Relay::BaseEdge + node_type(DatasetType) +end diff --git a/app/graphql/types/event_data_edge.rb b/app/graphql/types/event_data_edge.rb new file mode 100644 index 000000000..6ad672d67 --- /dev/null +++ b/app/graphql/types/event_data_edge.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class EventDataEdge < GraphQL::Relay::Edge + def event_data + @event_data ||= begin + Event.query(nil, subj_id: self.node[:id], obj_id: self.parent[:id])[:data].first + end + end + + def source + event_data[:source_id].underscore.camelcase(:lower) + end + + def relation_type + event_data[:relation_type_id].underscore.camelcase(:lower) + end + + def total + event_data[:total] + end +end diff --git a/app/graphql/types/event_edge_type.rb b/app/graphql/types/event_data_edge_type.rb similarity index 77% rename from app/graphql/types/event_edge_type.rb rename to app/graphql/types/event_data_edge_type.rb index b682e8582..069017f70 100644 --- a/app/graphql/types/event_edge_type.rb +++ b/app/graphql/types/event_data_edge_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class EventEdgeType < GraphQL::Types::Relay::BaseEdge - node_type(DatasetType) +class EventDataEdgeType < GraphQL::Types::Relay::BaseEdge + node_type(DoiItem) field :source, String, null: false, description: "Source for this event" field :relation_type, String, null: false, description: "Relation type for this event" diff --git a/app/graphql/types/event_edge.rb b/app/graphql/types/event_edge.rb deleted file mode 100644 index 51628a600..000000000 --- a/app/graphql/types/event_edge.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -class EventEdge < GraphQL::Relay::Edge - def event - logger = Logger.new(STDOUT) - - @event ||= begin - Event.query(nil, subj_id: self.node[:id], obj_id: self.parent[:id])[:data].first - end - end - - def source - event[:source_id].underscore.camelcase(:lower) - end - - def relation_type - event[:relation_type_id].underscore.camelcase(:lower) - end - - def total - event[:total] - end -end diff --git a/app/graphql/types/funder_dataset_connection_with_meta_type.rb b/app/graphql/types/funder_dataset_connection_with_meta_type.rb index 923bc134b..fe85f3465 100644 --- a/app/graphql/types/funder_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/funder_dataset_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class FunderDatasetConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/funder_publication_connection_with_meta_type.rb b/app/graphql/types/funder_publication_connection_with_meta_type.rb index 563e5bbb0..184536192 100644 --- a/app/graphql/types/funder_publication_connection_with_meta_type.rb +++ b/app/graphql/types/funder_publication_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class FunderPublicationConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/funder_software_connection_with_meta_type.rb b/app/graphql/types/funder_software_connection_with_meta_type.rb index ea5d6399a..bdec53250 100644 --- a/app/graphql/types/funder_software_connection_with_meta_type.rb +++ b/app/graphql/types/funder_software_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class FunderSoftwareConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/publication_connection_with_meta_type.rb b/app/graphql/types/publication_connection_with_meta_type.rb new file mode 100644 index 000000000..aa7489d57 --- /dev/null +++ b/app/graphql/types/publication_connection_with_meta_type.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class PublicationConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection + edge_type(DatasetEdgeType) + + field :total_count, Integer, null: false + + def total_count + args = object.arguments + + Doi.query(args[:query], resource_type_id: "Text", state: "findable", page: { number: 1, size: args[:first] }).results.total + end +end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 946320df7..659a0d8cb 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -109,13 +109,13 @@ def organization(id:) result end - field :datasets, [DatasetType], null: false do + 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 end def datasets(query: nil, first: nil) - Doi.query(query, resource_type_id: "Dataset", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Dataset", state: "findable", page: { number: 1, size: first }).results.to_a end field :dataset, DatasetType, null: false do @@ -126,13 +126,13 @@ def dataset(id:) set_doi(id) end - field :publications, [PublicationType], null: false do + field :publications, PublicationConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do argument :query, String, required: false argument :first, Int, required: false, default_value: 25 end def publications(query: nil, first: nil) - Doi.query(query, resource_type_id: "Text", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Text", state: "findable", page: { number: 1, size: first }).results.to_a end field :publication, PublicationType, null: false do @@ -149,7 +149,7 @@ def publication(id:) end def audiovisuals(query: nil, first: nil) - Doi.query(query, resource_type_id: "Audiovisual", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Audiovisual", state: "findable", page: { number: 1, size: first }).results.to_a end field :audiovisual, AudiovisualType, null: false do @@ -166,7 +166,7 @@ def audiovisual(id:) end def collections(query: nil, first: nil) - Doi.query(query, resource_type_id: "Collection", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Collection", state: "findable", page: { number: 1, size: first }).results.to_a end field :collection, CollectionType, null: false do @@ -183,7 +183,7 @@ def collection(id:) end def data_papers(query: nil, first: nil) - Doi.query(query, resource_type_id: "DataPaper", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "DataPaper", state: "findable", page: { number: 1, size: first }).results.to_a end field :data_paper, DataPaperType, null: false do @@ -200,7 +200,7 @@ def data_paper(id:) end def events(query: nil, first: nil) - Doi.query(query, resource_type_id: "Event", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Event", state: "findable", page: { number: 1, size: first }).results.to_a end field :event, EventType, null: false do @@ -217,7 +217,7 @@ def event(id:) end def images(query: nil, first: nil) - Doi.query(query, resource_type_id: "Image", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Image", state: "findable", page: { number: 1, size: first }).results.to_a end field :image, ImageType, null: false do @@ -234,7 +234,7 @@ def image(id:) end def interactive_resources(query: nil, first: nil) - Doi.query(query, resource_type_id: "InteractiveResource", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "InteractiveResource", state: "findable", page: { number: 1, size: first }).results.to_a end field :interactive_resource, InteractiveResourceType, null: false do @@ -251,7 +251,7 @@ def interactive_resource(id:) end def models(query: nil, first: nil) - Doi.query(query, resource_type_id: "Model", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Model", state: "findable", page: { number: 1, size: first }).results.to_a end field :model, ModelType, null: false do @@ -268,7 +268,7 @@ def model(id:) end def physical_objects(query: nil, first: nil) - Doi.query(query, resource_type_id: "PhysicalObject", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "PhysicalObject", state: "findable", page: { number: 1, size: first }).results.to_a end field :physical_object, PhysicalObjectType, null: false do @@ -285,7 +285,7 @@ def physical_object(id:) end def services(query: nil, first: nil) - Doi.query(query, resource_type_id: "Service", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Service", state: "findable", page: { number: 1, size: first }).results.to_a end field :service, ServiceType, null: false do @@ -296,13 +296,13 @@ def service(id:) set_doi(id) end - field :softwares, [SoftwareType], null: false do + field :softwares, SoftwareConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do argument :query, String, required: false argument :first, Int, required: false, default_value: 25 end def softwares(query: nil, first: nil) - Doi.query(query, resource_type_id: "Software", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Software", state: "findable", page: { number: 1, size: first }).results.to_a end field :software, SoftwareType, null: false do @@ -319,7 +319,7 @@ def software(id:) end def sounds(query: nil, first: nil) - Doi.query(query, resource_type_id: "Sound", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Sound", state: "findable", page: { number: 1, size: first }).results.to_a end field :sound, SoundType, null: false do @@ -336,7 +336,7 @@ def sound(id:) end def workflows(query: nil, first: nil) - Doi.query(query, resource_type_id: "Workflow", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Workflow", state: "findable", page: { number: 1, size: first }).results.to_a end field :workflow, WorkflowType, null: false do @@ -353,7 +353,7 @@ def workflow(id:) end def others(query: nil, first: nil) - Doi.query(query, resource_type_id: "Other", state: "findable", page: { number: 1, size: first }) + Doi.query(query, resource_type_id: "Other", state: "findable", page: { number: 1, size: first }).results.to_a end field :other, OtherType, null: false do diff --git a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb index 6873f90b0..38f4b61ad 100644 --- a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ResearcherDatasetConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/researcher_publication_connection_with_meta_type.rb b/app/graphql/types/researcher_publication_connection_with_meta_type.rb index d53133d52..6f0b00008 100644 --- a/app/graphql/types/researcher_publication_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_publication_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ResearcherPublicationConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/researcher_software_connection_with_meta_type.rb b/app/graphql/types/researcher_software_connection_with_meta_type.rb index 59d163eea..6774fa55d 100644 --- a/app/graphql/types/researcher_software_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_software_connection_with_meta_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ResearcherSoftwareConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection - edge_type(EventEdgeType, edge_class: EventEdge) + edge_type(EventDataEdgeType, edge_class: EventDataEdge) field :total_count, Integer, null: false diff --git a/app/graphql/types/software_connection_with_meta_type.rb b/app/graphql/types/software_connection_with_meta_type.rb new file mode 100644 index 000000000..63e3dec04 --- /dev/null +++ b/app/graphql/types/software_connection_with_meta_type.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class SoftwareConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection + edge_type(DatasetEdgeType) + + field :total_count, Integer, null: false + + def total_count + args = object.arguments + Doi.query(args[:query], resource_type_id: "Software", state: "findable", page: { number: 1, size: args[:first] }).results.total + end +end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 527e8bd2c..89bd4fdc4 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -117,7 +117,7 @@ def query(query, options={}) search_after = [options.dig(:page, :cursor)] sort = [{ created: { order: 'asc' }}] else - from = (options.dig(:page, :number) - 1) * options.dig(:page, :size) + from = ((options.dig(:page, :number) || 1) - 1) * (options.dig(:page, :size) || 25) search_after = nil sort = options[:sort] end