Skip to content

Commit e4bde68

Browse files
committed
add mutation tests
1 parent ed45fb4 commit e4bde68

File tree

7 files changed

+31
-5
lines changed

7 files changed

+31
-5
lines changed

src/definitions/object.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
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";
30-
import { findTypeInResolverInterfacesConfig } from "../helpers/findTypeInResolverInterfacesConfig";
30+
import { findTypeInResolverInterfacesConfig } from "../helpers/find-type-in-resolver-interfaces-config";
3131
import { titleCase } from "title-case";
3232

3333
export function buildObjectTypeDefinition(
@@ -78,7 +78,7 @@ export function buildObjectTypeDefinition(
7878

7979
if (node.name.value === "Query" || node.name.value === "Mutation") {
8080
const individualQueryClasses = node.fields?.map((fieldNode) => {
81-
const className = `${titleCase(fieldNode.name.value)}Query`;
81+
const className = `${titleCase(fieldNode.name.value)}${node.name.value}`;
8282
return `${annotations}${outputRestrictionAnnotation}open class ${className}${interfaceInheritance} {
8383
${getDataClassMembers({ node, fieldNodes: [fieldNode], schema, config, shouldGenerateFunctions })}
8484
}`;

src/helpers/build-config-with-defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function buildConfigWithDefaults(
1212
includeDependentTypes: true,
1313
unionGeneration: "MARKER_INTERFACE",
1414
extraImports: ["com.expediagroup.graphql.generator.annotations.*"],
15-
resolverInterfaces: [{ typeName: "Query" }],
15+
resolverInterfaces: [{ typeName: "Query" }, { typeName: "Mutation" }],
1616
} as const satisfies GraphQLKotlinCodegenConfig;
1717

1818
return merge(defaultConfig, config) as GraphQLKotlinCodegenConfig &

src/helpers/build-field-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
2424
import { indent } from "@graphql-codegen/visitor-plugin-common";
2525
import { buildAnnotations } from "./build-annotations";
26-
import { findTypeInResolverInterfacesConfig } from "./findTypeInResolverInterfacesConfig";
26+
import { findTypeInResolverInterfacesConfig } from "./find-type-in-resolver-interfaces-config";
2727

2828
export function buildFieldDefinition(
2929
node: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,

src/helpers/findTypeInResolverInterfacesConfig.ts renamed to src/helpers/find-type-in-resolver-interfaces-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function findTypeInResolverInterfacesConfig(
55
node: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
66
config: CodegenConfigWithDefaults,
77
) {
8-
return config.resolverInterfaces.find(
8+
return config.resolverInterfaces.findLast(
99
(resolverInterface) => resolverInterface.typeName === node.name.value,
1010
);
1111
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { GraphQLKotlinCodegenConfig } from "../../../src/plugin";
2+
3+
export default {
4+
resolverInterfaces: [{ typeName: "Mutation", classMethods: "SUSPEND" }],
5+
} satisfies GraphQLKotlinCodegenConfig;

test/unit/should_handle_query_type_properly/expected.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ open class GetStuffQuery {
1717
open class GetStuffWithInputQuery {
1818
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
1919
}
20+
21+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
22+
open class Mutation {
23+
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
24+
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
25+
}
26+
27+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
28+
open class MutateStuffMutation {
29+
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
30+
}
31+
32+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
33+
open class MutateStuffWithInputMutation {
34+
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
35+
}

test/unit/should_handle_query_type_properly/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ type Query {
22
getStuff: String!
33
getStuffWithInput(input: String!): String!
44
}
5+
6+
type Mutation {
7+
mutateStuff: String!
8+
mutateStuffWithInput(input: String!): String!
9+
}

0 commit comments

Comments
 (0)