Skip to content

Commit 2950564

Browse files
committed
feat: add nullableDataFetchingEnvironment config
1 parent a48c872 commit 2950564

File tree

10 files changed

+65
-45
lines changed

10 files changed

+65
-45
lines changed

src/config/schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export const configSchema = object({
112112
* interface functions to enforce a type contract.
113113
*
114114
* Type names can be optionally passed with the classMethods config to generate the interface with `suspend` functions or
115-
* `java.util.concurrent.CompletableFuture` functions.
115+
* `java.util.concurrent.CompletableFuture` functions. Pass `nullableDataFetchingEnvironment: true` to make the
116+
* `DataFetchingEnvironment` argument nullable in each resolver function for that class.
116117
* @example
117118
* [
118119
* {
@@ -125,6 +126,10 @@ export const configSchema = object({
125126
* {
126127
* typeName: "MyCompletableFutureResolverType",
127128
* classMethods: "COMPLETABLE_FUTURE",
129+
* },
130+
* {
131+
* typeName: "MyTypeWithNullableDataFetchingEnvironment",
132+
* nullableDataFetchingEnvironment: true,
128133
* }
129134
* ]
130135
* @link https://opensource.expediagroup.com/graphql-kotlin-codegen/docs/recommended-usage
@@ -136,6 +141,7 @@ export const configSchema = object({
136141
classMethods: optional(
137142
union([literal("SUSPEND"), literal("COMPLETABLE_FUTURE")]),
138143
),
144+
nullableDataFetchingEnvironment: optional(boolean()),
139145
}),
140146
),
141147
),

src/definitions/field.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ function buildFieldArguments(
287287
const argMetadata = buildTypeMetadata(arg.type, schema, config);
288288
return `${sanitizeName(arg.name.value)}: ${argMetadata.typeName}${arg.type.kind === Kind.NON_NULL_TYPE ? "" : nullableSuffix}`;
289289
});
290-
const dataFetchingEnvironmentArgument =
291-
"dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null";
290+
const dataFetchingEnvironmentArgument = `dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment${typeInResolverInterfacesConfig?.nullableDataFetchingEnvironment ? "? = null" : ""}`;
292291
const extraFieldArguments = [dataFetchingEnvironmentArgument];
293292
const allFieldArguments = existingFieldArguments?.concat(extraFieldArguments);
294293
return allFieldArguments?.length

test/unit/should_consolidate_input_and_output_types/expected.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ data class MyTypeToConsolidateInputParent(
7070

7171
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
7272
open class MyTypeToConsolidateParent2 {
73-
open fun field(input: MyTypeToConsolidate, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = throw NotImplementedError("MyTypeToConsolidateParent2.field must be implemented.")
73+
open fun field(input: MyTypeToConsolidate, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = throw NotImplementedError("MyTypeToConsolidateParent2.field must be implemented.")
7474
}
7575

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

test/unit/should_generate_classes_for_types_with_field_args/expected.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
66
open class TypeWithOnlyFieldArgs {
7-
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = throw NotImplementedError("TypeWithOnlyFieldArgs.nullableResolver must be implemented.")
8-
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("TypeWithOnlyFieldArgs.nonNullableResolver must be implemented.")
7+
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = throw NotImplementedError("TypeWithOnlyFieldArgs.nullableResolver must be implemented.")
8+
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("TypeWithOnlyFieldArgs.nonNullableResolver must be implemented.")
99
}
1010

1111
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
@@ -15,8 +15,8 @@ open class HybridType(
1515
private val nullableResolver: String? = null,
1616
private val nonNullableResolver: String
1717
) {
18-
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = nullableResolver
19-
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = nonNullableResolver
18+
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = nullableResolver
19+
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = nonNullableResolver
2020
}
2121

2222
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT])
@@ -27,8 +27,8 @@ data class InputTypeForResolver(
2727
interface HybridInterface {
2828
val field1: String?
2929
val field2: String
30-
fun nullableListResolver(arg1: Int? = null, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String?>?
31-
fun nonNullableListResolver(arg1: Int, arg2: Int? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String>
30+
fun nullableListResolver(arg1: Int? = null, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String?>?
31+
fun nonNullableListResolver(arg1: Int, arg2: Int? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String>
3232
}
3333

3434
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
@@ -42,6 +42,6 @@ open class TypeImplementingInterface(
4242
private val nullableListResolver: List<String?>? = null,
4343
private val nonNullableListResolver: List<String> = emptyList()
4444
) : HybridInterface {
45-
override fun nullableListResolver(arg1: Int?, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String?>? = nullableListResolver
46-
override fun nonNullableListResolver(arg1: Int, arg2: Int?, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String> = nonNullableListResolver
45+
override fun nullableListResolver(arg1: Int?, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String?>? = nullableListResolver
46+
override fun nonNullableListResolver(arg1: Int, arg2: Int?, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String> = nonNullableListResolver
4747
}

test/unit/should_handle_reserved_kotlin_keywords/expected.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class TypeWithReservedKotlinKeywordsAndFieldArgs(
1414
val `typeof`: String? = null,
1515
private val `throw`: String? = null
1616
) {
17-
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = `throw`
17+
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = `throw`
1818
}
1919

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

test/unit/should_handle_top_level_types_properly/expected.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import com.expediagroup.graphql.generator.annotations.*
44

55
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
66
open class Query {
7-
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Query.getStuff must be implemented.")
8-
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
7+
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuff must be implemented.")
8+
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
99
}
1010

1111
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
1212
open class GetStuffQueryInterface {
13-
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): 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])
1717
open class GetStuffWithInputQueryInterface {
18-
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
18+
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
1919
}
2020

2121
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
2222
open class Mutation {
23-
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
24-
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
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.")
2525
}
2626

2727
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
2828
open class MutateStuffMutationInterface {
29-
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
29+
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
3030
}
3131

3232
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
3333
open class MutateStuffWithInputMutationInterface {
34-
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
34+
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
3535
}

test/unit/should_honor_resolverInterfaces_config/codegen.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ export default {
2323
typeName: "MyIncludedInterfaceSuspend",
2424
classMethods: "SUSPEND",
2525
},
26+
{
27+
typeName: "MyIncludedResolverTypeWithNullDataFetchingEnvironment",
28+
nullableDataFetchingEnvironment: true,
29+
},
2630
],
2731
} satisfies GraphQLKotlinCodegenConfig;

0 commit comments

Comments
 (0)