Skip to content

Commit 7597939

Browse files
Failing specs showing lookahead issue on fragments
while also adding a description of `#selects?` and moving some assertions from `#selections`
1 parent daf2e6a commit 7597939

File tree

1 file changed

+178
-2
lines changed

1 file changed

+178
-2
lines changed

spec/graphql/execution/lookahead_spec.rb

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Schema < GraphQL::Schema
142142
GRAPHQL
143143
}
144144

145-
it "finds fields on object types and interface types" do
145+
it "enumerates fields on object types and interface types" do
146146
node_lookahead = query.lookahead.selection("node")
147147
assert_equal [:id, :name, :latin_name], node_lookahead.selections.map(&:name)
148148
end
@@ -404,13 +404,189 @@ def query(doc = document)
404404
variables: { skipName: false, includeGenus: true })
405405
lookahead = query.lookahead.selection("findBirdSpecies")
406406
assert_equal [:id, :name, :genus], lookahead.selections.map(&:name)
407-
assert_equal true, lookahead.selects?(:name)
408407

409408
query = GraphQL::Query.new(LookaheadTest::Schema, document: document,
410409
variables: { skipName: true, includeGenus: false })
411410
lookahead = query.lookahead.selection("findBirdSpecies")
412411
assert_equal [:id], lookahead.selections.map(&:name)
412+
end
413+
end
414+
415+
describe '#selects?' do
416+
let(:document) {
417+
GraphQL.parse <<-GRAPHQL
418+
query {
419+
findBirdSpecies(byName: "Laughing Gull") {
420+
name
421+
similarSpecies {
422+
likesWater: isWaterfowl
423+
}
424+
}
425+
}
426+
GRAPHQL
427+
}
428+
429+
def query(doc = document)
430+
GraphQL::Query.new(LookaheadTest::Schema, document: doc)
431+
end
432+
433+
it "returns true for a field that is selected" do
434+
ast_node = document.definitions.first.selections.first
435+
field = LookaheadTest::Query.fields["findBirdSpecies"]
436+
lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
437+
assert lookahead.selects?(:name)
438+
assert lookahead.selects?(:similar_species)
439+
assert_equal false, lookahead.selects?(:is_waterfowl)
440+
end
441+
442+
it "returns false for a field that is not selected" do
443+
ast_node = document.definitions.first.selections.first
444+
field = LookaheadTest::Query.fields["findBirdSpecies"]
445+
lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
446+
assert_equal false, lookahead.selects?(:is_waterfowl)
447+
end
448+
449+
it "returns false for a selection which does not match arguments" do
450+
ast_node = document.definitions.first
451+
lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], root_type: LookaheadTest::Query)
452+
arguments = { by_name: "Cardinal" }
453+
454+
assert_equal false, lookahead.selects?(:name, arguments: arguments)
455+
end
456+
457+
it "returns true for a selection which matches arguments" do
458+
ast_node = document.definitions.first
459+
lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], root_type: LookaheadTest::Query)
460+
arguments = { by_name: "Laughing Gull" }
461+
462+
assert lookahead.selects?(:find_bird_species, arguments: arguments)
463+
end
464+
465+
it 'returns true for selection that is duplicated across fragments' do
466+
doc = GraphQL.parse <<-GRAPHQL
467+
query {
468+
... on Query {
469+
...MoreFields
470+
}
471+
}
472+
473+
fragment MoreFields on Query {
474+
findBirdSpecies(byName: "Laughing Gull") {
475+
name
476+
}
477+
findBirdSpecies(byName: "Laughing Gull") {
478+
...EvenMoreFields
479+
}
480+
}
481+
482+
fragment EvenMoreFields on BirdSpecies {
483+
similarSpecies {
484+
likesWater: isWaterfowl
485+
}
486+
}
487+
GRAPHQL
488+
489+
lookahead = query(doc).lookahead
490+
assert lookahead.selects?(:find_bird_species)
491+
492+
assert lookahead.selection(:find_bird_species).selects?(:name)
493+
assert lookahead.selection(:find_bird_species).selects?(:similar_species)
494+
end
495+
496+
it "returns true for a field name that exists on multiple distinct types" do
497+
query = GraphQL::Query.new(LookaheadTest::Schema, <<-GRAPHQL)
498+
query {
499+
node(id: "Cardinal") {
500+
... on BirdSpecies {
501+
name
502+
}
503+
... on BirdGenus {
504+
name
505+
}
506+
id
507+
}
508+
}
509+
GRAPHQL
510+
511+
node_lookahead = query.lookahead.selection("node")
512+
assert node_lookahead.selects?(:id)
513+
assert node_lookahead.selects?(:name)
514+
end
515+
516+
it "returns false on missing selections" do
517+
ast_node = document.definitions.first.selections.first
518+
field = LookaheadTest::Query.fields["findBirdSpecies"]
519+
lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
520+
assert_equal false, lookahead.selection(:genus).selects?(:name)
521+
end
522+
523+
it "returns true for fields enabled by directives" do
524+
document = GraphQL.parse <<-GRAPHQL
525+
query($skipName: Boolean!, $includeGenus: Boolean!){
526+
findBirdSpecies(byName: "Cardinal") {
527+
id
528+
name @skip(if: $skipName)
529+
genus @include(if: $includeGenus)
530+
}
531+
}
532+
GRAPHQL
533+
query = GraphQL::Query.new(LookaheadTest::Schema, document: document,
534+
variables: { skipName: false, includeGenus: true })
535+
lookahead = query.lookahead.selection("findBirdSpecies")
536+
assert lookahead.selects?(:name)
537+
assert lookahead.selects?(:genus)
538+
end
539+
540+
it "returns false for fields disabled by directive" do
541+
document = GraphQL.parse <<-GRAPHQL
542+
query($skipName: Boolean!, $includeGenus: Boolean!){
543+
findBirdSpecies(byName: "Cardinal") {
544+
id
545+
name @skip(if: $skipName)
546+
genus @include(if: $includeGenus)
547+
}
548+
}
549+
GRAPHQL
550+
query = GraphQL::Query.new(LookaheadTest::Schema, document: document,
551+
variables: { skipName: true, includeGenus: false })
552+
lookahead = query.lookahead.selection("findBirdSpecies")
553+
assert lookahead.selects?(:id)
413554
assert_equal false, lookahead.selects?(:name)
555+
assert_equal false, lookahead.selects?(:genus)
556+
end
557+
558+
describe "fields on interfaces" do
559+
let(:document) {
560+
GraphQL.parse <<-GRAPHQL
561+
query {
562+
node(id: "Cardinal") {
563+
id
564+
... on BirdSpecies {
565+
name
566+
}
567+
...Other
568+
}
569+
}
570+
fragment Other on BirdGenus {
571+
latinName
572+
}
573+
GRAPHQL
574+
}
575+
576+
it "returns true for fields on direct object types" do
577+
node_lookahead = query.lookahead.selection("node")
578+
assert node_lookahead.selects?(:id)
579+
end
580+
581+
it "returns true for fields on interface types" do
582+
node_lookahead = query.lookahead.selection("node")
583+
assert node_lookahead.selects?(:name)
584+
end
585+
586+
it "returns true for fields on interface types through fragments" do
587+
node_lookahead = query.lookahead.selection("node")
588+
assert node_lookahead.selects?(:latin_name)
589+
end
414590
end
415591
end
416592
end

0 commit comments

Comments
 (0)