-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to migrate hydration based on type conditions (#667)
- Loading branch information
Showing
19 changed files
with
879 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...l/nadel/tests/next/fixtures/hydration/PolymorphicHydrationCommonInterfaceMigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package graphql.nadel.tests.next.fixtures.hydration | ||
|
||
import graphql.nadel.Nadel | ||
import graphql.nadel.engine.blueprint.NadelGenericHydrationInstruction | ||
import graphql.nadel.engine.transform.artificial.NadelAliasHelper | ||
import graphql.nadel.engine.transform.result.json.JsonNode | ||
import graphql.nadel.hooks.NadelExecutionHooks | ||
import graphql.nadel.tests.next.NadelIntegrationTest | ||
import graphql.normalized.ExecutableNormalizedField | ||
|
||
/** | ||
* Should resolve to old hydration if it's ambiguous. | ||
*/ | ||
class PolymorphicHydrationCommonInterfaceMigrationTest : NadelIntegrationTest( | ||
query = """ | ||
{ | ||
reference { | ||
object { | ||
__typename | ||
... on OldObject { | ||
name | ||
} | ||
... on CommonInterface { | ||
id | ||
} | ||
... on NewObject { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
services = listOf( | ||
Service( | ||
name = "monolith", | ||
overallSchema = """ | ||
type Query { | ||
reference: Reference | ||
oldObjects(ids: [ID!]!): [OldObject] | ||
newObjects(ids: [ID!]!): [NewObject] | ||
} | ||
union Object = OldObject | NewObject | ||
type Reference { | ||
objectId: ID! | ||
object: Object | ||
@idHydrated(idField: "objectId") | ||
} | ||
interface CommonInterface { | ||
id: ID! | ||
} | ||
type NewObject implements CommonInterface @defaultHydration(field: "newObjects", idArgument: "ids") { | ||
id: ID! | ||
name: String | ||
} | ||
type OldObject implements CommonInterface @defaultHydration(field: "oldObjects", idArgument: "ids") { | ||
id: ID! | ||
name: String | ||
} | ||
""".trimIndent(), | ||
runtimeWiring = { wiring -> | ||
data class Reference(val objectId: String) | ||
data class NewObject(val id: String, val name: String) | ||
data class OldObject(val id: String, val name: String) | ||
|
||
val newObjectsById = listOf( | ||
NewObject("ari:cloud:owner::type/1", "New object") | ||
).associateBy { it.id } | ||
|
||
val oldObjectsById = listOf( | ||
OldObject("ari:cloud:owner::type/1", "Old object") | ||
).associateBy { it.id } | ||
|
||
wiring | ||
.type("Query") { type -> | ||
type | ||
.dataFetcher("reference") { env -> | ||
Reference(objectId = "ari:cloud:owner::type/1") | ||
} | ||
.dataFetcher("oldObjects") { env -> | ||
env.getArgument<List<String>>("ids")?.map(oldObjectsById::get) | ||
} | ||
.dataFetcher("newObjects") { env -> | ||
env.getArgument<List<String>>("ids")?.map(newObjectsById::get) | ||
} | ||
} | ||
.type("Object") { type -> | ||
type.typeResolver { env -> | ||
env.schema.getObjectType(env.getObject<Any?>().javaClass.simpleName) | ||
} | ||
} | ||
.type("CommonInterface") { type -> | ||
type.typeResolver { env -> | ||
env.schema.getObjectType(env.getObject<Any?>().javaClass.simpleName) | ||
} | ||
} | ||
}, | ||
), | ||
), | ||
) { | ||
override fun makeNadel(): Nadel.Builder { | ||
return super.makeNadel() | ||
.executionHooks( | ||
object : NadelExecutionHooks { | ||
override fun <T : NadelGenericHydrationInstruction> getHydrationInstruction( | ||
virtualField: ExecutableNormalizedField, | ||
instructions: List<T>, | ||
parentNode: JsonNode, | ||
aliasHelper: NadelAliasHelper, | ||
userContext: Any?, | ||
): T? { | ||
val hasNewObject = virtualField.children.any { child -> | ||
child.objectTypeNames.size == 1 | ||
&& child.objectTypeNames.first() == "NewObject" | ||
&& !child.fieldName.startsWith("__") | ||
} | ||
val hasOldObject = virtualField.children.any { child -> | ||
child.objectTypeNames.contains("OldObject") | ||
&& !child.fieldName.startsWith("__") | ||
} | ||
|
||
return if (hasNewObject || !hasOldObject) { | ||
instructions.first { | ||
it.backingFieldDef.name == "newObjects" | ||
} | ||
} else { | ||
instructions.first { | ||
it.backingFieldDef.name == "oldObjects" | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.