Skip to content

Commit

Permalink
fix: consolidation nullability (#108)
Browse files Browse the repository at this point in the history
* unit test

* make test pass
  • Loading branch information
danadajian authored Aug 22, 2024
1 parent e92c452 commit 75895a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/utils/should-consolidate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ function typesAreEquivalent(
const matchingInputField = inputNode.astNode?.fields?.find(
(inputField) => inputField.name.value === typeField.name.value,
);
if (!matchingInputField?.type) return false;
if (
!matchingInputField?.type ||
typeField.type.kind !== matchingInputField.type.kind
) {
return false;
}

const baseTypeName = getBaseTypeNode(typeField.type).name.value;
const baseInputTypeName = getBaseTypeNode(matchingInputField.type).name
.value;
Expand Down
14 changes: 14 additions & 0 deletions test/unit/should_consolidate_input_and_output_types/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ enum class Enum2 {
fun findByName(name: String, ignoreCase: Boolean = false): Enum2? = values().find { it.name.equals(name, ignoreCase = ignoreCase) }
}
}

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
data class MyNullabilityType(
val field1: MyNestedNullabilityType
)

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
data class MyNullabilityTypeInput(
val field1: MyNestedNullabilityType? = null
)

data class MyNestedNullabilityType(
val field2: String
)
20 changes: 20 additions & 0 deletions test/unit/should_consolidate_input_and_output_types/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ input MySuperSetTypeInput {
field3: Int
}

# case where fields are different enum types

type MyTypeWithEnums {
field1: [Enum1!]
field2: [Enum2!]
Expand All @@ -149,3 +151,21 @@ enum Enum1 {
enum Enum2 {
THE_OTHER
}

# case where fields have different nullability

type MyNullabilityType {
field1: MyNestedNullabilityType!
}

input MyNullabilityTypeInput {
field1: MyNestedNullabilityTypeInput
}

type MyNestedNullabilityType {
field2: String!
}

input MyNestedNullabilityTypeInput {
field2: String!
}

0 comments on commit 75895a1

Please sign in to comment.