Skip to content

Commit

Permalink
graphql fine-tuning. #258
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 12, 2019
1 parent e072fb0 commit 2cb63d8
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 50 deletions.
13 changes: 13 additions & 0 deletions app/graphql/types/dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/graphql/types/dataset_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class DatasetEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(DatasetType)
end
21 changes: 21 additions & 0 deletions app/graphql/types/event_data_edge.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
23 changes: 0 additions & 23 deletions app/graphql/types/event_edge.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 13 additions & 0 deletions app/graphql/types/publication_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 18 additions & 18 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 12 additions & 0 deletions app/graphql/types/software_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2cb63d8

Please sign in to comment.