Skip to content

Commit 75895a1

Browse files
authored
fix: consolidation nullability (#108)
* unit test * make test pass
1 parent e92c452 commit 75895a1

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/utils/should-consolidate-types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ function typesAreEquivalent(
7171
const matchingInputField = inputNode.astNode?.fields?.find(
7272
(inputField) => inputField.name.value === typeField.name.value,
7373
);
74-
if (!matchingInputField?.type) return false;
74+
if (
75+
!matchingInputField?.type ||
76+
typeField.type.kind !== matchingInputField.type.kind
77+
) {
78+
return false;
79+
}
80+
7581
const baseTypeName = getBaseTypeNode(typeField.type).name.value;
7682
const baseInputTypeName = getBaseTypeNode(matchingInputField.type).name
7783
.value;

test/unit/should_consolidate_input_and_output_types/expected.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@ enum class Enum2 {
128128
fun findByName(name: String, ignoreCase: Boolean = false): Enum2? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
129129
}
130130
}
131+
132+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
133+
data class MyNullabilityType(
134+
val field1: MyNestedNullabilityType
135+
)
136+
137+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
138+
data class MyNullabilityTypeInput(
139+
val field1: MyNestedNullabilityType? = null
140+
)
141+
142+
data class MyNestedNullabilityType(
143+
val field2: String
144+
)

test/unit/should_consolidate_input_and_output_types/schema.graphql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ input MySuperSetTypeInput {
131131
field3: Int
132132
}
133133

134+
# case where fields are different enum types
135+
134136
type MyTypeWithEnums {
135137
field1: [Enum1!]
136138
field2: [Enum2!]
@@ -149,3 +151,21 @@ enum Enum1 {
149151
enum Enum2 {
150152
THE_OTHER
151153
}
154+
155+
# case where fields have different nullability
156+
157+
type MyNullabilityType {
158+
field1: MyNestedNullabilityType!
159+
}
160+
161+
input MyNullabilityTypeInput {
162+
field1: MyNestedNullabilityTypeInput
163+
}
164+
165+
type MyNestedNullabilityType {
166+
field2: String!
167+
}
168+
169+
input MyNestedNullabilityTypeInput {
170+
field2: String!
171+
}

0 commit comments

Comments
 (0)