@@ -28,6 +28,7 @@ import { buildFieldDefinition } from "../helpers/build-field-definition";
28
28
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults" ;
29
29
import { inputTypeHasMatchingOutputType } from "../helpers/input-type-has-matching-output-type" ;
30
30
import { findTypeInResolverInterfacesConfig } from "../helpers/findTypeInResolverInterfacesConfig" ;
31
+ import { titleCase } from "title-case" ;
31
32
32
33
export function buildObjectTypeDefinition (
33
34
node : ObjectTypeDefinitionNode ,
@@ -64,10 +65,32 @@ export function buildObjectTypeDefinition(
64
65
node ,
65
66
config ,
66
67
) ;
68
+ const fieldsWithArguments = node . fields ?. filter (
69
+ ( fieldNode ) => fieldNode . arguments ?. length ,
70
+ ) ;
71
+ const fieldNodes = typeInResolverInterfacesConfig
72
+ ? node . fields
73
+ : fieldsWithArguments ;
67
74
const shouldGenerateFunctions = Boolean (
68
75
typeInResolverInterfacesConfig ||
69
76
node . fields ?. some ( ( fieldNode ) => fieldNode . arguments ?. length ) ,
70
77
) ;
78
+
79
+ if ( node . name . value === "Query" || node . name . value === "Mutation" ) {
80
+ const individualQueryClasses = node . fields ?. map ( ( fieldNode ) => {
81
+ const className = `${ titleCase ( fieldNode . name . value ) } Query` ;
82
+ return `${ annotations } ${ outputRestrictionAnnotation } open class ${ className } ${ interfaceInheritance } {
83
+ ${ getDataClassMembers ( { node, fieldNodes : [ fieldNode ] , schema, config, shouldGenerateFunctions } ) }
84
+ }` ;
85
+ } ) ;
86
+ const consolidatedQueryClass = `${ annotations } ${ outputRestrictionAnnotation } open class ${ name } ${ interfaceInheritance } {
87
+ ${ getDataClassMembers ( { node, fieldNodes, schema, config, shouldGenerateFunctions } ) }
88
+ }` ;
89
+ return [ consolidatedQueryClass , ...( individualQueryClasses ?? [ ] ) ] . join (
90
+ "\n\n" ,
91
+ ) ;
92
+ }
93
+
71
94
if ( shouldGenerateFunctions ) {
72
95
const atLeastOneFieldHasNoArguments = node . fields ?. some (
73
96
( fieldNode ) => ! fieldNode . arguments ?. length ,
@@ -94,12 +117,6 @@ export function buildObjectTypeDefinition(
94
117
. join ( ",\n" ) } \n)`
95
118
: "" ;
96
119
97
- const fieldsWithArguments = node . fields ?. filter (
98
- ( fieldNode ) => fieldNode . arguments ?. length ,
99
- ) ;
100
- const fieldNodes = typeInResolverInterfacesConfig
101
- ? node . fields
102
- : fieldsWithArguments ;
103
120
return `${ annotations } ${ outputRestrictionAnnotation } open class ${ name } ${ constructor } ${ interfaceInheritance } {
104
121
${ getDataClassMembers ( { node, fieldNodes, schema, config, shouldGenerateFunctions } ) }
105
122
}` ;
0 commit comments