1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
# frozen_string_literal: true
require 'spec_helper'
describe "GraphQL::Relay::RelationConnection" do
def get_names(result)
ships = result["data"]["empire"]["bases"]["edges"]
ships.map { |e| e["node"]["name"] }
end
def get_page_info(result)
result["data"]["empire"]["bases"]["pageInfo"]
end
def get_first_cursor(result)
result["data"]["empire"]["bases"]["edges"].first["cursor"]
end
def get_last_cursor(result)
result["data"]["empire"]["bases"]["edges"].last["cursor"]
end
describe "results" do
let(:query_string) {%|
query getShips($first: Int, $after: String, $last: Int, $before: String, $nameIncludes: String){
empire {
bases(first: $first, after: $after, last: $last, before: $before, nameIncludes: $nameIncludes) {
... basesConnection
}
}
}
fragment basesConnection on BasesConnectionWithTotalCount {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
},
totalCount,
edges {
cursor
node {
name
}
}
}
|}
it 'limits the result' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(2, get_names(result).length)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))
result = star_wars_query(query_string, { "first" => 3 })
assert_equal(3, get_names(result).length)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))
end
it 'returns the correct hasNextPage value' do
first_page_result = star_wars_query(query_string, { "first" => 2})
assert_equal(true, get_page_info(first_page_result)["hasNextPage"])
result = star_wars_query(query_string, { "first" => 2, "after" => get_page_info(first_page_result)["endCursor"] })
assert_equal(false, get_page_info(result)["hasNextPage"])
end
it "uses unscope(:order) count(*) when the relation has some complicated SQL" do
query_s = <<-GRAPHQL
query getShips($first: Int, $after: String, $complexOrder: Boolean){
empire {
bases(first: $first, after: $after, complexOrder: $complexOrder) {
edges {
node {
name
}
}
pageInfo {
hasNextPage
}
}
}
}
GRAPHQL
result = nil
log = with_active_record_log do
result = star_wars_query(query_s, { "first" => 1, "after" => "MQ==", "complexOrder" => true })
end
conn = result["data"]["empire"]["bases"]
assert_equal(1, conn["edges"].size)
assert_equal(true, conn["pageInfo"]["hasNextPage"])
log_entries = log.split("\n")
assert_equal 1, log_entries.size, "It should run 1 sql query"
edges_query, = log_entries.first
assert_includes edges_query, "ORDER BY bases.name", "The query for edges _is_ ordered"
end
it 'provides custom fields on the connection type' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(
StarWars::Base.where(faction_id: 2).count,
result["data"]["empire"]["bases"]["totalCount"]
)
end
it "makes one sql query for items and another for count" do
query_str = <<-GRAPHQL
{
empire {
bases(first: 2) {
totalCount
edges {
cursor
node {
name
}
}
}
}
}
GRAPHQL
result = nil
log = with_active_record_log do
result = star_wars_query(query_str, { "first" => 2 })
end
assert_equal 2, log.scan("\n").count, "Two log entries"
assert_equal 3, result["data"]["empire"]["bases"]["totalCount"]
assert_equal 2, result["data"]["empire"]["bases"]["edges"].size
end
it "does bidirectional pagination by default" do
result = star_wars_query(query_string, { "first" => 1 })
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "first" => 1, "after" => last_cursor })
assert_equal true, get_page_info(result)["hasNextPage"]
assert_equal true, get_page_info(result)["hasPreviousPage"]
result = star_wars_query(query_string, { "first" => 100 })
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "last" => 1, "before" => last_cursor })
assert_equal true, get_page_info(result)["hasNextPage"]
assert_equal true, get_page_info(result)["hasPreviousPage"]
end
it 'slices the result' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
# After the last result, find the next 2:
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "after" => last_cursor, "first" => 2 })
assert_equal(["Headquarters"], get_names(result))
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 1 })
assert_equal(["Shield Generator"], get_names(result))
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 2 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 10 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
result = star_wars_query(query_string, { "last" => 2 })
assert_equal(["Shield Generator", "Headquarters"], get_names(result))
result = star_wars_query(query_string, { "last" => 10 })
assert_equal(["Death Star", "Shield Generator", "Headquarters"], get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"])
end
it 'works with before and after specified together' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
first_cursor = get_last_cursor(result)
# There is no records between before and after if they point to the same cursor
result = star_wars_query(query_string, { "before" => first_cursor, "after" => first_cursor, "last" => 2 })
assert_equal([], get_names(result))
result = star_wars_query(query_string, { "after" => first_cursor, "first" => 2 })
assert_equal(["Headquarters"], get_names(result))
second_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "after" => first_cursor, "before" => second_cursor, "first" => 3 })
assert_equal([], get_names(result))
end
it 'handles cursors above the bounds of the array' do
overreaching_cursor = Base64.strict_encode64("100")
result = star_wars_query(query_string, { "after" => overreaching_cursor, "first" => 2 })
assert_equal([], get_names(result))
end
it 'handles cursors below the bounds of the array' do
underreaching_cursor = Base64.strict_encode64("1")
result = star_wars_query(query_string, { "before" => underreaching_cursor, "first" => 2 })
assert_equal([], get_names(result))
end
it 'handles grouped connections with only last argument' do
grouped_conn_query = <<-GRAPHQL
query {
newestBasesGroupedByFaction(last: 2) {
edges {
node {
name
}
}
}
}
GRAPHQL
result = star_wars_query(grouped_conn_query)
names = result['data']['newestBasesGroupedByFaction']['edges'].map { |edge| edge['node']['name'] }
assert_equal(['Headquarters', 'Secret Hideout'], names)
end
it "applies custom arguments" do
result = star_wars_query(query_string, { "first" => 1, "nameIncludes" => "ea" })
assert_equal(["Death Star"], get_names(result))
after = get_last_cursor(result)
result = star_wars_query(query_string, { "first" => 2, "nameIncludes" => "ea", "after" => after })
assert_equal(["Headquarters"], get_names(result))
before = get_last_cursor(result)
result = star_wars_query(query_string, { "last" => 1, "nameIncludes" => "ea", "before" => before })
assert_equal(["Death Star"], get_names(result))
end
it 'works without first/last/after/before' do
result = star_wars_query(query_string)
assert_equal(3, result["data"]["empire"]["bases"]["edges"].length)
end
describe "applying max_page_size" do
let(:query_string) {%|
query getBases($first: Int, $after: String, $last: Int, $before: String){
empire {
bases: basesWithMaxLimitRelation(first: $first, after: $after, last: $last, before: $before) {
... basesConnection
}
}
}
fragment basesConnection on BaseConnection {
edges {
cursor
node {
name
}
},
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
|}
it "applies to queries by `first`" do
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(2, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"])
# Max page size is applied _without_ `first`, also
result = star_wars_query(query_string)
assert_equal(2, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"], "hasNextPage is false when first is not specified")
end
it "applies to queries by `last`" do
second_to_last_two_names = ["Death Star", "Shield Generator"]
first_and_second_names = ["Yavin", "Echo Base"]
last_cursor = "Ng=="
result = star_wars_query(query_string, { "last" => 100, "before" => last_cursor })
assert_equal(second_to_last_two_names, get_names(result))
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"])
result = star_wars_query(query_string, { "before" => last_cursor })
assert_equal(first_and_second_names, get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
third_cursor = "Mw"
result = star_wars_query(query_string, { "last" => 100, "before" => third_cursor })
assert_equal(first_and_second_names, get_names(result))
result = star_wars_query(query_string, { "before" => third_cursor })
assert_equal(first_and_second_names, get_names(result))
end
end
describe "applying default_max_page_size" do
let(:query_string) {%|
query getBases($first: Int, $after: String, $last: Int, $before: String){
empire {
bases: basesWithDefaultMaxLimitRelation(first: $first, after: $after, last: $last, before: $before) {
... basesConnection
}
}
}
fragment basesConnection on BaseConnection {
edges {
cursor
node {
name
}
},
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
|}
it "applies to queries by `first`" do
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(3, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"])
# Max page size is applied _without_ `first`, also
result = star_wars_query(query_string)
assert_equal(3, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"], "hasNextPage is false when first is not specified")
end
it "applies to queries by `last`" do
second_to_last_three_names = ["Secret Hideout", "Death Star", "Shield Generator"]
first_second_and_third_names = ["Yavin", "Echo Base", "Secret Hideout"]
last_cursor = "Ng=="
result = star_wars_query(query_string, { "last" => 100, "before" => last_cursor })
assert_equal(second_to_last_three_names, get_names(result))
assert_equal(true, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"])
result = star_wars_query(query_string, { "before" => last_cursor })
assert_equal(first_second_and_third_names, get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
fourth_cursor = "NA=="
result = star_wars_query(query_string, { "last" => 100, "before" => fourth_cursor })
assert_equal(first_second_and_third_names, get_names(result))
result = star_wars_query(query_string, { "before" => fourth_cursor })
assert_equal(first_second_and_third_names, get_names(result))
end
end
end
describe "applying a max_page_size bigger than the results" do
let(:query_string) {%|
query getBases($first: Int, $after: String, $last: Int, $before: String){
empire {
bases: basesWithLargeMaxLimitRelation(first: $first, after: $after, last: $last, before: $before) {
... basesConnection
}
}
}
fragment basesConnection on BaseConnection {
edges {
cursor
node {
name
}
},
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
|}
it "applies to queries by `first`" do
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(6, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"])
# Max page size is applied _without_ `first`, also
result = star_wars_query(query_string)
assert_equal(6, result["data"]["empire"]["bases"]["edges"].size)
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasNextPage"], "hasNextPage is false when first is not specified")
end
it "applies to queries by `last`" do
all_names = ["Yavin", "Echo Base", "Secret Hideout", "Death Star", "Shield Generator", "Headquarters"]
last_cursor = "Ng=="
result = star_wars_query(query_string, { "last" => 100, "before" => last_cursor })
assert_equal(all_names[0..4], get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"])
result = star_wars_query(query_string, { "last" => 100 })
assert_equal(all_names, get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"])
result = star_wars_query(query_string, { "before" => last_cursor })
assert_equal(all_names[0..4], get_names(result))
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
fourth_cursor = "NA=="
result = star_wars_query(query_string, { "last" => 100, "before" => fourth_cursor })
assert_equal(all_names[0..2], get_names(result))
result = star_wars_query(query_string, { "before" => fourth_cursor })
assert_equal(all_names[0..2], get_names(result))
end
end
describe "without a block" do
let(:query_string) {%|
{
empire {
basesClone(first: 10) {
edges {
node {
name
}
}
}
}
}|}
it "uses default resolve" do
result = star_wars_query(query_string)
bases = result["data"]["empire"]["basesClone"]["edges"]
assert_equal(3, bases.length)
end
end
describe "custom ordering" do
let(:query_string) {%|
query getBases {
empire {
basesByName(first: 30) { ... basesFields }
bases(first: 30) { ... basesFields2 }
}
}
fragment basesFields on BaseConnection {
edges {
node {
name
}
}
}
fragment basesFields2 on BasesConnectionWithTotalCount {
edges {
node {
name
}
}
}
|}
def get_names(result, field_name)
bases = result["data"]["empire"][field_name]["edges"]
bases.map { |b| b["node"]["name"] }
end
it "applies the default value" do
result = star_wars_query(query_string)
bases_by_id = ["Death Star", "Shield Generator", "Headquarters"]
bases_by_name = ["Death Star", "Headquarters", "Shield Generator"]
assert_equal(bases_by_id, get_names(result, "bases"))
assert_equal(bases_by_name, get_names(result, "basesByName"))
end
end
describe "with a Sequel::Dataset" do
def get_names(result)
ships = result["data"]["empire"]["basesAsSequelDataset"]["edges"]
ships.map { |e| e["node"]["name"] }
end
def get_page_info(result)
result["data"]["empire"]["basesAsSequelDataset"]["pageInfo"]
end
def get_first_cursor(result)
result["data"]["empire"]["basesAsSequelDataset"]["edges"].first["cursor"]
end
def get_last_cursor(result)
result["data"]["empire"]["basesAsSequelDataset"]["edges"].last["cursor"]
end
describe "results" do
let(:query_string) {%|
query getShips($first: Int, $after: String, $last: Int, $before: String, $nameIncludes: String){
empire {
basesAsSequelDataset(first: $first, after: $after, last: $last, before: $before, nameIncludes: $nameIncludes) {
... basesConnection
}
}
}
fragment basesConnection on BasesConnectionWithTotalCount {
totalCount,
edges {
cursor
node {
name
}
},
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
|}
it 'limits the result' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(2, get_names(result).length)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))
result = star_wars_query(query_string, { "first" => 3 })
assert_equal(3, get_names(result).length)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))
end
it 'provides custom fields on the connection type' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(
StarWars::Base.where(faction_id: 2).count,
result["data"]["empire"]["basesAsSequelDataset"]["totalCount"]
)
end
it 'slices the result' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
# After the last result, find the next 2:
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "after" => last_cursor, "first" => 2 })
assert_equal(["Headquarters"], get_names(result))
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 1 })
assert_equal(["Shield Generator"], get_names(result))
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 2 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 10 })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
end
it 'handles cursors above the bounds of the array' do
overreaching_cursor = Base64.strict_encode64("100")
result = star_wars_query(query_string, { "after" => overreaching_cursor, "first" => 2 })
assert_equal([], get_names(result))
end
it 'handles cursors below the bounds of the array' do
underreaching_cursor = Base64.strict_encode64("1")
result = star_wars_query(query_string, { "before" => underreaching_cursor, "first" => 2 })
assert_equal([], get_names(result))
end
it "applies custom arguments" do
result = star_wars_query(query_string, { "first" => 1, "nameIncludes" => "ea" })
assert_equal(["Death Star"], get_names(result))
after = get_last_cursor(result)
result = star_wars_query(query_string, { "first" => 2, "nameIncludes" => "ea", "after" => after })
assert_equal(["Headquarters"], get_names(result))
before = get_last_cursor(result)
result = star_wars_query(query_string, { "last" => 1, "nameIncludes" => "ea", "before" => before })
assert_equal(["Death Star"], get_names(result))
end
it "makes one sql query for items and another for count" do
query_str = <<-GRAPHQL
{
empire {
basesAsSequelDataset(first: 2) {
totalCount
edges {
cursor
node {
name
}
}
}
}
}
GRAPHQL
result = nil
io = StringIO.new
begin
SequelDB.loggers << Logger.new(io)
result = star_wars_query(query_str, { "first" => 2 })
ensure
SequelDB.loggers.pop
end
assert_equal 2, io.string.scan("SELECT").count
assert_equal 3, result["data"]["empire"]["basesAsSequelDataset"]["totalCount"]
assert_equal 2, result["data"]["empire"]["basesAsSequelDataset"]["edges"].size
end
end
end
end
|