File tree 5 files changed +35
-15
lines changed
test/unit/should_consolidate_input_and_output_types 5 files changed +35
-15
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,10 @@ import {
22
22
import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common" ;
23
23
import { wrapTypeWithModifiers } from "@graphql-codegen/java-common" ;
24
24
import { CodegenConfigWithDefaults } from "./build-config-with-defaults" ;
25
+ import {
26
+ getTypeNameWithoutInput ,
27
+ inputTypeHasMatchingOutputType ,
28
+ } from "./input-type-has-matching-output-type" ;
25
29
26
30
export interface TypeMetadata {
27
31
typeName : string ;
@@ -73,9 +77,16 @@ export function buildTypeMetadata(
73
77
) ,
74
78
} ;
75
79
} else {
80
+ const typeWillBeConsolidated =
81
+ schemaType ?. name . endsWith ( "Input" ) &&
82
+ schemaType ?. astNode ?. kind === Kind . INPUT_OBJECT_TYPE_DEFINITION &&
83
+ inputTypeHasMatchingOutputType ( schemaType . astNode , schema ) ;
84
+ const typeName = typeWillBeConsolidated
85
+ ? getTypeNameWithoutInput ( schemaType . name )
86
+ : schemaType . name ;
76
87
return {
77
88
...commonMetadata ,
78
- typeName : buildListType ( typeNode , schemaType . name ) ,
89
+ typeName : buildListType ( typeNode , typeName ) ,
79
90
} ;
80
91
}
81
92
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
19
19
TypeNode ,
20
20
} from "graphql" ;
21
21
import { CodegenConfigWithDefaults } from "./build-config-with-defaults" ;
22
+ import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common" ;
22
23
23
24
export function getDependentFieldTypeNames (
24
25
node : TypeDefinitionNode ,
@@ -36,19 +37,7 @@ export function getDependentFieldTypeNames(
36
37
}
37
38
38
39
export function getFieldTypeName ( fieldType : TypeNode ) {
39
- switch ( fieldType . kind ) {
40
- case Kind . NAMED_TYPE :
41
- return fieldType . name . value ;
42
- case Kind . LIST_TYPE :
43
- return getFieldTypeName ( fieldType . type ) ;
44
- case Kind . NON_NULL_TYPE :
45
- switch ( fieldType . type . kind ) {
46
- case Kind . NAMED_TYPE :
47
- return fieldType . type . name . value ;
48
- case Kind . LIST_TYPE :
49
- return getFieldTypeName ( fieldType . type . type ) ;
50
- }
51
- }
40
+ return getBaseTypeNode ( fieldType ) . name . value ;
52
41
}
53
42
54
43
export function getDependentInterfaceNames ( node : TypeDefinitionNode ) {
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ export function inputTypeHasMatchingOutputType(
24
24
return Boolean ( matchingTypeFields ?. length && fieldsMatch ) ;
25
25
}
26
26
27
- function getTypeNameWithoutInput ( name : string ) {
27
+ export function getTypeNameWithoutInput ( name : string ) {
28
28
return name . endsWith ( "Input" ) ? name . replace ( "Input" , "" ) : name ;
29
29
}
30
30
Original file line number Diff line number Diff line change @@ -57,3 +57,13 @@ data class MyTypeWhereFieldsDoNotMatchInput(
57
57
val field : String? = null ,
58
58
val field2 : Int? = null
59
59
)
60
+
61
+ @GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations .Locations .OBJECT ])
62
+ data class MyTypeToConsolidateParent (
63
+ val field : MyTypeToConsolidate ? = null
64
+ )
65
+
66
+ @GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations .Locations .INPUT_OBJECT ])
67
+ data class MyTypeToConsolidateInputParent (
68
+ val field : MyTypeToConsolidate ? = null
69
+ )
Original file line number Diff line number Diff line change @@ -84,3 +84,13 @@ input MyTypeWhereFieldsDoNotMatchInput {
84
84
field : String
85
85
field2 : Int
86
86
}
87
+
88
+ # case where parent types reference consolidated types
89
+
90
+ type MyTypeToConsolidateParent {
91
+ field : MyTypeToConsolidate
92
+ }
93
+
94
+ input MyTypeToConsolidateInputParent {
95
+ field : MyTypeToConsolidateInput
96
+ }
You can’t perform that action at this time.
0 commit comments