Skip to content

Commit 520cd61

Browse files
authored
feat: allow nullable dataFetchingEnvironment in resolver functions (#86)
1 parent 499886d commit 520cd61

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
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";
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_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;

test/unit/should_honor_resolverInterfaces_config/expected.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ interface MyIncludedInterfaceSuspend {
6363
interface MyExcludedInterface {
6464
val field: String?
6565
}
66+
67+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
68+
open class MyIncludedResolverTypeWithNullDataFetchingEnvironment {
69+
open fun nullableField(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = throw NotImplementedError("MyIncludedResolverTypeWithNullDataFetchingEnvironment.nullableField must be implemented.")
70+
open fun nonNullableField(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("MyIncludedResolverTypeWithNullDataFetchingEnvironment.nonNullableField must be implemented.")
71+
}

test/unit/should_honor_resolverInterfaces_config/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ interface MyIncludedInterfaceSuspend {
5151
interface MyExcludedInterface {
5252
field: String
5353
}
54+
55+
type MyIncludedResolverTypeWithNullDataFetchingEnvironment {
56+
nullableField: String
57+
nonNullableField: String!
58+
}

0 commit comments

Comments
 (0)