Skip to content

Commit

Permalink
feat: allow nullable dataFetchingEnvironment in resolver functions (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jun 15, 2024
1 parent 499886d commit 520cd61
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export const configSchema = object({
* interface functions to enforce a type contract.
*
* Type names can be optionally passed with the classMethods config to generate the interface with `suspend` functions or
* `java.util.concurrent.CompletableFuture` functions.
* `java.util.concurrent.CompletableFuture` functions. Pass `nullableDataFetchingEnvironment: true` to make the
* `DataFetchingEnvironment` argument nullable in each resolver function for that class.
* @example
* [
* {
Expand All @@ -125,6 +126,10 @@ export const configSchema = object({
* {
* typeName: "MyCompletableFutureResolverType",
* classMethods: "COMPLETABLE_FUTURE",
* },
* {
* typeName: "MyTypeWithNullableDataFetchingEnvironment",
* nullableDataFetchingEnvironment: true,
* }
* ]
* @link https://opensource.expediagroup.com/graphql-kotlin-codegen/docs/recommended-usage
Expand All @@ -136,6 +141,7 @@ export const configSchema = object({
classMethods: optional(
union([literal("SUSPEND"), literal("COMPLETABLE_FUTURE")]),
),
nullableDataFetchingEnvironment: optional(boolean()),
}),
),
),
Expand Down
3 changes: 1 addition & 2 deletions src/definitions/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ function buildFieldArguments(
const argMetadata = buildTypeMetadata(arg.type, schema, config);
return `${sanitizeName(arg.name.value)}: ${argMetadata.typeName}${arg.type.kind === Kind.NON_NULL_TYPE ? "" : nullableSuffix}`;
});
const dataFetchingEnvironmentArgument =
"dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment";
const dataFetchingEnvironmentArgument = `dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment${typeInResolverInterfacesConfig?.nullableDataFetchingEnvironment ? "? = null" : ""}`;
const extraFieldArguments = [dataFetchingEnvironmentArgument];
const allFieldArguments = existingFieldArguments?.concat(extraFieldArguments);
return allFieldArguments?.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export default {
typeName: "MyIncludedInterfaceSuspend",
classMethods: "SUSPEND",
},
{
typeName: "MyIncludedResolverTypeWithNullDataFetchingEnvironment",
nullableDataFetchingEnvironment: true,
},
],
} satisfies GraphQLKotlinCodegenConfig;
6 changes: 6 additions & 0 deletions test/unit/should_honor_resolverInterfaces_config/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ interface MyIncludedInterfaceSuspend {
interface MyExcludedInterface {
val field: String?
}

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
open class MyIncludedResolverTypeWithNullDataFetchingEnvironment {
open fun nullableField(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = throw NotImplementedError("MyIncludedResolverTypeWithNullDataFetchingEnvironment.nullableField must be implemented.")
open fun nonNullableField(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("MyIncludedResolverTypeWithNullDataFetchingEnvironment.nonNullableField must be implemented.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ interface MyIncludedInterfaceSuspend {
interface MyExcludedInterface {
field: String
}

type MyIncludedResolverTypeWithNullDataFetchingEnvironment {
nullableField: String
nonNullableField: String!
}

0 comments on commit 520cd61

Please sign in to comment.