Skip to content

Commit

Permalink
Implement initial GraphQL interface
Browse files Browse the repository at this point in the history
Why these changes are being introduced:

We need a GraphQL interface to record SearchEvents from TACOS
consumers.

Relevant ticket(s):

https://mitlibraries.atlassian.net/browse/ENGX-234

How this addresses that need:

This adds a SearchEvent type and corresponding query field that
returns information about the search event and creates a new
SearchEvent (and Term, if applicable).

Side effects of this change:

* Removed a couple of generated GraphQL test fields.
* Miscellaneous rubocop fixes.
  • Loading branch information
jazairi committed Nov 1, 2023
1 parent 918c8ce commit 8301b7e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
8 changes: 0 additions & 8 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# frozen_string_literal: true

module Types
class MutationType < Types::BaseObject
# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World"
end
end
end
23 changes: 14 additions & 9 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

module Types
class QueryType < Types::BaseObject
field :node, Types::NodeType, null: true, description: "Fetches an object given its ID." do
argument :id, ID, required: true, description: "ID of the object."
field :node, Types::NodeType, null: true, description: 'Fetches an object given its ID.' do
argument :id, ID, required: true, description: 'ID of the object.'
end

def node(id:)
context.schema.object_from_id(id, context)
end

field :nodes, [Types::NodeType, null: true], null: true, description: "Fetches a list of objects given a list of IDs." do
argument :ids, [ID], required: true, description: "IDs of the objects."
field :nodes, [Types::NodeType, { null: true }], null: true,
description: 'Fetches a list of objects given a list of IDs.' do
argument :ids, [ID], required: true, description: 'IDs of the objects.'
end

def nodes(ids:)
Expand All @@ -21,11 +22,15 @@ def nodes(ids:)
# Add root-level fields here.
# They will be entry points for queries on your schema.

# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World!"
field :log_search_event, SearchEventType, null: false,
description: 'Log a search and return information about it.' do
argument :search_term, String, required: true
argument :source_system, String, required: true
end

def log_search_event(search_term:, source_system:)
term = Term.create_or_find_by!(phrase: search_term)
term.search_events.create!(source: source_system)
end
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/search_event_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
class SearchEventType < Types::BaseObject
field :id, ID, null: false
field :term_id, Integer
field :source, String
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
end
end
51 changes: 51 additions & 0 deletions test/controllers/graphql_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test_helper'

class GraphqlControllerTest < ActionDispatch::IntegrationTest
test 'search event query returns relevant data' do
post '/graphql', params: { query: '{
logSearchEvent(sourceSystem: "bento", searchTerm: "range life") {
termId
source
createdAt
updatedAt
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)
term_id = Term.last.id

assert_equal 'bento', json['data']['logSearchEvent']['source']
assert_equal term_id, json['data']['logSearchEvent']['termId']
assert_equal Date.today, json['data']['logSearchEvent']['createdAt'].to_date
assert_equal Date.today, json['data']['logSearchEvent']['updatedAt'].to_date
end

test 'search event query creates a new term if one does not exist' do
initial_term_count = Term.count
post '/graphql', params: { query: '{
logSearchEvent(sourceSystem: "bento", searchTerm: "range life") {
termId
source
createdAt
updatedAt
}
}' }
assert_equal(200, response.status)
assert_equal Term.count, (initial_term_count + 1)
assert_equal 'range life', Term.last.phrase
end

test 'search event query does not create a new term if phrase is already stored' do
initial_term_count = Term.count
post '/graphql', params: { query: '{
logSearchEvent(sourceSystem: "timdex", searchTerm: "Super cool search") {
termId
source
createdAt
updatedAt
}
}' }
assert_equal(200, response.status)
assert_equal Term.count, initial_term_count
end
end

0 comments on commit 8301b7e

Please sign in to comment.