Skip to content

Commit 87c08f9

Browse files
authored
Merge pull request rmosolgo#4652 from rmosolgo/enum-printer-fix
Fix enum variable default values in sanitized_query_string
2 parents 300ad40 + 8328e6b commit 87c08f9

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/graphql/language/sanitized_printer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ def value_to_ast(value, type)
203203
[value].map { |v| value_to_ast(v, type.of_type) }
204204
end
205205
when "ENUM"
206-
GraphQL::Language::Nodes::Enum.new(name: value)
206+
if value.is_a?(GraphQL::Language::Nodes::Enum)
207+
# if it was a default value, it's already wrapped
208+
value
209+
else
210+
GraphQL::Language::Nodes::Enum.new(name: value)
211+
end
207212
else
208213
value
209214
end

spec/graphql/language/sanitized_printer_spec.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,37 @@ def sanitize_string(query_string, inline_variables: true, **options)
344344
query = GraphQL::Query.new(SanitizeTest::CustomSanitizedPrinterSchema, query_str)
345345
assert_equal expected_query_string, query.sanitized_query_string
346346
end
347-
end
348347

348+
it "properly prints enum variable default values" do
349+
class EnumSchema < GraphQL::Schema
350+
class Grouping < GraphQL::Schema::Enum
351+
value "DAY"
352+
end
353+
354+
class Query < GraphQL::Schema::Object
355+
field :things, [String] do
356+
argument :group, Grouping
357+
end
358+
359+
def things(group:)
360+
[group]
361+
end
362+
end
363+
364+
query(Query)
365+
end
366+
query_string = <<~EOS
367+
query(
368+
$group: Grouping = DAY
369+
) {
370+
things(group: $group)
371+
}
372+
EOS
373+
374+
query = ::GraphQL::Query.new(EnumSchema, query_string)
375+
376+
assert_equal ["DAY"], query.result["data"]["things"]
377+
expected_query_string = "query {\n things(group: DAY)\n}"
378+
assert_equal expected_query_string, query.sanitized_query_string
379+
end
380+
end

0 commit comments

Comments
 (0)