Skip to content

Commit

Permalink
Merge pull request #474 from atlassian-labs/when-condition-input-types
Browse files Browse the repository at this point in the history
Adding When condition input types
  • Loading branch information
sbarker2 committed Dec 6, 2023
2 parents 8691951 + 158219d commit 79c58b5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
54 changes: 53 additions & 1 deletion lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import graphql.language.BooleanValue
import graphql.language.DirectiveDefinition.newDirectiveDefinition
import graphql.language.EnumTypeDefinition.newEnumTypeDefinition
import graphql.language.EnumValueDefinition.newEnumValueDefinition
import graphql.language.InputObjectTypeDefinition
import graphql.language.InputObjectTypeDefinition.newInputObjectDefinition
import graphql.language.ObjectValue
import graphql.language.StringValue
import graphql.language.TypeDefinition
import graphql.language.TypeName
import graphql.language.Value
import graphql.nadel.dsl.FieldMappingDefinition
import graphql.nadel.dsl.RemoteArgumentDefinition
import graphql.nadel.dsl.RemoteArgumentSource
import graphql.nadel.dsl.RemoteArgumentSource.SourceType
import graphql.nadel.dsl.TypeMappingDefinition
import graphql.nadel.dsl.UnderlyingServiceHydration
import graphql.nadel.engine.util.singleOfType
import graphql.nadel.util.IntValue
import graphql.nadel.util.description
import graphql.nadel.util.emptyArrayValue
Expand All @@ -34,6 +38,7 @@ import graphql.nadel.util.onInterface
import graphql.nadel.util.onObject
import graphql.nadel.util.onScalar
import graphql.nadel.util.onUnion
import graphql.parser.Parser
import graphql.schema.GraphQLAppliedDirective
import graphql.schema.GraphQLAppliedDirectiveArgument
import graphql.schema.GraphQLDirectiveContainer
Expand Down Expand Up @@ -79,6 +84,38 @@ object NadelDirectives {
)
.build()

val nadelWhenConditionPredicateDefinition = parseType<InputObjectTypeDefinition>(
"""
input NadelWhenConditionPredicate @oneOf {
startsWith: String
equals: JSON
matches: String
}
""".trimIndent(),
)

val nadelWhenConditionResultDefinition = newInputObjectDefinition()
.name("NadelWhenConditionResult")
.description("Specify a condition for the hydration to activate based on the result")
.inputValueDefinition(
name = "sourceField",
type = nonNull(GraphQLString),
)
.inputValueDefinition(
name = "predicate",
type = nonNull(nadelWhenConditionPredicateDefinition),
)
.build()

val nadelWhenConditionDefinition = newInputObjectDefinition()
.name("NadelWhenCondition")
.description("Specify a condition for the hydration to activate")
.inputValueDefinition(
name = "result",
type = nonNull(nadelWhenConditionResultDefinition),
)
.build()

val hydratedDirectiveDefinition = newDirectiveDefinition()
.name("hydrated")
.onFieldDefinition()
Expand Down Expand Up @@ -135,6 +172,13 @@ object NadelDirectives {
description = "The arguments to the hydrated field",
type = nonNull(list(nonNull(nadelHydrationArgumentDefinition))),
)
.inputValueDefinition(
name = "when",
description = "Specify a condition for the hydration to activate",
type = TypeName.newTypeName()
.name(nadelWhenConditionDefinition.name)
.build()
)
.build()

val dynamicServiceDirectiveDefinition = newDirectiveDefinition()
Expand Down Expand Up @@ -482,6 +526,14 @@ object NadelDirectives {

private fun <T> resolveArgumentValue(graphQLArgument: GraphQLAppliedDirectiveArgument): T {
@Suppress("UNCHECKED_CAST") // Trust caller. Can't do much
return ValuesResolver.valueToInternalValue(graphQLArgument.argumentValue, graphQLArgument.type, GraphQLContext.getDefault(), Locale.getDefault()) as T
return ValuesResolver.valueToInternalValue(
graphQLArgument.argumentValue,
graphQLArgument.type,
GraphQLContext.getDefault(),
Locale.getDefault()
) as T
}
private inline fun <reified T : TypeDefinition<*>> parseType(sdl: String): T {
return Parser.parse(sdl).definitions.singleOfType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ internal class OverallSchemaGenerator {
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelHydrationTemplateEnumDefinition)
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hydratedFromDirectiveDefinition)
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hydratedTemplateDirectiveDefinition)
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelWhenConditionPredicateDefinition)
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelWhenConditionDefinition)
addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelWhenConditionResultDefinition)

addIfNotPresent(overallRegistry, allDefinitions, newScalarTypeDefinition()
.name(ExtendedScalars.Json.name)
.build())
Expand Down
10 changes: 5 additions & 5 deletions lib/src/test/kotlin/graphql/nadel/NadelSchemasTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NadelSchemasTest : DescribeSpec({
.build()

// then
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "JSON"))
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "JSON", "NadelWhenCondition", "NadelWhenConditionPredicate", "NadelWhenConditionResult"))
val testService = schemas.services.single()
assert(testService.underlyingSchema.userTypeNames == setOf("World", "Echo", "Query", "Food"))
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class NadelSchemasTest : DescribeSpec({
.build()

// then
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "JSON"))
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "JSON", "NadelWhenCondition", "NadelWhenConditionPredicate", "NadelWhenConditionResult"))
val testService = schemas.services.single()
assert(testService.underlyingSchema.userTypeNames == setOf("World", "Echo", "Query", "Food"))
}
Expand Down Expand Up @@ -211,7 +211,7 @@ class NadelSchemasTest : DescribeSpec({
.build()

// then
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "Issue", "JSON"))
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "Issue", "JSON", "NadelWhenCondition", "NadelWhenConditionPredicate", "NadelWhenConditionResult"))

val issueService = schemas.services.single { it.name == "issue" }
assert(issueService.underlyingSchema.userTypeNames == setOf("Query", "Issue"))
Expand Down Expand Up @@ -272,7 +272,7 @@ class NadelSchemasTest : DescribeSpec({
.build()

// then
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "Issue", "JSON"))
assert(schemas.engineSchema.userTypeNames == setOf("World", "Echo", "Query", "Issue", "JSON", "NadelWhenCondition", "NadelWhenConditionPredicate", "NadelWhenConditionResult"))
}

it("does not validate the schemas") {
Expand Down Expand Up @@ -321,7 +321,7 @@ class NadelSchemasTest : DescribeSpec({
.build()

// then
assert(schemas.engineSchema.userTypeNames == setOf("Query", "World", "Echo", "Issue", "JSON"))
assert(schemas.engineSchema.userTypeNames == setOf("Query", "World", "Echo", "Issue", "JSON", "NadelWhenCondition", "NadelWhenConditionPredicate", "NadelWhenConditionResult"))

val testService = schemas.services.first { it.name == "test" }
assert(testService.definitionRegistry.typeNames == setOf("Query", "Echo", "World"))
Expand Down
12 changes: 12 additions & 0 deletions lib/src/test/kotlin/graphql/nadel/schema/NadelDirectivesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import graphql.nadel.schema.NadelDirectives.nadelHydrationArgumentDefinition
import graphql.nadel.schema.NadelDirectives.nadelHydrationComplexIdentifiedBy
import graphql.nadel.schema.NadelDirectives.nadelHydrationFromArgumentDefinition
import graphql.nadel.schema.NadelDirectives.nadelHydrationTemplateEnumDefinition
import graphql.nadel.schema.NadelDirectives.nadelWhenConditionDefinition
import graphql.nadel.schema.NadelDirectives.nadelWhenConditionPredicateDefinition
import graphql.nadel.schema.NadelDirectives.nadelWhenConditionResultDefinition
import graphql.schema.GraphQLSchema
import graphql.schema.idl.MockedWiringFactory
import graphql.schema.idl.RuntimeWiring
Expand All @@ -30,6 +33,9 @@ class NadelDirectivesTest : DescribeSpec({
${AstPrinter.printAst(nadelHydrationTemplateEnumDefinition)}
${AstPrinter.printAst(hydratedFromDirectiveDefinition)}
${AstPrinter.printAst(hydratedTemplateDirectiveDefinition)}
${AstPrinter.printAst(nadelWhenConditionDefinition)}
${AstPrinter.printAst(nadelWhenConditionPredicateDefinition)}
${AstPrinter.printAst(nadelWhenConditionResultDefinition)}
scalar JSON
"""

Expand All @@ -55,6 +61,12 @@ class NadelDirectivesTest : DescribeSpec({
{name: "fieldVal" value: "$source.namespace.issueId"}
{name: "argVal" value: "$argument.cloudId"}
]
when: {
result: {
sourceField: "type"
predicate: { equals: "issue" }
}
}
)
}
""".trimIndent())
Expand Down

0 comments on commit 79c58b5

Please sign in to comment.