Skip to content

Commit

Permalink
more specific text types in graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 12, 2020
1 parent fa39b92 commit 3ba2d44
Show file tree
Hide file tree
Showing 55 changed files with 7,001 additions and 2,421 deletions.
7,445 changes: 5,178 additions & 2,267 deletions app/graphql/schema.graphql

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions app/graphql/types/audiovisual_connection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class AudiovisualConnectionType < BaseConnection

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :registration_agencies, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)
Expand All @@ -20,6 +23,27 @@ def years
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
Expand Down
26 changes: 26 additions & 0 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,30 @@ def facet_by_software(arr)
"count" => hsh["doc_count"] }
end
end

def facet_by_affiliation(arr)
# generate hash with id and name for each affiliation in facet
return [] if arr.blank?

ids = arr.map { |hsh| "\"#{hsh["key"]}\"" }.join(" ")
affiliations = Organization.query(ids, size: 1000)[:data] || []

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => affiliations.find { |a| a["id"] == hsh["key"] }.to_h["name"] || hsh["key"],
"count" => hsh["doc_count"] }
end
end

def facet_by_client(arr)
# generate hash with id and name for each client in facet
ids = arr.map { |hsh| hsh["key"] }.join(",")
clients = Client.find_by_id(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end
end
69 changes: 69 additions & 0 deletions app/graphql/types/book_chapter_connection_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

class BookChapterConnectionType < BaseConnection
edge_type(BookChapterEdgeType)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :registration_agencies, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)

response(args).results.total
end

def years
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
user_id: args[:user_id],
client_id: args[:repository_id],
provider_id: args[:member_id],
funder_id: args[:funder_id],
affiliation_id: args[:affiliation_id],
re3data_id: args[:re3data_id],
year: args[:year],
resource_type_id: "Text",
resource_type: "BookChapter",
has_person: args[:has_person],
has_funder: args[:has_funder],
has_organization: args[:has_organization],
has_citations: args[:has_citations],
has_parts: args[:has_parts],
has_versions: args[:has_versions],
has_views: args[:has_views],
has_downloads: args[:has_downloads],
page: { number: 1, size: 0 })
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/book_chapter_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

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

class BookChapterType < BaseObject
implements DoiItem

def type
"BookChapter"
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

class ThesisConnectionType < BaseConnection
edge_type(ThesisEdgeType)
class BookConnectionType < BaseConnection
edge_type(BookEdgeType)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :registration_agencies, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)
Expand All @@ -20,6 +23,27 @@ def years
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
Expand All @@ -31,7 +55,7 @@ def response(**args)
re3data_id: args[:re3data_id],
year: args[:year],
resource_type_id: "Text",
resource_type: "Thesis",
resource_type: "Book",
has_person: args[:has_person],
has_funder: args[:has_funder],
has_organization: args[:has_organization],
Expand Down
5 changes: 5 additions & 0 deletions app/graphql/types/book_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

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

class BookType < BaseObject
implements DoiItem

def type
"Book"
end
end
23 changes: 23 additions & 0 deletions app/graphql/types/collection_connection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class CollectionConnectionType < BaseConnection

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)
Expand All @@ -20,6 +22,27 @@ def years
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
Expand Down
69 changes: 69 additions & 0 deletions app/graphql/types/conference_paper_connection_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

class ConferencePaperConnectionType < BaseConnection
edge_type(ConferencePaperEdgeType)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :registration_agencies, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)

response(args).results.total
end

def years
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
user_id: args[:user_id],
client_id: args[:repository_id],
provider_id: args[:member_id],
funder_id: args[:funder_id],
affiliation_id: args[:affiliation_id],
re3data_id: args[:re3data_id],
year: args[:year],
resource_type_id: "Text",
resource_type: "\"Conference paper\"",
has_person: args[:has_person],
has_funder: args[:has_funder],
has_organization: args[:has_organization],
has_citations: args[:has_citations],
has_parts: args[:has_parts],
has_versions: args[:has_versions],
has_views: args[:has_views],
has_downloads: args[:has_downloads],
page: { number: 1, size: 0 })
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/conference_paper_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

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

class ConferencePaperType < BaseObject
implements DoiItem

def type
"ConferencePaper"
end
end
24 changes: 24 additions & 0 deletions app/graphql/types/data_paper_connection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class DataPaperConnectionType < BaseConnection

field :total_count, Integer, null: false, cache: true
field :years, [FacetType], null: true, cache: true
field :registration_agencies, [FacetType], null: true, cache: true
field :repositories, [FacetType], null: true, cache: true
field :affiliations, [FacetType], null: true, cache: true

def total_count
args = prepare_args(object.arguments)
Expand All @@ -20,6 +23,27 @@ def years
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : []
end

def registration_agencies
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.registration_agencies.buckets) : []
end

def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
end

def affiliations
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_affiliation(res.response.aggregations.affiliations.buckets) : []
end

def response(**args)
Doi.query(args[:query],
ids: args[:ids],
Expand Down
Loading

0 comments on commit 3ba2d44

Please sign in to comment.