@@ -11,17 +11,13 @@ See the License for the specific language governing permissions and
11
11
limitations under the License.
12
12
*/
13
13
14
- import {
15
- GraphQLSchema ,
16
- InputObjectTypeDefinitionNode ,
17
- Kind ,
18
- TypeNode ,
19
- } from "graphql" ;
14
+ import { GraphQLSchema , InputObjectTypeDefinitionNode } from "graphql" ;
20
15
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition" ;
21
16
import { buildTypeMetadata } from "../helpers/build-type-metadata" ;
22
17
import { buildAnnotations } from "../helpers/build-annotations" ;
23
18
import { indent } from "@graphql-codegen/visitor-plugin-common" ;
24
19
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults" ;
20
+ import { inputTypeHasMatchingOutputType } from "../helpers/input-type-has-matching-output-type" ;
25
21
26
22
export function buildInputObjectDefinition (
27
23
node : InputObjectTypeDefinitionNode ,
@@ -32,21 +28,8 @@ export function buildInputObjectDefinition(
32
28
return "" ;
33
29
}
34
30
35
- const typeNameWithoutInput = getTypeNameWithoutInput ( node . name . value ) ;
36
- const matchingType = schema . getType ( typeNameWithoutInput ) ?. astNode ;
37
- const matchingTypeFields =
38
- matchingType ?. kind === Kind . OBJECT_TYPE_DEFINITION
39
- ? matchingType . fields
40
- : [ ] ;
41
- const inputFields = node . fields ;
42
- const fieldsMatch = matchingTypeFields ?. every ( ( field ) => {
43
- const matchingInputField = inputFields ?. find (
44
- ( inputField ) => inputField . name . value === field . name . value ,
45
- ) ;
46
- if ( ! matchingInputField ) return false ;
47
- return fieldsAreEquivalent ( field . type , matchingInputField . type ) ;
48
- } ) ;
49
- if ( matchingTypeFields ?. length && fieldsMatch ) {
31
+ const typeWillBeConsolidated = inputTypeHasMatchingOutputType ( node , schema ) ;
32
+ if ( typeWillBeConsolidated ) {
50
33
return "" ;
51
34
}
52
35
@@ -77,30 +60,3 @@ export function buildInputObjectDefinition(
77
60
${ classMembers }
78
61
)` ;
79
62
}
80
-
81
- function getTypeNameWithoutInput ( name : string ) {
82
- return name . endsWith ( "Input" ) ? name . replace ( "Input" , "" ) : name ;
83
- }
84
-
85
- function fieldsAreEquivalent (
86
- typeField : TypeNode ,
87
- inputField : TypeNode ,
88
- ) : boolean {
89
- switch ( typeField . kind ) {
90
- case Kind . NAMED_TYPE :
91
- return (
92
- inputField . kind === Kind . NAMED_TYPE &&
93
- typeField . name . value === getTypeNameWithoutInput ( inputField . name . value )
94
- ) ;
95
- case Kind . LIST_TYPE :
96
- return (
97
- inputField . kind === Kind . LIST_TYPE &&
98
- fieldsAreEquivalent ( typeField . type , inputField . type )
99
- ) ;
100
- case Kind . NON_NULL_TYPE :
101
- return (
102
- inputField . kind === Kind . NON_NULL_TYPE &&
103
- fieldsAreEquivalent ( typeField . type , inputField . type )
104
- ) ;
105
- }
106
- }
0 commit comments