Skip to content

Commit 9a40a3d

Browse files
authored
fix: add graphql valid object annotation to all input and output types (#45)
* failing test * pass test * refactor * improve test cases * fix: include GraphQLValidObject annotation on all input and output types
1 parent 9c5747a commit 9a40a3d

File tree

27 files changed

+68
-2
lines changed

27 files changed

+68
-2
lines changed

src/definitions/input.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export function buildInputObjectDefinition(
5656
definitionNode: node,
5757
});
5858

59-
return `${annotations}data class ${node.name.value}(
59+
const inputRestrictionAnnotation =
60+
"@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])\n";
61+
return `${annotations}${inputRestrictionAnnotation}data class ${node.name.value}(
6062
${classMembers}
6163
)`;
6264
}

src/definitions/object.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { isResolverType } from "../helpers/is-resolver-type";
2424
import { buildFieldDefinition } from "../helpers/build-field-definition";
2525
import { isExternalField } from "../helpers/is-external-field";
2626
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults";
27+
import { inputTypeHasMatchingOutputType } from "../helpers/input-type-has-matching-output-type";
2728

2829
export function buildObjectTypeDefinition(
2930
node: ObjectTypeDefinitionNode,
@@ -57,7 +58,14 @@ ${getDataClassMembers({ node, schema, config, completableFuture: true })}
5758
}`;
5859
}
5960

60-
return `${annotations}data class ${name}(
61+
const potentialMatchingInputType = schema.getType(`${name}Input`)?.astNode;
62+
const typeWillBeConsolidated =
63+
potentialMatchingInputType?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION &&
64+
inputTypeHasMatchingOutputType(potentialMatchingInputType, schema);
65+
const outputRestrictionAnnotation = typeWillBeConsolidated
66+
? ""
67+
: "@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])\n";
68+
return `${annotations}${outputRestrictionAnnotation}data class ${name}(
6169
${getDataClassMembers({ node, schema, config })}
6270
)${interfaceInheritance}`;
6371
}

test/unit/should_annotate_types_properly/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.kotlin.generated
33
import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLDescription("A description for MyType")
6+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
67
data class TypeThatShouldBeProperlyAnnotated(
78
val field: String? = null,
89
@GraphQLDescription("A description for fieldWithDescription")

test/unit/should_consolidate_input_and_output_types/expected.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,34 @@ data class MyTypeToConsolidate4(
2525
val field: String? = null
2626
)
2727

28+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
2829
data class MyTypeNotToConsolidate(
2930
val field: String? = null
3031
)
3132

3233
@GraphQLDescription("The type name must exactly match in order to consolidate")
34+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
3335
data class MyTypeToNotConsolidateInput(
3436
val field: String? = null
3537
)
3638

39+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
3740
data class MyTypeToNotConsolidate2(
3841
val field: String? = null
3942
)
4043

44+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
4145
data class MyTypeInputToNotConsolidate2(
4246
val field: String? = null
4347
)
4448

49+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
4550
data class MyTypeWhereFieldsDoNotMatch(
4651
val field: String? = null,
4752
val field2: String? = null
4853
)
4954

55+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
5056
data class MyTypeWhereFieldsDoNotMatchInput(
5157
val field: String? = null,
5258
val field2: Int? = null

test/unit/should_convert_graphql_float_to_kotlin_double/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kotlin.generated
22

33
import com.expediagroup.graphql.generator.annotations.*
44

5+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
56
data class MyFloatType(
67
val field: Double? = null
78
)

test/unit/should_default_non_nullable_boolean_fields_to_false/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kotlin.generated
22

33
import com.expediagroup.graphql.generator.annotations.*
44

5+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
56
data class MyBooleanType(
67
val field: Boolean = false,
78
val field2: Boolean? = null

test/unit/should_generate_field_resolver_interfaces/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface QueryCompletableFuture {
1818
fun nonNullableResolver(arg: InputTypeGenerateFieldResolverInterfaces): java.util.concurrent.CompletableFuture<String>
1919
}
2020

21+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
2122
data class InputTypeGenerateFieldResolverInterfaces(
2223
val field: String? = null
2324
)

test/unit/should_generate_input_types_properly/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.kotlin.generated
33
import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLDescription("A description for MyInputType")
6+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
67
data class InputTypeThatShouldBeGeneratedProperly(
78
val field1: String? = null,
89
@GraphQLDescription("A description for field2")

test/unit/should_generate_interfaces_with_inheritance/expected.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface InterfaceWithInheritance {
99
}
1010

1111
@GraphQLDescription("A description for MyInterfaceImplementation")
12+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
1213
data class MyInterfaceImplementation(
1314
override val field: String? = null,
1415
override val field2: String
@@ -22,6 +23,7 @@ interface InheritedInterface2 {
2223
val field2: String
2324
}
2425

26+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
2527
data class MyMergedInterfaceImplementation(
2628
override val field: String? = null,
2729
override val field2: String

test/unit/should_generate_list_types_properly/expected.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kotlin.generated
22

33
import com.expediagroup.graphql.generator.annotations.*
44

5+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
56
data class MyListType(
67
val field: List<String> = emptyList(),
78
val field2: List<String?> = emptyList(),

0 commit comments

Comments
 (0)