Skip to content

Commit

Permalink
feat: add nullableDataFetchingEnvironment config
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian committed Jun 14, 2024
1 parent a48c872 commit 2950564
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 45 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? = null";
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 @@ -70,7 +70,7 @@ data class MyTypeToConsolidateInputParent(

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

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.expediagroup.graphql.generator.annotations.*

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

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
Expand All @@ -15,8 +15,8 @@ open class HybridType(
private val nullableResolver: String? = null,
private val nonNullableResolver: String
) {
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = nullableResolver
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = nonNullableResolver
open fun nullableResolver(arg: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = nullableResolver
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = nonNullableResolver
}

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

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
Expand All @@ -42,6 +42,6 @@ open class TypeImplementingInterface(
private val nullableListResolver: List<String?>? = null,
private val nonNullableListResolver: List<String> = emptyList()
) : HybridInterface {
override fun nullableListResolver(arg1: Int?, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String?>? = nullableListResolver
override fun nonNullableListResolver(arg1: Int, arg2: Int?, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): List<String> = nonNullableListResolver
override fun nullableListResolver(arg1: Int?, arg2: Int, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String?>? = nullableListResolver
override fun nonNullableListResolver(arg1: Int, arg2: Int?, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String> = nonNullableListResolver
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class TypeWithReservedKotlinKeywordsAndFieldArgs(
val `typeof`: String? = null,
private val `throw`: String? = null
) {
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String? = `throw`
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = `throw`
}

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
Expand Down
16 changes: 8 additions & 8 deletions test/unit/should_handle_top_level_types_properly/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ import com.expediagroup.graphql.generator.annotations.*

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

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
open class GetStuffQueryInterface {
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Query.getStuff must be implemented.")
open fun getStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuff must be implemented.")
}

@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
open class GetStuffWithInputQueryInterface {
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
open fun getStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Query.getStuffWithInput must be implemented.")
}

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

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

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

0 comments on commit 2950564

Please sign in to comment.