Skip to content

Commit ed45fb4

Browse files
committed
pass test
1 parent 92a0212 commit ed45fb4

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

bun.lockb

412 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@graphql-codegen/java-common": "3.0.0",
2323
"@graphql-codegen/plugin-helpers": "5.0.4",
2424
"@graphql-codegen/visitor-plugin-common": "5.2.0",
25+
"title-case": "4.3.1",
2526
"ts-deepmerge": "7.0.0",
2627
"valibot": "0.30.0"
2728
},

src/definitions/object.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { buildFieldDefinition } from "../helpers/build-field-definition";
2828
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults";
2929
import { inputTypeHasMatchingOutputType } from "../helpers/input-type-has-matching-output-type";
3030
import { findTypeInResolverInterfacesConfig } from "../helpers/findTypeInResolverInterfacesConfig";
31+
import { titleCase } from "title-case";
3132

3233
export function buildObjectTypeDefinition(
3334
node: ObjectTypeDefinitionNode,
@@ -64,10 +65,32 @@ export function buildObjectTypeDefinition(
6465
node,
6566
config,
6667
);
68+
const fieldsWithArguments = node.fields?.filter(
69+
(fieldNode) => fieldNode.arguments?.length,
70+
);
71+
const fieldNodes = typeInResolverInterfacesConfig
72+
? node.fields
73+
: fieldsWithArguments;
6774
const shouldGenerateFunctions = Boolean(
6875
typeInResolverInterfacesConfig ||
6976
node.fields?.some((fieldNode) => fieldNode.arguments?.length),
7077
);
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+
7194
if (shouldGenerateFunctions) {
7295
const atLeastOneFieldHasNoArguments = node.fields?.some(
7396
(fieldNode) => !fieldNode.arguments?.length,
@@ -94,12 +117,6 @@ export function buildObjectTypeDefinition(
94117
.join(",\n")}\n)`
95118
: "";
96119

97-
const fieldsWithArguments = node.fields?.filter(
98-
(fieldNode) => fieldNode.arguments?.length,
99-
);
100-
const fieldNodes = typeInResolverInterfacesConfig
101-
? node.fields
102-
: fieldsWithArguments;
103120
return `${annotations}${outputRestrictionAnnotation}open class ${name}${constructor}${interfaceInheritance} {
104121
${getDataClassMembers({ node, fieldNodes, schema, config, shouldGenerateFunctions })}
105122
}`;

test/unit/should_handle_query_type_properly/expected.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ open class Query {
1010

1111
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
1212
open class GetStuffQuery {
13-
open fun getStuff(): String = throw NotImplementedError("Query.getStuff must be implemented.")
13+
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuff must be implemented.")
1414
}
1515

1616
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])

0 commit comments

Comments
 (0)