Skip to content

Commit 2514744

Browse files
committed
fix: make dataFetchingEnvironment nullable in resolver functions
1 parent 499886d commit 2514744

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
lines changed

src/definitions/field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function buildFieldArguments(
288288
return `${sanitizeName(arg.name.value)}: ${argMetadata.typeName}${arg.type.kind === Kind.NON_NULL_TYPE ? "" : nullableSuffix}`;
289289
});
290290
const dataFetchingEnvironmentArgument =
291-
"dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment";
291+
"dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null";
292292
const extraFieldArguments = [dataFetchingEnvironmentArgument];
293293
const allFieldArguments = existingFieldArguments?.concat(extraFieldArguments);
294294
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): String? = throw NotImplementedError("MyTypeToConsolidateParent2.field must be implemented.")
73+
open fun field(input: MyTypeToConsolidate, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): 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): 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.")
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.")
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): String? = nullableResolver
19-
open fun nonNullableResolver(arg: InputTypeForResolver, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = nonNullableResolver
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
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): List<String?>?
31-
fun nonNullableListResolver(arg1: Int, arg2: Int? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<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>
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): List<String?>? = nullableListResolver
46-
override fun nonNullableListResolver(arg1: Int, arg2: Int?, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): List<String> = nonNullableListResolver
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
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): String? = `throw`
17+
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): 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): 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.")
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.")
99
}
1010

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

2727
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
2828
open class MutateStuffMutationInterface {
29-
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String = throw NotImplementedError("Mutation.mutateStuff must be implemented.")
29+
open suspend fun mutateStuff(dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): 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): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
34+
open suspend fun mutateStuffWithInput(input: String, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment? = null): String = throw NotImplementedError("Mutation.mutateStuffWithInput must be implemented.")
3535
}

0 commit comments

Comments
 (0)