Skip to content

Commit

Permalink
Add Events to GraphQL endpoint (#934)
Browse files Browse the repository at this point in the history
* event and allEvents
* can include the address on an event
* Tests covering Event GraphQL endpoints
* Can ask for `allAddressLines` or `fullStreetAddress`

Co-authored-by: Dr Kim Foale <[email protected]>
  • Loading branch information
ivankocienski and kimadactyl authored Feb 14, 2022
1 parent dedf9ed commit 373d0e6
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
17 changes: 17 additions & 0 deletions app/graphql/types/address_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Types
class AddressType < Types::BaseObject

description 'An address representing a physical location'

# field :id, ID, null: false
field :street_address, String
field :street_address2, String
field :street_address3, String
field :city, String
field :postcode, String
field :country_code, String
field :full_street_address, String
field :all_address_lines, [String]
end
end

14 changes: 14 additions & 0 deletions app/graphql/types/event_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Types
class EventType < Types::BaseObject

description 'An Event that is run by a Parter'

field :id, ID, null: false
field :description, String
field :summary, String

field :address, AddressType

end
end

2 changes: 1 addition & 1 deletion app/graphql/types/partner_type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Types
class PartnerType < Types::BaseObject

description 'A partner'
description 'A Partner who runs Events'

field :id, ID, null: false
field :name, String, null: false
Expand Down
18 changes: 18 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ def all_partners
Partner.all
end

####

field :event, EventType, "Find Event by ID" do
argument :id, ID
end

field :all_events, [EventType]

def event(id:)
Event.find(id)
end

def all_events
Event.all
end

####

field :ping, String, null: false, description: "Ping server"

def ping
Expand Down
82 changes: 82 additions & 0 deletions test/integration/graphql/event_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'test_helper'

class GraphQLEventTest < ActionDispatch::IntegrationTest

setup do
@partner = FactoryBot.create(:partner)

@address = @partner.address
assert @address, 'Failed to create Address from partner'

@calendar = FactoryBot.create(
:calendar,
partner: @partner,
name: 'Partner Calendar',
source: 'http://example.com'
)
assert @calendar, 'Failed to create calendar from partner'
end

test 'can show partners' do

(0...5).collect do |n|
@partner.events.create!(
dtstart: Time.now,
summary: "An event summary #{n}",
description: 'Longer text covering the event in more detail',
address: @address
)
end

query_string = <<-GRAPHQL
query {
allEvents {
id
summary
description
}
}
GRAPHQL

result = PlaceCalSchema.execute(query_string)
data = result['data']
assert data.has_key?('allEvents'), 'result is missing key `allEvents`'

events = data['allEvents']
assert events.length == 5
end

test 'can show specific event' do
event = @partner.events.create!(
dtstart: Time.now,
summary: "An event summary",
description: 'Longer text covering the event in more detail',
address: @address
)

query_string = <<-GRAPHQL
query {
event(id: #{event.id}) {
id
summary
description
address {
streetAddress
city
postcode
}
}
}
GRAPHQL

result = PlaceCalSchema.execute(query_string)

data = result['data']
assert data.has_key?('event'), 'Data structure does not contain event key'

data_event = data['event']
assert data_event['summary'] == event.summary
end
end

0 comments on commit 373d0e6

Please sign in to comment.