From 4e51086f660a169c7ba4a830df6a7e9f5e753e64 Mon Sep 17 00:00:00 2001 From: Artyom Emelyanenko Date: Mon, 17 Jun 2024 12:53:17 +1000 Subject: [PATCH] add graphql long to wiring factory (#550) --- .../nadel/schema/NeverWiringFactory.kt | 76 +++++++++++++------ ...ng-scalar-argument-with-default-values.yml | 45 +++++++++++ ...hrough-and-can-be-anything-technically.yml | 8 +- 3 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 test/src/test/resources/fixtures/scalars/long-scalar-argument-with-default-values.yml diff --git a/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt b/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt index 70739f9c0..69042d283 100644 --- a/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt +++ b/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt @@ -1,6 +1,9 @@ package graphql.nadel.schema import graphql.Assert.assertShouldNeverHappen +import graphql.GraphQLContext +import graphql.execution.CoercedVariables +import graphql.language.Value import graphql.scalars.ExtendedScalars import graphql.schema.Coercing import graphql.schema.DataFetcher @@ -12,9 +15,10 @@ import graphql.schema.idl.ScalarInfo import graphql.schema.idl.ScalarWiringEnvironment import graphql.schema.idl.UnionWiringEnvironment import graphql.schema.idl.WiringFactory +import java.util.Locale /** - * This wiring factory is designed to be NEVER called and will assert if it ever is. Nadel + * This wiring factory is designed to be NEVER called and will assert if it ever is. Nadel * uses this for the overall schema and also in part for the underlying schema by default. */ open class NeverWiringFactory : WiringFactory { @@ -24,31 +28,55 @@ open class NeverWiringFactory : WiringFactory { } override fun getScalar(environment: ScalarWiringEnvironment): GraphQLScalarType? { - val scalarName = environment.scalarTypeDefinition.name - if (scalarName == ExtendedScalars.Json.name) { - return ExtendedScalars.Json - } - return GraphQLScalarType - .newScalar() - .name(scalarName) - .definition(environment.scalarTypeDefinition) - .coercing(object : Coercing { - override fun serialize(dataFetcherResult: Any): Any? { - return assertShouldNeverHappen("This %s scalar coercing should NEVER be called from Nadel", - scalarName) - } + return when (val scalarName = environment.scalarTypeDefinition.name) { + ExtendedScalars.Json.name -> { + ExtendedScalars.Json + } + ExtendedScalars.GraphQLLong.name -> { + ExtendedScalars.GraphQLLong + } + else -> { + GraphQLScalarType.newScalar() + .name(scalarName) + .definition(environment.scalarTypeDefinition) + .coercing(object : Coercing { + override fun serialize( + dataFetcherResult: Any, + graphQLContext: GraphQLContext, + locale: Locale, + ): Any? { + return assertShouldNeverHappen( + "This %s scalar coercing should NEVER be called from Nadel", + scalarName + ) + } - override fun parseValue(input: Any): Any { - return assertShouldNeverHappen("This %s scalar coercing should NEVER be called from Nadel", - scalarName) - } + override fun parseValue( + input: Any, + graphQLContext: GraphQLContext, + locale: Locale, + ): Any? { + return assertShouldNeverHappen( + "This %s scalar coercing should NEVER be called from Nadel", + scalarName + ) + } - override fun parseLiteral(input: Any): Any { - return assertShouldNeverHappen("This %s scalar coercing should NEVER be called from Nadel", - scalarName) - } - }) - .build() + override fun parseLiteral( + input: Value<*>, + variables: CoercedVariables, + graphQLContext: GraphQLContext, + locale: Locale, + ): Any? { + return assertShouldNeverHappen( + "This %s scalar coercing should NEVER be called from Nadel", + scalarName + ) + } + }) + .build() + } + } } override fun providesTypeResolver(environment: InterfaceWiringEnvironment): Boolean { diff --git a/test/src/test/resources/fixtures/scalars/long-scalar-argument-with-default-values.yml b/test/src/test/resources/fixtures/scalars/long-scalar-argument-with-default-values.yml new file mode 100644 index 000000000..3f4e40d45 --- /dev/null +++ b/test/src/test/resources/fixtures/scalars/long-scalar-argument-with-default-values.yml @@ -0,0 +1,45 @@ +name: "long scalar argument with default values" +enabled: true +# language=GraphQL +overallSchema: + service: | + type Query { + getFoo(arg: Long = 123): String + } + scalar Long +# language=GraphQL +underlyingSchema: + service: | + type Query { + getFoo(arg: Long = 123): String + } + scalar Long +query: | + query { + getFoo(arg: 15) + } +variables: { } +serviceCalls: + - serviceName: "service" + request: + # language=GraphQL + query: | + query { + getFoo(arg: 15) + } + variables: { } + # language=JSON + response: |- + { + "data": { + "getFoo": "KFC Good" + }, + "extensions": {} + } +# language=JSON +response: |- + { + "data": { + "getFoo": "KFC Good" + } + } diff --git a/test/src/test/resources/fixtures/scalars/long-scalar-is-passthrough-and-can-be-anything-technically.yml b/test/src/test/resources/fixtures/scalars/long-scalar-is-passthrough-and-can-be-anything-technically.yml index e24e6be37..d5ad3019e 100644 --- a/test/src/test/resources/fixtures/scalars/long-scalar-is-passthrough-and-can-be-anything-technically.yml +++ b/test/src/test/resources/fixtures/scalars/long-scalar-is-passthrough-and-can-be-anything-technically.yml @@ -4,16 +4,16 @@ enabled: true overallSchema: service: | type Query { - aField: URL + aField: Long } - scalar URL + scalar Long # language=GraphQL underlyingSchema: service: | type Query { - aField: URL + aField: Long } - scalar URL + scalar Long query: | query { aField