2.3 Deeper Association
Query Dynamic Entities
- Java
- Kotlin
BookStoreTable table = BookStoreTable.$;
List<BookStore> stores = sqlClient
.createQuery(table)
.where(table.name().eq("MANNING"))
.select(
table.fetch(
BookStoreFetcher.$
.allScalarFields()
.books( ❶
BookFetcher.$
.allScalarFields()
.authors( ❷
AuthorFetcher.$
.allScalarFields()
)
)
)
)
.execute();
val books = sqlClient
.createQuery(BookStore::class) {
where(table.name eq "MANNING")
select(
table.fetchBy {
allScalarFields()
books { ❶
allScalarFields()
authors { ❷
allScalarFields()
}
}
}
)
}
.execute()
The result is
[
{
"id":2,
"name":"MANNING",
"website":null,
"books":[ ❶
{
"id":12,
"name":"GraphQL in Action",
"edition":3,
"price":80,
"authors":[ ❷
{
"id":5,
"firstName":"Samer",
"lastName":"Buna",
"gender":"MALE"
}
]
},
{
"id":11,
"name":"GraphQL in Action",
"edition":2,
"price":81,
"authors":[ ❷
{
"id":5,
"firstName":"Samer",
"lastName":"Buna",
"gender":"MALE"
}
]
},
{
"id":10,
"name":"GraphQL in Action",
"edition":1,
"price":82,
"authors":[ ❷
{
"id":5,
"firstName":"Samer",
"lastName":"Buna",
"gender":"MALE"
}
]
}
]
}
]
Query Static DTO
Create any file with the dto
extension under the src/main/dto
folder, and edit the code as follows:
export com.yourcompany.yourproject.model.BookStore
-> package com.yourcompany.yourproject.model.dto
BookStoreView {
#allScalars(this)
books { ❶
#allScalars(this)
authors { ❷
#allScalars(this)
}
}
}
Compile the project, a java/kotlin class named BookStoreView
will be automatically generated.
- Java
- Kotlin
BookStoreTable table = BookStoreTable.$;
List<BookStoreView> stores = sqlClient
.createQuery(table)
.where(table.name().eq("MANNING"))
.select(
table.fetch(BookStoreView.class)
)
.execute();
val stores = sqlClient
.createQuery(BookStore::class) {
where(table.name eq "Learning GraphQL")
select(
table.fetch(BookStoreView::class)
)
}
.execute()
The result is
[
BookStoreView(
id=2,
name=MANNING,
website=null,
books=[
BookStoreView.TargetOf_books(
id=12,
name=GraphQL in Action,
edition=3,
price=80.00,
authors=[
BookStoreView.TargetOf_books.TargetOf_authors_2(
id=5,
firstName=Samer,
lastName=Buna,
gender=MALE
)
]
),
BookStoreView.TargetOf_books(
id=11,
name=GraphQL in Action,
edition=2,
price=81.00,
authors=[
BookStoreView.TargetOf_books.TargetOf_authors_2(
id=5,
firstName=Samer,
lastName=Buna,
gender=MALE
)
]
),
BookStoreView.TargetOf_books(
id=10,
name=GraphQL in Action,
edition=1,
price=82.00,
authors=[
BookStoreView.TargetOf_books.TargetOf_authors_2(
id=5,
firstName=Samer,
lastName=Buna,
gender=MALE
)
]
)
]
)
]