File tree Expand file tree Collapse file tree 5 files changed +35
-15
lines changed
test/unit/should_consolidate_input_and_output_types Expand file tree Collapse file tree 5 files changed +35
-15
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,10 @@ import {
2222import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common" ;
2323import { wrapTypeWithModifiers } from "@graphql-codegen/java-common" ;
2424import { CodegenConfigWithDefaults } from "./build-config-with-defaults" ;
25+ import {
26+ getTypeNameWithoutInput ,
27+ inputTypeHasMatchingOutputType ,
28+ } from "./input-type-has-matching-output-type" ;
2529
2630export interface TypeMetadata {
2731 typeName : string ;
@@ -73,9 +77,16 @@ export function buildTypeMetadata(
7377 ) ,
7478 } ;
7579 } 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 ;
7687 return {
7788 ...commonMetadata ,
78- typeName : buildListType ( typeNode , schemaType . name ) ,
89+ typeName : buildListType ( typeNode , typeName ) ,
7990 } ;
8091 }
8192}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
1919 TypeNode ,
2020} from "graphql" ;
2121import { CodegenConfigWithDefaults } from "./build-config-with-defaults" ;
22+ import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common" ;
2223
2324export function getDependentFieldTypeNames (
2425 node : TypeDefinitionNode ,
@@ -36,19 +37,7 @@ export function getDependentFieldTypeNames(
3637}
3738
3839export 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 ;
5241}
5342
5443export function getDependentInterfaceNames ( node : TypeDefinitionNode ) {
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ export function inputTypeHasMatchingOutputType(
2424 return Boolean ( matchingTypeFields ?. length && fieldsMatch ) ;
2525}
2626
27- function getTypeNameWithoutInput ( name : string ) {
27+ export function getTypeNameWithoutInput ( name : string ) {
2828 return name . endsWith ( "Input" ) ? name . replace ( "Input" , "" ) : name ;
2929}
3030
Original file line number Diff line number Diff line change @@ -57,3 +57,13 @@ data class MyTypeWhereFieldsDoNotMatchInput(
5757 val field : String? = null ,
5858 val field2 : Int? = null
5959)
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 {
8484 field : String
8585 field2 : Int
8686}
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