Skip to content

Commit

Permalink
grapühql connections for prefixes. #269
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 11, 2019
1 parent 5475bd6 commit aa75fac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/graphql/types/client_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class ClientType < GraphQL::Schema::Object
field :description, String, null: true, description: "Description of the client"
field :contact_name, String, null: true, description: "Client contact name"
field :contact_email, String, null: true, description: "Client contact email"
field :prefixes, [PrefixType], null: false, description: "Prefixes managed by the client" do
field :prefixes, PrefixConnectionWithTotalCountType, null: false, description: "Prefixes managed by the client", connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :year, String, required: false
argument :first, Int, required: false, default_value: 25
end

Expand All @@ -26,7 +27,8 @@ class ClientType < GraphQL::Schema::Object
def prefixes(**args)
collection = object.prefixes
collection = collection.query(args[:query]) if args[:query].present?
collection.page(1).per(args[:first])
collection = collection.where('YEAR(prefix.created) = ?', args[:year]) if args[:year].present?
collection
end

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

class FacetType < BaseObject
description "Summary information"

field :id, String, null: true, description: "ID"
field :title, String, null: true, description: "Title"
field :count, Int, null: true, description: "Count"
end
26 changes: 26 additions & 0 deletions app/graphql/types/prefix_connection_with_total_count_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class PrefixConnectionWithTotalCountType < GraphQL::Types::Relay::BaseConnection
edge_type(PrefixEdgeType)

field :total_count, Integer, null: false
field :years, [FacetType], null: false

def total_count
object.nodes.size
end

def years
if object.parent.class.name == "Provider"
collection = object.parent.provider_prefixes.joins(:prefix)
years = collection.where.not(prefixes: nil).order("YEAR(allocator_prefixes.created_at) DESC").group("YEAR(allocator_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
elsif object.parent.class.name == "Client"
collection = object.parent.client_prefixes.joins(:prefix)
years = collection.where.not(prefixes: nil).order("YEAR(datacentre_prefixes.created_at) DESC").group("YEAR(datacentre_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
else
{}
end
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/prefix_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class PrefixEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(PrefixType)
end
12 changes: 8 additions & 4 deletions app/graphql/types/provider_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@ class ProviderType < BaseObject
field :organization_type, String, null: true, description: "Type of organization"
field :focus_area, String, null: true, description: "Field of science covered by member"
field :joined, String, null: true, description: "Date provider joined DataCite"
field :prefixes, [PrefixType], null: false do
field :prefixes, PrefixConnectionWithTotalCountType, null: false, description: "Prefixes managed by the provider", connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :state, String, required: false
argument :year, String, required: false
argument :first, Int, required: false, default_value: 25
end

field :clients, [ClientType], null: false, description: "Clients associated with the provider" do
field :clients, [ClientType], null: false, description: "Clients associated with the provider", max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def prefixes(**args)
collection = object.prefixes
collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?
collection = collection.query(args[:query]) if args[:query].present?
collection.page(1).per(args[:first])
collection = collection.where('YEAR(prefix.created) = ?', args[:year]) if args[:year].present?
collection
end

def clients(**args)
Client.query(args[:query], provider_id: object.uid, page: { number: 1, size: args[:first] }).records
Client.query(args[:query], provider_id: object.uid, page: { cursor: 1, size: args[:first] }).records
end
end
4 changes: 4 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def query(query, options={})
from = 0
search_after = [options.dig(:page, :cursor)]
sort = [{ created: { order: 'asc' }}]
elsif options.dig(:page, :cursor).present?
from = 0
search_after = [options.dig(:page, :cursor)]
sort = [{ created: { order: 'asc' }}]
else
from = (options.dig(:page, :number) - 1) * options.dig(:page, :size)
search_after = nil
Expand Down

0 comments on commit aa75fac

Please sign in to comment.