Skip to content

Commit

Permalink
Merge pull request #97 from MITLibraries/tco-24-suggested-resource-gr…
Browse files Browse the repository at this point in the history
…aphql

Add GraphQL for Detector::SuggestedResource
  • Loading branch information
jazairi authored Aug 27, 2024
2 parents dbcd565 + 31b6af4 commit cfd7a95
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/graphql/types/detectors_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class DetectorsType < Types::BaseObject

field :journals, [Types::JournalsType], description: 'Information about journals detected in the search term'
field :standard_identifiers, [Types::StandardIdentifiersType], description: 'Currently supported: ISBN, ISSN, PMID, DOI'
field :suggested_resources, [Types::SuggestedResourcesType], description: 'Suggested resources detected in the search term'

def standard_identifiers
Detector::StandardIdentifiers.new(@object).identifiers.map do |identifier|
Expand All @@ -18,5 +19,11 @@ def journals
{ title: journal.name, additional_info: journal.additional_info }
end
end

def suggested_resources
Detector::SuggestedResource.full_term_match(@object).map do |suggested_resource|
{ title: suggested_resource.title, url: suggested_resource.url }
end
end
end
end
10 changes: 10 additions & 0 deletions app/graphql/types/suggested_resources_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Types
class SuggestedResourcesType < Types::BaseObject
description 'A detector for any suggested resources associated with a search term'

field :title, String, null: false, description: 'The title or name of the suggested resource'
field :url, String, null: false, description: 'The URL to the suggested resource'
end
end
20 changes: 20 additions & 0 deletions test/controllers/graphql_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ class GraphqlControllerTest < ActionDispatch::IntegrationTest
json['data']['logSearchEvent']['detectors']['journals'].first['additionalInfo'])
end

test 'search event query can return detected suggested resources' do
post '/graphql', params: { query: '{
logSearchEvent(sourceSystem: "timdex", searchTerm: "web of science") {
detectors {
suggestedResources {
title
url
}
}
}
}' }

json = response.parsed_body

assert_equal 'Web of Science',
json['data']['logSearchEvent']['detectors']['suggestedResources'].first['title']
assert_equal 'https://libguides.mit.edu/webofsci',
json['data']['logSearchEvent']['detectors']['suggestedResources'].first['url']
end

test 'search event query can return phrase from logged term' do
post '/graphql', params: { query: '{
logSearchEvent(sourceSystem: "timdex", searchTerm: "10.1038/nphys1170") {
Expand Down

0 comments on commit cfd7a95

Please sign in to comment.