Skip to content

Commit 0ed3aaa

Browse files
author
Julien Neuhart
committed
Add Lists documentation
1 parent 1599e31 commit 0ed3aaa

File tree

5 files changed

+599
-6
lines changed

5 files changed

+599
-6
lines changed

docs/docs/11_Lists/1_Overview.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
---
22
title: Overview
3-
slug: /theme
3+
slug: /lists
44
---
55

6-
🚧
6+
In the API, a list means retrieving data from the database according to:
7+
8+
* Random filters (e.g., free search).
9+
* Predictable filters (e.g., enum values).
10+
* A sort direction on a field.
11+
* A limit and an offset.
12+
13+
On the web application, a list means displaying data according to user's inputs.
14+
15+
In the next chapters, we explain how it works and how you should implement it.

docs/docs/11_Lists/2_Database.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Database
3+
slug: /lists/database
4+
---
5+
6+
## DAO
7+
8+
The first step is to write a query in a DAO class. For instance:
9+
10+
```php title="src/api/src/Domain/Dao/UserDao.php"
11+
/**
12+
* @return User[]|ResultIterator
13+
*/
14+
public function search(
15+
?string $search = null,
16+
?Role $role = null,
17+
?UsersSortBy $sortBy = null,
18+
?SortOrder $sortOrder = null
19+
): ResultIterator {
20+
$sortBy = $sortBy ?: UsersSortBy::FIRST_NAME();
21+
$sortOrder = $sortOrder ?: SortOrder::ASC();
22+
23+
return $this->find(
24+
[
25+
'first_name LIKE :search OR last_name LIKE :search OR email LIKE :search',
26+
'role = :role',
27+
],
28+
[
29+
'search' => '%' . $search . '%',
30+
'role' => $role,
31+
],
32+
$sortBy . ' ' . $sortOrder
33+
);
34+
}
35+
```
36+
37+
Here we have:
38+
39+
* A random filter, the `$search` string.
40+
* A predictable filter, the `$role` enum.
41+
* A sort direction and a sort column.
42+
43+
All the arguments are optional (`= null`).
44+
45+
:::note
46+
47+
📣  If a parameter's value is `null`, [TDBM](https://github.com/thecodingmachine/tdbm)
48+
automatically removes the corresponding conditions in the first argument of the `find` method.
49+
50+
:::
51+
52+
## ResultIterator
53+
54+
Thanks to the `ResultIterator` class, you may retrieve a precise scope of the resulting data:
55+
56+
```php
57+
$result = $userDao->search();
58+
$result->take($offset, $limit);
59+
```
60+
61+
:::note
62+
63+
📣  Most of the time, you won't have to explicitly call the `take` method
64+
thanks to [GraphQLite](https://graphqlite.thecodingmachine.io/). More on that in the next chapter.
65+
66+
:::
67+
68+
## Enum
69+
70+
The folder *src/api/src/Domain/Enum* contains our enums.
71+
72+
Each enum's key (i.e., `FIRST_NAME`) is a GraphQL value, while each enum's value (i.e., `first_name`)
73+
is a valid SQL expression.
74+
75+
Most enums are for:
76+
77+
* Sort by values.
78+
* Business data.

docs/docs/11_Lists/3_GraphQL.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: GraphQL
3+
slug: /lists/graphql
4+
---
5+
6+
In the previous chapter, we define a `search` method in a DAO.
7+
8+
We now have to create a GraphQL query from it:
9+
10+
```php title="src/api/src/UseCase/User/GetUsers.php"
11+
/**
12+
* @return User[]|ResultIterator
13+
*
14+
* @Query
15+
* @Logged
16+
* @Security("is_granted('ROLE_ADMINISTRATOR')")
17+
*/
18+
public function users(
19+
?string $search = null,
20+
?Role $role = null,
21+
?UsersSortBy $sortBy = null,
22+
?SortOrder $sortOrder = null
23+
): ResultIterator {
24+
return $this->userDao->search(
25+
$search,
26+
$role,
27+
$sortBy,
28+
$sortOrder
29+
);
30+
}
31+
```
32+
33+
It's simple as that! 😉
34+
35+
Let's take a look at the GraphQL query for the `GetUsers` use case:
36+
37+
```graphql title="GraphQL query"
38+
query users($search: String, $role: Role, $sortBy: UsersSortBy, $sortOrder: SortOrder, $limit: Int!, $offset: Int!) {
39+
users(search: $search, role: $role, sortBy: $sortBy, sortOrder: $sortOrder) {
40+
items(limit: $limit, offset: $offset) {
41+
id
42+
firstName
43+
lastName
44+
email
45+
locale
46+
role
47+
}
48+
count
49+
}
50+
}
51+
```
52+
53+
Here we can see that:
54+
55+
1. The enums are valid GraphQL types.
56+
2. The `items` property with the `limit` and `offset` arguments plus the `count` property come from the `ResultIterator`.
57+

0 commit comments

Comments
 (0)