Skip to content

Commit

Permalink
streamline args for graphql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 4, 2020
1 parent a356e11 commit c28023d
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 68 deletions.
367 changes: 363 additions & 4 deletions app/graphql/schema.graphql

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions app/graphql/types/client_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def total_count
def years
args = prepare_args(object.arguments)

res = response(**args)
res.results.total.positive? ? facet_by_year(res.response.aggregations.years.buckets) : nil
r = response(**args)
r.results.total.positive? ? facet_by_year(r.response.aggregations.years.buckets) : nil
end

def software
args = prepare_args(object.arguments)

res = response(**args)
res.results.total.positive? ? facet_by_software(res.response.aggregations.software.buckets) : nil
r = response(**args)
r.results.total.positive? ? facet_by_software(r.response.aggregations.software.buckets) : nil
end

def response(**args)
Expand Down
67 changes: 58 additions & 9 deletions app/graphql/types/client_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ class ClientType < BaseObject

field :datasets, DatasetConnectionWithMetaType, null: true, connection: true, description: "Datasets managed by the client" do
argument :query, String, required: false
argument :ids, String, required: false
argument :user_id, String, required: false
argument :provider_id, String, required: false
argument :funder_id, String, required: false
argument :affiliation_id, String, required: false
argument :resource_type_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -33,7 +41,15 @@ class ClientType < BaseObject

field :publications, PublicationConnectionWithMetaType, null: true, connection: true, description: "Publications managed by the client" do
argument :query, String, required: false
argument :ids, String, required: false
argument :user_id, String, required: false
argument :provider_id, String, required: false
argument :funder_id, String, required: false
argument :affiliation_id, String, required: false
argument :resource_type_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -42,7 +58,15 @@ class ClientType < BaseObject

field :softwares, SoftwareConnectionWithMetaType, null: true, connection: true, description: "Software managed by the client" do
argument :query, String, required: false
argument :ids, String, required: false
argument :user_id, String, required: false
argument :provider_id, String, required: false
argument :funder_id, String, required: false
argument :affiliation_id, String, required: false
argument :resource_type_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -51,7 +75,15 @@ class ClientType < BaseObject

field :works, WorkConnectionWithMetaType, null: true, connection: true, description: "Works managed by the client" do
argument :query, String, required: false
argument :ids, String, required: false
argument :user_id, String, required: false
argument :provider_id, String, required: false
argument :funder_id, String, required: false
argument :affiliation_id, String, required: false
argument :resource_type_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -70,38 +102,55 @@ def type
end

def datasets(**args)
Doi.query(args[:query], user_id: args[:user_id].present? ? orcid_from_url(args[:user_id]) : nil, client_id: object.uid, resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Dataset"
r = response(**args)

r.results.to_a
end

def publications(**args)
Doi.query(args[:query], user_id: args[:user_id].present? ? orcid_from_url(args[:user_id]) : nil, client_id: object.uid, resource_type_id: "Text", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Text"
r = response(**args)

r.results.to_a
end

def softwares(**args)
Doi.query(args[:query], user_id: args[:user_id].present? ? orcid_from_url(args[:user_id]) : nil, client_id: object.uid, resource_type_id: "Software", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Software"
r = response(**args)

r.results.to_a
end

def works(**args)
Doi.query(args[:query], user_id: args[:user_id].present? ? orcid_from_url(args[:user_id]) : nil, client_id: object.uid, page: { number: 1, size: args[:first] }).results.to_a
r = response(**args)

r.results.to_a
end

def prefixes(**args)
ClientPrefix.query(args[:query], client_id: object.uid, state: args[:state], year: args[:year], page: { number: 1, size: args[:first] }).results.to_a
end

def view_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.views.buckets) : []
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.views.buckets) : []
end

def download_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.downloads.buckets) : []
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.downloads.buckets) : []
end

def citation_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.citations.buckets) : []
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.citations.buckets) : []
end

def response
@response ||= Doi.query(nil, client_id: object.uid, state: "findable", page: { number: 1, size: 0 })
def response(**args)
Doi.query(args[:query], funder_id: args[:funder_id], user_id: args[:user_id], client_id: object.uid, provider_id: args[:provider_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], has_person: args[:has_person], has_organization: args[:has_organization], has_funder: args[:has_funder], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] })
end
end
12 changes: 11 additions & 1 deletion app/graphql/types/doi_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ module DoiItem
field :citations_over_time, [YearTotalType], null: true, description: "Citations by year."
field :views_over_time, [YearMonthTotalType], null: true, description: "Views by month."
field :downloads_over_time, [YearMonthTotalType], null: true, description: "Downloads by month."

field :references, WorkConnectionWithMetaType, null: true, connection: true, max_page_size: 100, description: "References for this DOI." do
argument :query, String, required: false
argument :ids, String, required: false
argument :user_id, String, required: false
argument :funder_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_organization, Boolean, required: false
Expand All @@ -67,6 +69,7 @@ module DoiItem
argument :funder_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_organization, Boolean, required: false
Expand All @@ -82,6 +85,7 @@ module DoiItem
argument :funder_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_organization, Boolean, required: false
Expand All @@ -97,6 +101,7 @@ module DoiItem
argument :funder_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_organization, Boolean, required: false
Expand All @@ -114,6 +119,7 @@ module DoiItem
argument :provider_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :affiliation_id, String, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
Expand All @@ -127,6 +133,10 @@ module DoiItem
argument :funder_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_funder, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand Down Expand Up @@ -219,7 +229,7 @@ def version_of(**args)
def response(**args)
return [] if args[:ids].blank?

@response ||= Doi.query(args[:query], ids: args[:ids], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] }).results.to_a
Doi.query(args[:query], ids: args[:ids], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] }).results.to_a
end

def doi_link(url)
Expand Down
45 changes: 36 additions & 9 deletions app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class FunderType < BaseObject
argument :user_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -30,6 +32,8 @@ class FunderType < BaseObject
argument :user_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -42,6 +46,8 @@ class FunderType < BaseObject
argument :user_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -54,6 +60,10 @@ class FunderType < BaseObject
argument :user_id, String, required: false
argument :client_id, String, required: false
argument :provider_id, String, required: false
argument :affiliation_id, String, required: false
argument :resource_type_id, String, required: false
argument :has_person, Boolean, required: false
argument :has_organization, Boolean, required: false
argument :has_citations, Int, required: false
argument :has_views, Int, required: false
argument :has_downloads, Int, required: false
Expand All @@ -66,34 +76,51 @@ def address
end

def publications(**args)
Doi.query(args[:query], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Text", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Text"
r = response(**args)

r.results.to_a
end

def datasets(**args)
Doi.query(args[:query], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Dataset", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Dataset"
r = response(**args)

r.results.to_a
end

def softwares(**args)
Doi.query(args[:query], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Software", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Software"
r = response(**args)

r.results.to_a
end

def works(**args)
Doi.query(args[:query], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] }).results.to_a
r = response(**args)

r.results.to_a
end

def view_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.views.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.views.buckets) : 0
end

def download_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.downloads.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.downloads.buckets) : 0
end

def citation_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.citations.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.citations.buckets) : 0
end

def response
@response ||= Doi.query(nil, funder_id: object[:id], state: "findable", page: { number: 1, size: 0 })
def response(**args)
Doi.query(args[:query], funder_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], has_person: args[:has_person], has_organization: args[:has_organization], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] })
end
end
37 changes: 26 additions & 11 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,51 @@ def address
end

def publications(**args)
Doi.query(args[:query], affiliation_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Text", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Text"
r = response(args)

r.results.to_a
end

def datasets(**args)
Doi.query(args[:query], affiliation_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Dataset", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Dataset"
r = response(args)

r.results.to_a
end

def softwares(**args)
Doi.query(args[:query], affiliation_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Software", state: "findable", page: { number: 1, size: args[:first] }).results.to_a
args[:resource_type_id] = "Software"
r = response(args)

r.results.to_a
end

def works(**args)
Rails.logger.info object[:id]
Rails.logger.info args.inspect
Doi.query(args[:query], affiliation_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] }).results.to_a
r = response(args)

r.results.to_a
end

def view_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.views.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.views.buckets) : 0
end

def download_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.downloads.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.downloads.buckets) : 0
end

def citation_count
response.results.total.positive? ? aggregate_count(response.response.aggregations.citations.buckets) : 0
args = { first: 0 }
r = response(args)
r.results.total.positive? ? aggregate_count(r.response.aggregations.citations.buckets) : 0
end

def response
@response ||= Doi.query(nil, affiliation_id: object[:id], state: "findable", page: { number: 1, size: 0 })
def response(**args)
Doi.query(args[:query], affiliation_id: object[:id], user_id: args[:user_id], client_id: args[:client_id], provider_id: args[:provider_id], funder_id: args[:funder_id], resource_type_id: args[:resource_type_id], has_person: args[:has_person], has_funder: args[:has_funder], has_citations: args[:has_citations], has_views: args[:has_views], has_downloads: args[:has_downloads], state: "findable", page: { number: 1, size: args[:first] })
end
end
Loading

0 comments on commit c28023d

Please sign in to comment.