Skip to content

Commit

Permalink
graphql connections for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 12, 2019
1 parent 4f6d5d6 commit ef001a7
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 15 deletions.
11 changes: 11 additions & 0 deletions app/graphql/types/client_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ClientConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(ClientEdgeType)

field :total_count, Integer, null: false

def total_count
object.nodes.size
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/client_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ClientEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(ClientType)
end
3 changes: 3 additions & 0 deletions app/graphql/types/client_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ class ClientType < GraphQL::Schema::Object

field :id, ID, null: false, hash_key: "uid", description: "Unique identifier for each client"
field :name, String, null: false, description: "Client name"
field :re3data, String, null: true, description: "The re3data identifier for the client"
field :description, String, null: true, description: "Description of the client"
field :url, String, null: true, description: "The homepage of the client"
field :contact_name, String, null: true, description: "Client contact name"
field :contact_email, String, null: true, description: "Client contact email"
field :software, String, null: true, description: "The name of the software that is used to run the repository"
field :prefixes, PrefixConnectionWithMetaType, 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
Expand Down
11 changes: 11 additions & 0 deletions app/graphql/types/organization_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class OrganizationConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(OrganizationEdgeType)

field :total_count, Integer, null: false

def total_count
object.nodes.size
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/organization_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class OrganizationEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(OrganizationType)
end
11 changes: 11 additions & 0 deletions app/graphql/types/provider_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ProviderConnectionWithMetaType < GraphQL::Types::Relay::BaseConnection
edge_type(ProviderEdgeType)

field :total_count, Integer, null: false

def total_count
object.nodes.size
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/provider_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ProviderEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(ProviderType)
end
7 changes: 5 additions & 2 deletions app/graphql/types/provider_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class ProviderType < BaseObject
argument :first, Int, required: false, default_value: 25
end

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

def prefixes(**args)
Expand All @@ -37,6 +40,6 @@ def prefixes(**args)
end

def clients(**args)
Client.query(args[:query], provider_id: object.uid, page: { cursor: 1, size: args[:first] }).records
Client.query(args[:query], provider_id: object.uid, year: args[:year], software: args[:software], page: { number: 1, size: 500 }).results.to_a
end
end
24 changes: 12 additions & 12 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

class QueryType < BaseObject
field :providers, [ProviderType], null: false do
field :providers, ProviderConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def providers(query: nil, first: nil)
Provider.query(query, page: { number: 1, size: first }).records
def providers(query: nil)
Provider.query(query, page: { number: 1, size: 250 }).records.to_a
end

field :provider, ProviderType, null: false do
Expand All @@ -18,13 +17,14 @@ def provider(id:)
Provider.unscoped.where("allocator.role_name IN ('ROLE_ALLOCATOR', 'ROLE_ADMIN')").where(deleted_at: nil).where(symbol: id).first
end

field :clients, [ClientType], null: false do
field :clients, ClientConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
argument :year, String, required: false
argument :software, String, required: false
end

def clients(query: nil, first: nil)
Client.query(query, page: { number: 1, size: first }).records
def clients(query: nil, year: nil, software: nil)
Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).records.to_a
end

field :client, ClientType, null: false do
Expand Down Expand Up @@ -58,21 +58,21 @@ def prefix(id:)
Prefix.where(prefix: id).first
end

field :funders, [FunderType], null: false do
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)
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).first
result = Funder.find_by_id(id)[:data].first
fail ActiveRecord::RecordNotFound if result.nil?

result
Expand All @@ -89,7 +89,7 @@ def researcher(id:)
result
end

field :organizations, [OrganizationType], null: false do
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
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ def media_ids

def self.find_by_ids(ids, options={})
dois = ids.split(",").map(&:upcase)

__elasticsearch__.search({
from: 0,
size: 1000,
Expand Down

0 comments on commit ef001a7

Please sign in to comment.