Permalink
Browse files

Switch user meetings to use relay style paginaion.

  • Loading branch information...
dblock committed Jul 8, 2018
1 parent 7a90263 commit f0bd7df9dd0f4311344557dce4e673e28c84f1d1
Showing with 69 additions and 16 deletions.
  1. +1 −1 app/graphql/types/user_type.rb
  2. +3 −0 config/initializers/mongoid_relay.rb
  3. +65 −15 spec/graphql/queries/user_meetings_spec.rb
@@ -15,5 +15,5 @@
}
end

field :meetings, -> { !types[Types::MeetingType] }
connection :meetings, Types::MeetingType.connection_type
end
@@ -0,0 +1,3 @@
GraphQL::Relay::BaseConnection.register_connection_implementation(
Mongoid::Association::Referenced::HasMany::Targets::Enumerable, GraphQL::Relay::MongoRelationConnection
)
@@ -3,25 +3,75 @@
describe 'Current User Meetings Query', type: :request do
include_context 'Authenticated Client'

let(:query) do
<<-GRAPHQL
query {
user {
meetings {
id
title
started
finished
let!(:meetings) { 3.times { Fabricate(:meeting, user: current_user) } }

context 'all at once' do
let(:query) do
<<-GRAPHQL
query {
user {
meetings {
edges {
node {
id
title,
started,
finished
},
cursor
},
pageInfo {
hasNextPage,
hasPreviousPage,
endCursor
}
}
}
}
}
GRAPHQL
GRAPHQL
end

it 'returns the meetings' do
meetings = client.execute(query).data.user.meetings.edges.map(&:node)
expect(meetings.size).to eq 3
end
end

let!(:meetings) { 3.times { Fabricate(:meeting, user: current_user) } }
context 'by 2' do
let(:query) do
<<-GRAPHQL
query($first: Int, $after: String) {
user {
meetings(first: $first, after: $after) {
edges {
node {
id
},
cursor
},
pageInfo {
hasNextPage,
hasPreviousPage,
endCursor
}
}
}
}
GRAPHQL
end

it 'returns the meetings' do
meetings = client.execute(query).data.user.meetings
expect(meetings.size).to eq 3
it 'relay-paginates through meetings' do
all = []
cursor = nil
loop do
page_meetings = client.execute(query, first: 2, after: cursor).data.user.meetings
edges = page_meetings.edges
expect(edges.size).to be <= 2
all.concat(edges.map(&:node))
cursor = page_meetings.page_info.end_cursor
break unless cursor
end
expect(all.map(&:id).sort).to eq current_user.meetings.map(&:id).map(&:to_s).sort
end
end
end

0 comments on commit f0bd7df

Please sign in to comment.