From 98a1f6432c71344999f4ac4ff267a58013a98739 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 12 Oct 2019 11:38:29 +0200 Subject: [PATCH] added events to graphql api. #346 --- app/graphql/types/event_data_edge.rb | 19 ++++++++++++------- app/models/concerns/indexable.rb | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/graphql/types/event_data_edge.rb b/app/graphql/types/event_data_edge.rb index b38ab5018..2d1cbbe31 100644 --- a/app/graphql/types/event_data_edge.rb +++ b/app/graphql/types/event_data_edge.rb @@ -10,12 +10,13 @@ class EventDataEdge < GraphQL::Relay::Edge def event_data @event_data ||= begin - Event.query(nil, subj_id: self.node.id, obj_id: self.parent.id).results.first.to_h + return nil unless self.node.present? + Event.query(nil, subj_id: doi_from_node(self.node), obj_id: self.parent.id).results.first.to_h.fetch("_source", nil) end end def source_id - self.node.id + self.node.identifier if self.node.present? end def target_id @@ -23,16 +24,20 @@ def target_id end def source - event_data.source_id.underscore.camelcase(:lower) + event_data.source_id if event_data.present? end - # We are switching subj and obj, and thus need to change direction of relation type def relation_type - rt = event_data.relation_type_id.underscore.camelcase(:lower) - RELATION_TYPES.fetch(rt, rt) + event_data.relation_type_id.underscore.camelcase(:lower) if event_data.present? end def total - event_data.total + event_data.total if event_data.present? + end + + def doi_from_node(node) + return nil unless node.present? + + "https://doi.org/#{node.uid.downcase}" end end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 8daa06001..39f44cd18 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -210,8 +210,8 @@ def query(query, options={}) must << { terms: { "client.certificate" => options[:certificate].split(",") }} if options[:certificate].present? must_not << { terms: { provider_id: ["crossref", "medra", "op"] }} if options[:exclude_registration_agencies] elsif self.name == "Event" - must << { term: { subj_id: options[:subj_id] }} if options[:subj_id].present? - must << { term: { obj_id: options[:obj_id] }} if options[:obj_id].present? + must << { term: { subj_id: URI.decode(options[:subj_id]) }} if options[:subj_id].present? + must << { term: { obj_id: URI.decode(options[:obj_id]) }} if options[:obj_id].present? must << { term: { citation_type: options[:citation_type] }} if options[:citation_type].present? must << { term: { year_month: options[:year_month] }} if options[:year_month].present? must << { range: { "subj.datePublished" => { gte: "#{options[:publication_year].split("-").min}||/y", lte: "#{options[:publication_year].split("-").max}||/y", format: "yyyy" }}} if options[:publication_year].present?