Skip to content

Commit

Permalink
Add support for defer payload label (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf authored Aug 5, 2024
1 parent c94076e commit 77ff8a6
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ internal class NadelHydrationTransform(
)
}

// This isn't really right… but we start with this
val label = overallField.deferredExecutions.firstNotNullOfOrNull { it.label }

executionContext.incrementalResultSupport.defer {
val instructionSequence = hydrations
.map {
Expand All @@ -232,6 +235,7 @@ internal class NadelHydrationTransform(
.incrementalItems(
listOf(
DeferPayload.Builder()
.label(label)
.data(
mapOf(
overallField.resultKey to results?.newValue?.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package graphql.nadel.tests.next.fixtures.hydration.defer

import graphql.nadel.NadelExecutionHints
import graphql.nadel.engine.util.strictAssociateBy
import graphql.nadel.tests.next.NadelIntegrationTest

open class HydrationDeferLabelTest : NadelIntegrationTest(
query = """
query {
issue(id: "ari:cloud:jira::issue/1") {
id
... @defer(label: "who is assigned this issue") {
assignee {
name
}
}
}
}
""".trimIndent(),
services = listOf(
Service(
name = "issues",
overallSchema = """
type Query {
issue(id: ID!): Issue
}
type Issue {
id: ID!
assigneeId: ID! @hidden
assignee: User
@hydrated(
service: "users"
field: "user"
arguments: [{name: "id", value: "$source.assigneeId"}]
)
}
""".trimIndent(),
runtimeWiring = { wiring ->
data class Issue(
val id: String,
val assigneeId: String,
)

val issuesById: Map<String, Issue> = listOf(
Issue(
id = "ari:cloud:jira::issue/1",
assigneeId = "ari:cloud:jira::user/1",
),
).strictAssociateBy { it.id }

wiring
.type("Query") { type ->
type.dataFetcher("issue") { env ->
issuesById[env.getArgument("id")]
}
}
},
),
Service(
name = "users",
overallSchema = """
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
}
""".trimIndent(),
runtimeWiring = { wiring ->
data class User(
val id: String,
val name: String,
)

val usersById = listOf(
User(
id = "ari:cloud:jira::user/1",
name = "Franklin",
),
).strictAssociateBy { it.id }

wiring
.type("Query") { type ->
type.dataFetcher("user") { env ->
usersById[env.getArgument("id")]
}
}
},
),
),
) {
override fun makeExecutionHints(): NadelExecutionHints.Builder {
return super.makeExecutionHints()
.deferSupport { true }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// @formatter:off
package graphql.nadel.tests.next.fixtures.hydration.defer

import graphql.nadel.tests.next.ExpectedNadelResult
import graphql.nadel.tests.next.ExpectedServiceCall
import graphql.nadel.tests.next.TestSnapshot
import graphql.nadel.tests.next.listOfJsonStrings
import kotlin.Suppress
import kotlin.collections.List
import kotlin.collections.listOf

private suspend fun main() {
graphql.nadel.tests.next.update<HydrationDeferLabelTest>()
}

/**
* This class is generated. Do NOT modify.
*
* Refer to [graphql.nadel.tests.next.UpdateTestSnapshots
*/
@Suppress("unused")
public class HydrationDeferLabelTestSnapshot : TestSnapshot() {
override val calls: List<ExpectedServiceCall> = listOf(
ExpectedServiceCall(
service = "issues",
query = """
| {
| issue(id: "ari:cloud:jira::issue/1") {
| id
| hydration__assignee__assigneeId: assigneeId
| __typename__hydration__assignee: __typename
| }
| }
""".trimMargin(),
variables = "{}",
result = """
| {
| "data": {
| "issue": {
| "id": "ari:cloud:jira::issue/1",
| "hydration__assignee__assigneeId": "ari:cloud:jira::user/1",
| "__typename__hydration__assignee": "Issue"
| }
| }
| }
""".trimMargin(),
delayedResults = listOfJsonStrings(
),
),
ExpectedServiceCall(
service = "users",
query = """
| {
| user(id: "ari:cloud:jira::user/1") {
| name
| }
| }
""".trimMargin(),
variables = "{}",
result = """
| {
| "data": {
| "user": {
| "name": "Franklin"
| }
| }
| }
""".trimMargin(),
delayedResults = listOfJsonStrings(
),
),
)

/**
* ```json
* {
* "data": {
* "issue": {
* "id": "ari:cloud:jira::issue/1",
* "assignee": {
* "name": "Franklin"
* }
* }
* }
* }
* ```
*/
override val result: ExpectedNadelResult = ExpectedNadelResult(
result = """
| {
| "data": {
| "issue": {
| "id": "ari:cloud:jira::issue/1"
| }
| },
| "hasNext": true
| }
""".trimMargin(),
delayedResults = listOfJsonStrings(
"""
| {
| "hasNext": false,
| "incremental": [
| {
| "path": [
| "issue"
| ],
| "label": "who is assigned this issue",
| "data": {
| "assignee": {
| "name": "Franklin"
| }
| }
| }
| ]
| }
""".trimMargin(),
),
)
}

0 comments on commit 77ff8a6

Please sign in to comment.