Skip to content

Commit 5f5ea31

Browse files
authored
skip/include fixes. (#178)
1 parent bd1ea9d commit 5f5ea31

4 files changed

Lines changed: 104 additions & 7 deletions

File tree

lib/graphql/stitching/executor/shaper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def resolve_object_scope(raw_object, parent_type, selections, typename = nil)
3232
field_name = node.alias || node.name
3333

3434
if @request.query.get_field(parent_type, node.name).introspection?
35-
if node.name == TYPENAME && parent_type == @root_type
35+
if node.name == TYPENAME && parent_type == @root_type && node != TypeResolver::TYPENAME_EXPORT_NODE
3636
raw_object[field_name] = @root_type.graphql_name
3737
end
3838
next

lib/graphql/stitching/planner.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def perform
2424
end
2525

2626
def steps
27-
@steps_by_entrypoint.each_value.select { _1.selections.any? }.sort_by!(&:index)
27+
@steps_by_entrypoint.values.sort_by!(&:index)
2828
end
2929

3030
private
@@ -213,7 +213,7 @@ def extract_locale_selections(
213213
input_selections.each do |node|
214214
case node
215215
when GraphQL::Language::Nodes::Field
216-
if node.alias&.start_with?(TypeResolver::EXPORT_PREFIX)
216+
if node.alias&.start_with?(TypeResolver::EXPORT_PREFIX) && node != TypeResolver::TYPENAME_EXPORT_NODE
217217
raise StitchingError, %(Alias "#{node.alias}" is not allowed because "#{TypeResolver::EXPORT_PREFIX}" is a reserved prefix.)
218218
elsif node.name == TYPENAME
219219
locale_selections << node
@@ -238,7 +238,6 @@ def extract_locale_selections(
238238
selection_set = extract_locale_selections(current_location, field_type, parent_index, node.selections, path, locale_variables)
239239
path.pop
240240

241-
next if selection_set.empty?
242241
locale_selections << node.merge(selections: selection_set)
243242
end
244243

@@ -251,7 +250,6 @@ def extract_locale_selections(
251250
extract_locale_selections(current_location, fragment_type, parent_index, node.selections, path, locale_variables, selection_set)
252251

253252
unless is_same_scope
254-
next if selection_set.empty?
255253
locale_selections << node.merge(selections: selection_set)
256254
requires_typename = true
257255
end
@@ -267,7 +265,6 @@ def extract_locale_selections(
267265
extract_locale_selections(current_location, fragment_type, parent_index, fragment.selections, path, locale_variables, selection_set)
268266

269267
unless is_same_scope
270-
next if selection_set.empty?
271268
locale_selections << GraphQL::Language::Nodes::InlineFragment.new(type: fragment.type, selections: selection_set)
272269
end
273270

lib/graphql/stitching/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module GraphQL
44
module Stitching
5-
VERSION = "1.7.1"
5+
VERSION = "1.7.2"
66
end
77
end
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require_relative "../../../schemas/example"
5+
6+
describe 'GraphQL::Stitching, skip/include' do
7+
def setup
8+
@supergraph = compose_definitions({
9+
"products" => Schemas::Example::Products,
10+
"storefronts" => Schemas::Example::Storefronts,
11+
"manufacturers" => Schemas::Example::Manufacturers,
12+
})
13+
end
14+
15+
def test_skips_partial_object_fields
16+
query = %|
17+
query($id: ID!) {
18+
storefront(id: $id) {
19+
products {
20+
upc
21+
manufacturer @skip(if: true) {
22+
name
23+
}
24+
}
25+
}
26+
}
27+
|
28+
29+
result = plan_and_execute(@supergraph, query, { "id" => "1" })
30+
expected = {
31+
"storefront" => {
32+
"products" => [
33+
{ "upc" => "1" },
34+
{ "upc" => "2" },
35+
],
36+
},
37+
}
38+
39+
assert_equal expected, result.dig("data")
40+
end
41+
42+
def test_skips_all_object_fields
43+
query = %|
44+
query($id: ID!) {
45+
storefront(id: $id) {
46+
products {
47+
manufacturer @skip(if: true) {
48+
name
49+
}
50+
}
51+
}
52+
}
53+
|
54+
55+
result = plan_and_execute(@supergraph, query, { "id" => "1" })
56+
expected = {
57+
"storefront" => {
58+
"products" => [
59+
{},
60+
{},
61+
],
62+
},
63+
}
64+
65+
assert_equal expected, result.dig("data")
66+
end
67+
68+
def test_skips_partial_root_fields
69+
query = %|{
70+
product(upc: "1") {
71+
upc
72+
}
73+
storefront(id: "1") @skip(if: true) {
74+
id
75+
}
76+
}|
77+
78+
result = plan_and_execute(@supergraph, query)
79+
expected = {
80+
"product" => { "upc" => "1" }
81+
}
82+
83+
assert_equal expected, result.dig("data")
84+
end
85+
86+
def test_skips_all_root_fields
87+
query = %|
88+
query($id: ID!) {
89+
storefront(id: $id) @skip(if: true) {
90+
id
91+
}
92+
}
93+
|
94+
95+
result = plan_and_execute(@supergraph, query, { "id" => "1" })
96+
expected = {}
97+
98+
assert_equal expected, result.dig("data")
99+
end
100+
end

0 commit comments

Comments
 (0)