Skip to main content

2.3 Deeper Association

Query Dynamic Entities

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();

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.

BookStoreTable table = BookStoreTable.$;

List<BookStoreView> stores = sqlClient
.createQuery(table)
.where(table.name().eq("MANNING"))
.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
)
]
)
]
)
]