diff --git a/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt b/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt index 7b030c08c..2f609441e 100644 --- a/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt +++ b/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt @@ -11,6 +11,7 @@ import graphql.language.DirectiveDefinition.newDirectiveDefinition import graphql.language.EnumTypeDefinition.newEnumTypeDefinition import graphql.language.EnumValueDefinition.newEnumValueDefinition import graphql.language.InputObjectTypeDefinition.newInputObjectDefinition +import graphql.language.ObjectValue import graphql.language.StringValue import graphql.language.Value import graphql.nadel.dsl.FieldMappingDefinition @@ -33,7 +34,6 @@ import graphql.nadel.util.onInterface import graphql.nadel.util.onObject import graphql.nadel.util.onScalar import graphql.nadel.util.onUnion -import graphql.scalars.`object`.JsonScalar import graphql.schema.GraphQLAppliedDirective import graphql.schema.GraphQLAppliedDirectiveArgument import graphql.schema.GraphQLDirectiveContainer @@ -41,7 +41,6 @@ import graphql.schema.GraphQLEnumType import graphql.schema.GraphQLFieldDefinition import graphql.schema.GraphQLSchema import java.util.Locale -import kotlin.reflect.typeOf /** * If you update this file please add to NadelBuiltInTypes @@ -255,8 +254,7 @@ object NadelDirectives { val hydrations = fieldDefinition.getAppliedDirectives(hydratedDirectiveDefinition.name) .asSequence() .map { directive -> - val argumentValues = resolveArgumentValue>(directive.getArgument("arguments")) - val arguments = createRemoteArgs(argumentValues) + val arguments = createRemoteArgs(directive.getArgument("arguments").argumentValue.value as ArrayValue) val inputIdentifiedBy = directive.getArgument("inputIdentifiedBy") val identifiedByValues = resolveArgumentValue>(inputIdentifiedBy) @@ -334,20 +332,20 @@ object NadelDirectives { ) } - private fun createRemoteArgs(arguments: List): List { + private fun createRemoteArgs(arguments: ArrayValue): List { fun Map.requireArgument(key: String): String { return requireNotNull(this[key]) { "${nadelHydrationArgumentDefinition.name} definition requires '$key' to be not-null" } } - return arguments + return arguments.values .map { arg -> @Suppress("UNCHECKED_CAST") // trust GraphQL type system and caller - val argMap = arg as Map - val remoteArgName = argMap.requireArgument("name") - val remoteArgValue = argMap.requireArgument("value") - val remoteArgumentSource = createRemoteArgumentSource(StringValue(remoteArgValue)) + val argMap = arg as ObjectValue + val remoteArgName = (argMap.objectFields.single { it.name == "name" }.value as StringValue).value + val remoteArgValue = argMap.objectFields.single { it.name == "value" }.value + val remoteArgumentSource = createRemoteArgumentSource(remoteArgValue) RemoteArgumentDefinition(remoteArgName, remoteArgumentSource) } } diff --git a/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt b/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt index 2bf4d5add..d8522616d 100644 --- a/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt +++ b/lib/src/main/java/graphql/nadel/schema/NeverWiringFactory.kt @@ -1,6 +1,7 @@ package graphql.nadel.schema import graphql.Assert.assertShouldNeverHappen +import graphql.scalars.ExtendedScalars import graphql.schema.Coercing import graphql.schema.DataFetcher import graphql.schema.GraphQLScalarType @@ -24,6 +25,9 @@ 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) diff --git a/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt b/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt index ffc49aef8..29bd4be9f 100644 --- a/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt +++ b/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt @@ -4,6 +4,7 @@ import graphql.GraphQLException import graphql.language.FieldDefinition import graphql.language.ObjectTypeDefinition import graphql.language.ObjectTypeDefinition.newObjectTypeDefinition +import graphql.language.ScalarTypeDefinition.newScalarTypeDefinition import graphql.language.SchemaDefinition import graphql.language.SourceLocation import graphql.nadel.NadelDefinitionRegistry @@ -12,6 +13,7 @@ import graphql.nadel.util.AnyNamedNode import graphql.nadel.util.AnySDLDefinition import graphql.nadel.util.AnySDLNamedDefinition import graphql.nadel.util.isExtensionDef +import graphql.scalars.ExtendedScalars import graphql.schema.GraphQLSchema import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaGenerator @@ -70,6 +72,9 @@ internal class OverallSchemaGenerator { addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelHydrationTemplateEnumDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hydratedFromDirectiveDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hydratedTemplateDirectiveDefinition) + addIfNotPresent(overallRegistry, allDefinitions, newScalarTypeDefinition() + .name(ExtendedScalars.Json.name) + .build()) for (definition in allDefinitions) { val error = overallRegistry.add(definition) diff --git a/lib/src/test/kotlin/graphql/nadel/schema/NadelDirectivesTest.kt b/lib/src/test/kotlin/graphql/nadel/schema/NadelDirectivesTest.kt index 32a43ace2..f057b25b7 100644 --- a/lib/src/test/kotlin/graphql/nadel/schema/NadelDirectivesTest.kt +++ b/lib/src/test/kotlin/graphql/nadel/schema/NadelDirectivesTest.kt @@ -10,7 +10,8 @@ import graphql.nadel.schema.NadelDirectives.nadelHydrationComplexIdentifiedBy import graphql.nadel.schema.NadelDirectives.nadelHydrationFromArgumentDefinition import graphql.nadel.schema.NadelDirectives.nadelHydrationTemplateEnumDefinition import graphql.schema.GraphQLSchema -import graphql.schema.idl.RuntimeWiring.MOCKED_WIRING +import graphql.schema.idl.MockedWiringFactory +import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaGenerator import graphql.schema.idl.SchemaParser import io.kotest.core.spec.style.DescribeSpec @@ -29,11 +30,14 @@ class NadelDirectivesTest : DescribeSpec({ ${AstPrinter.printAst(nadelHydrationTemplateEnumDefinition)} ${AstPrinter.printAst(hydratedFromDirectiveDefinition)} ${AstPrinter.printAst(hydratedTemplateDirectiveDefinition)} + scalar JSON """ fun getSchema(schemaText: String): GraphQLSchema { val typeDefs = SchemaParser().parse(commonDefs + "\n" + schemaText) - return SchemaGenerator().makeExecutableSchema(typeDefs, MOCKED_WIRING) + return SchemaGenerator().makeExecutableSchema(typeDefs, RuntimeWiring + .newRuntimeWiring() + .wiringFactory(NeverWiringFactory()).build()) } describe("@hydrated") {