Skip to content

Commit

Permalink
Add test for abstract type
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf committed Dec 10, 2023
1 parent 166e5a6 commit 5de3752
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package graphql.nadel.tests

import graphql.nadel.validation.NadelSchemaValidationError

data class ValidationException constructor(
data class ValidationException(
override val message: String,
val errors: Set<NadelSchemaValidationError>,
) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package graphql.nadel.tests.hooks

import graphql.nadel.Nadel
import graphql.nadel.NadelExecutionHints
import graphql.nadel.engine.blueprint.NadelGenericHydrationInstruction
import graphql.nadel.engine.transform.result.json.JsonNode
import graphql.nadel.hooks.NadelExecutionHooks
import graphql.nadel.tests.EngineTestHook
import graphql.nadel.tests.UseHook

@UseHook
class `new-batching-conditional-hydration-in-abstract-type` : EngineTestHook {
override fun makeExecutionHints(builder: NadelExecutionHints.Builder): NadelExecutionHints.Builder {
return super.makeExecutionHints(builder)
.newBatchHydrationGrouping { true }
}

override fun makeNadel(builder: Nadel.Builder): Nadel.Builder {
return super.makeNadel(builder)
.executionHooks(
object : NadelExecutionHooks {
override fun <T : NadelGenericHydrationInstruction> getHydrationInstruction(
instructions: List<T>,
sourceId: JsonNode,
userContext: Any?,
): T {
val type = (sourceId.value as String).substringBefore("/")

return instructions
.first {
it.actorFieldDef.name.startsWith(type, ignoreCase = true)
}
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
name: "new batching conditional hydration in abstract type"
enabled: true
overallSchema:
# language=GraphQL
monolith: |
type Query {
activity: [IActivity]
issuesByIds(ids: [ID!]!): [Issue!]
commentsByIds(ids: [ID!]!): [Comment!]
}
interface IActivity {
content: [ActivityContent]
}
union ActivityContent = Issue | Comment
type Activity implements IActivity {
id: ID!
content: [ActivityContent]
@hydrated(
service: "monolith"
field: "commentsByIds"
arguments: [
{name: "ids" value: "$source.contentIds"}
]
)
@hydrated(
service: "monolith"
field: "issuesByIds"
arguments: [
{name: "ids" value: "$source.contentIds"}
]
)
}
type SingleActivity implements IActivity {
id: ID!
content: [ActivityContent]
@hydrated(
service: "monolith"
field: "issuesByIds"
arguments: [
{name: "ids" value: "$source.contentId"}
]
)
}
type Issue {
id: ID!
title: String
}
type Comment {
id: ID!
content: String
}
underlyingSchema:
# language=GraphQL
monolith: |
type Query {
activity: [IActivity]
commentsByIds(ids: [ID!]!): [Comment!]
issuesByIds(ids: [ID!]!): [Issue!]
}
interface IActivity {
content: [ActivityContent]
}
union ActivityContent = Issue | Comment
type Activity implements IActivity {
id: ID!
content: [ActivityContent]
contentIds: [ID!]
}
type SingleActivity implements IActivity {
id: ID!
content: [ActivityContent]
contentId: ID!
}
type Issue {
id: ID!
title: String
}
type Comment {
id: ID!
content: String
}
# language=GraphQL
query: |
{
activity {
content {
__typename
... on Issue {
id
title
}
... on Comment {
id
content
}
}
}
}
variables: { }
serviceCalls:
- serviceName: "monolith"
request:
# language=GraphQL
query: |
{
activity {
__typename__batch_hydration__content: __typename
... on Activity {
batch_hydration__content__contentIds: contentIds
batch_hydration__content__contentIds: contentIds
}
... on SingleActivity {
batch_hydration__content__contentId: contentId
}
}
}
variables: { }
# language=JSON
response: |-
{
"data": {
"activity": [
{
"__typename__batch_hydration__content": "Activity",
"batch_hydration__content__contentIds": [
"issue/4000",
"comment/5000",
"comment/6000"
]
},
{
"__typename__batch_hydration__content": "SingleActivity",
"batch_hydration__content__contentId": "issue/8080"
},
{
"__typename__batch_hydration__content": "Activity",
"batch_hydration__content__contentIds": [
"comment/1234",
"comment/9001"
]
},
{
"__typename__batch_hydration__content": "SingleActivity",
"batch_hydration__content__contentId": "issue/7496"
}
]
},
"extensions": {}
}
- serviceName: "monolith"
request:
# language=GraphQL
query: |
{
issuesByIds(ids: ["issue/4000"]) {
__typename
id
batch_hydration__content__id: id
title
}
}
variables: { }
# language=JSON
response: |-
{
"data": {
"issuesByIds": [
{
"__typename": "Issue",
"id": "issue/4000",
"batch_hydration__content__id": "issue/4000",
"title": "Four Thousand"
}
]
},
"extensions": {}
}
- serviceName: "monolith"
request:
# language=GraphQL
query: |
{
issuesByIds(ids: ["issue/8080", "issue/7496"]) {
__typename
id
batch_hydration__content__id: id
title
}
}
variables: { }
# language=JSON
response: |-
{
"data": {
"issuesByIds": [
{
"__typename": "Issue",
"id": "issue/7496",
"batch_hydration__content__id": "issue/7496",
"title": "Seven Four Nine Six"
}
]
},
"extensions": {}
}
- serviceName: "monolith"
request:
# language=GraphQL
query: |
{
commentsByIds(ids: ["comment/5000", "comment/6000", "comment/1234", "comment/9001"]) {
__typename
content
id
batch_hydration__content__id: id
}
}
variables: { }
# language=JSON
response: |-
{
"data": {
"commentsByIds": [
{
"__typename": "Comment",
"id": "comment/5000",
"batch_hydration__content__id": "comment/5000",
"content": "Five Thousand"
},
{
"__typename": "Comment",
"id": "comment/6000",
"batch_hydration__content__id": "comment/6000",
"content": "Six Thousand"
},
{
"__typename": "Comment",
"id": "comment/9001",
"batch_hydration__content__id": "comment/9001",
"content": "It's over 9000"
},
{
"__typename": "Comment",
"id": "comment/1234",
"batch_hydration__content__id": "comment/1234",
"content": "One Two Three Four"
}
]
},
"extensions": {}
}
# language=JSON
response: |
{
"data": {
"activity": [
{
"content": [
{
"__typename": "Issue",
"id": "issue/4000",
"title": "Four Thousand"
},
{
"__typename": "Comment",
"id": "comment/5000",
"content": "Five Thousand"
},
{
"__typename": "Comment",
"id": "comment/6000",
"content": "Six Thousand"
}
]
},
{
"content": [
null
]
},
{
"content": [
{
"__typename": "Comment",
"id": "comment/1234",
"content": "One Two Three Four"
},
{
"__typename": "Comment",
"id": "comment/9001",
"content": "It's over 9000"
}
]
},
{
"content": [
{
"__typename": "Issue",
"id": "issue/7496",
"title": "Seven Four Nine Six"
}
]
}
]
},
"errors": []
}

0 comments on commit 5de3752

Please sign in to comment.