Skip to content

Commit 4e57d85

Browse files
authored
Merge pull request #38 from jturkel/add-argument-with-default
Adding non-null arguments with a default value should be non-breaking
2 parents 363a2b4 + 835b9ea commit 4e57d85

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

lib/graphql/schema_comparator/changes.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ class InputFieldAdded < AbstractChange
579579
attr_reader :input_object_type, :field, :criticality
580580

581581
def initialize(input_object_type, field)
582-
@criticality = if field.type.non_null?
583-
Changes::Criticality.breaking(reason: "Adding a non-null field to an existing input type will cause existing queries that use this input type to error because they will not provide a value for this new field.")
582+
@criticality = if field.type.non_null? && !field.default_value?
583+
Changes::Criticality.breaking(reason: "Adding a non-null input field without a default value to an existing input type will cause existing queries that use this input type to error because they will not provide a value for this new field.")
584584
else
585585
Changes::Criticality.non_breaking
586586
end
@@ -602,8 +602,8 @@ class FieldArgumentAdded < AbstractChange
602602
attr_reader :type, :field, :argument, :criticality
603603

604604
def initialize(type, field, argument)
605-
@criticality = if argument.type.non_null?
606-
Changes::Criticality.breaking(reason: "Adding a required argument to an existing field is a breaking change because it will cause existing uses of this field to error.")
605+
@criticality = if argument.type.non_null? && !argument.default_value?
606+
Changes::Criticality.breaking(reason: "Adding a required argument without a default value to an existing field is a breaking change because it will cause existing uses of this field to error.")
607607
else
608608
Changes::Criticality.non_breaking
609609
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require "test_helper"
2+
3+
class GraphQL::SchemaComparator::Changes::FieldArgumentAddedTest < Minitest::Test
4+
def setup
5+
@type = GraphQL::ObjectType.define do
6+
name "Type"
7+
end
8+
9+
@field = GraphQL::Field.define do
10+
name "field"
11+
end
12+
13+
@nullable_argument = GraphQL::Argument.define do
14+
name "foo"
15+
type GraphQL::STRING_TYPE
16+
end
17+
18+
@non_null_argument = GraphQL::Argument.define do
19+
name "foo"
20+
type !GraphQL::STRING_TYPE
21+
end
22+
23+
@non_null_argument_with_default = GraphQL::Argument.define do
24+
name "foo"
25+
type !GraphQL::STRING_TYPE
26+
default_value "bar"
27+
end
28+
end
29+
30+
def test_nullable_added
31+
change = GraphQL::SchemaComparator::Changes::FieldArgumentAdded.new(
32+
@type,
33+
@field,
34+
@nullable_argument
35+
)
36+
37+
assert change.non_breaking?
38+
end
39+
40+
def test_non_null_added
41+
change = GraphQL::SchemaComparator::Changes::FieldArgumentAdded.new(
42+
@type,
43+
@field,
44+
@non_null_argument
45+
)
46+
47+
assert change.breaking?
48+
end
49+
50+
def test_non_null_with_default_added
51+
change = GraphQL::SchemaComparator::Changes::FieldArgumentAdded.new(
52+
@type,
53+
@field,
54+
@non_null_argument_with_default
55+
)
56+
57+
assert change.non_breaking?
58+
end
59+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "test_helper"
2+
3+
class GraphQL::SchemaComparator::Changes::InputFieldAddedTest < Minitest::Test
4+
def setup
5+
@input_type = GraphQL::InputObjectType.define do
6+
name "Input"
7+
end
8+
9+
@nullable_input_field = GraphQL::Argument.define do
10+
name "foo"
11+
type GraphQL::STRING_TYPE
12+
end
13+
14+
@non_null_input_field = GraphQL::Argument.define do
15+
name "foo"
16+
type !GraphQL::STRING_TYPE
17+
end
18+
19+
@non_null_input_field_with_default = GraphQL::Argument.define do
20+
name "foo"
21+
type !GraphQL::STRING_TYPE
22+
default_value "bar"
23+
end
24+
end
25+
26+
def test_nullable_added
27+
change = GraphQL::SchemaComparator::Changes::InputFieldAdded.new(
28+
@input_type,
29+
@nullable_input_field
30+
)
31+
32+
assert change.non_breaking?
33+
end
34+
35+
def test_non_null_added
36+
change = GraphQL::SchemaComparator::Changes::InputFieldAdded.new(
37+
@input_type,
38+
@non_null_input_field
39+
)
40+
41+
assert change.breaking?
42+
end
43+
44+
def test_non_null_with_default_added
45+
change = GraphQL::SchemaComparator::Changes::InputFieldAdded.new(
46+
@input_type,
47+
@non_null_input_field_with_default
48+
)
49+
50+
assert change.non_breaking?
51+
end
52+
end

0 commit comments

Comments
 (0)