Skip to content

Commit fdbd505

Browse files
Failing specs showing lookahead issue on fragments
1 parent daf2e6a commit fdbd505

File tree

1 file changed

+175
-5
lines changed

1 file changed

+175
-5
lines changed

spec/graphql/execution/lookahead_spec.rb

+175-5
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
@@ -243,9 +243,9 @@ class Schema < GraphQL::Schema
243243

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

0 commit comments

Comments
 (0)