You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/book/app/learn/fundamentals/module-links/query/page.mdx
+87-10Lines changed: 87 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,35 @@ The method returns an object that has a `data` property, which holds an array of
71
71
}
72
72
```
73
73
74
+
### Query Usage in Workflows
75
+
76
+
To retrieve data with Query in a [workflow](../../workflows/page.mdx), use the [useQueryGraphStep](!resources!/references/helper-steps/useQueryGraphStep).
`.*` means that all of data model's properties should be retrieved. You can also retrieve specific properties by replacing the `*` with the property name, for each property.
130
+
`.*` means that all of the data model's properties should be retrieved. You can also retrieve specific properties by replacing the `*` with the property name for each property.
102
131
103
132
For example:
104
133
@@ -137,7 +166,7 @@ In the example above, you retrieve all products linked to a post.
137
166
138
167
### Apply Filters and Pagination on Linked Records
139
168
140
-
Consider you want to apply filters or pagination configurations on the product(s) linked to `post`. To do that, you must query the module link's table instead.
169
+
Consider that you want to apply filters or pagination configurations on the product(s) linked to a`post`. To do that, you must query the module link's table instead.
141
170
142
171
As mentioned in the [Module Link](../page.mdx) documentation, Medusa creates a table for your module link. So, not only can you retrieve linked records, but you can also retrieve the records in a module link's table.
- You pass the `entryPoint` property of the link definition as the value for `entity`. So, Query will retrieve records from the module link's table.
173
-
- You pass three items to the `field` property:
202
+
- You pass three items to the `fields` property:
174
203
-`*` to retrieve the link table's fields. This is useful if the link table has [custom columns](../custom-columns/page.mdx).
175
204
-`product.*` to retrieve the fields of a product record linked to a `Post` record.
176
205
-`post.*` to retrieve the fields of a `Post` record linked to a product record.
@@ -243,7 +272,7 @@ Under the hood, Query uses one of the following methods from the data model's mo
243
272
-`listX` if you don't pass [pagination parameters](#apply-pagination). For example, `listPosts`.
244
273
-`listAndCountX` if you pass pagination parameters. For example, `listAndCountPosts`.
245
274
246
-
Both methods accepts a filter object that can be used to filter records.
275
+
Both methods accept a filter object that can be used to filter records.
247
276
248
277
Those filters don't just allow you to filter by exact values. You can also filter by properties that don't match a value, match multiple values, and other filter types.
249
278
@@ -418,9 +447,57 @@ The `order` property is an object whose keys are property names, and values are
418
447
419
448
---
420
449
450
+
## Retrieve Deleted Records
451
+
452
+
By default, Query doesn't retrieve deleted records. To retrieve all records including deleted records, you can pass the `withDeleted` property to the `query.graph` method.
453
+
454
+
<Note>
455
+
456
+
The `withDeleted` property is available from [Medusa v2.8.5](https://github.com/medusajs/medusa/releases/tag/v2.8.5).
457
+
458
+
</Note>
459
+
460
+
For example:
461
+
462
+
```ts highlights={[["4", "withDeleted", "Include deleted posts in the results."]]}
463
+
const { data: posts } =awaitquery.graph({
464
+
entity: "post",
465
+
fields: ["id", "title"],
466
+
withDeleted: true,
467
+
})
468
+
```
469
+
470
+
In the example above, you retrieve all posts, including deleted ones.
471
+
472
+
### Retrieve Only Deleted Records
473
+
474
+
To retrieve only deleted records, you can add a `deleted_at` filter and set its value to not `null`. For example:
475
+
476
+
exportconst withDeletedHighlights = [
477
+
["5", "deleted_at", "Filter to retrieve posts whose `deleted_at` property is not `null`."],
478
+
["9", "withDeleted", "Include deleted posts in the results."],
479
+
]
480
+
481
+
```ts highlights={withDeletedHighlights}
482
+
const { data: posts } =awaitquery.graph({
483
+
entity: "post",
484
+
fields: ["id", "title"],
485
+
filters: {
486
+
deleted_at: {
487
+
$ne: null,
488
+
},
489
+
},
490
+
withDeleted: true,
491
+
})
492
+
```
493
+
494
+
In the example above, you retrieve only deleted posts by enabling the `withDeleted` property and adding a filter to only retrieve records where the `deleted_at` property is not `null`.
495
+
496
+
---
497
+
421
498
## Configure Query to Throw Errors
422
499
423
-
By default, if Query doesn't find records matching your query, it returns an empty array. You can add option to configure Query to throw an error when no records are found.
500
+
By default, if Query doesn't find records matching your query, it returns an empty array. You can add an option to configure Query to throw an error when no records are found.
424
501
425
502
The `query.graph` method accepts as a second parameter an object that can have a `throwIfKeyNotFound` property. Its value is a boolean indicating whether to throw an error if no record is found when filtering by IDs. By default, it's `false`.
In the example above, Query throws an error either if no post is found with the ID `post_123` or if its found but its author ID isn't `author_123`.
539
+
In the example above, Query throws an error either if no post is found with the ID `post_123` or if it's found but its author ID isn't `author_123`.
463
540
464
541
In the above example, it's assumed that a post belongs to an author, so it has an `author_id` property. However, this also works in the opposite case, where an author has many posts.
465
542
@@ -538,7 +615,7 @@ The `validateAndTransformQuery` accepts two parameters:
538
615
4.`order`: The fields to order the returned items by in ascending or descending order.
539
616
2. A Query configuration object. It accepts the following properties:
540
617
1.`defaults`: An array of default fields and relations to retrieve in each resource.
541
-
2.`isList`: A boolean indicating whether a list of items are returned in the response.
618
+
2.`isList`: A boolean indicating whether a list of items is returned in the response.
542
619
3.`allowed`: An array of fields and relations allowed to be passed in the `fields` query parameter.
543
620
4.`defaultLimit`: A number indicating the default limit to use if no limit is provided. By default, it's `50`.
544
621
@@ -554,7 +631,7 @@ As of [Medusa v2.2.0](https://github.com/medusajs/medusa/releases/tag/v2.2.0), `
554
631
555
632
</Note>
556
633
557
-
For example, Create the file `src/api/customs/route.ts` with the following content:
634
+
For example, create the file `src/api/customs/route.ts` with the following content:
558
635
559
636
exportconst queryConfigHighlights = [
560
637
["17", "req.queryConfig", "Pass the parsed request Query configurations to the Query graph execution."]
@@ -590,7 +667,7 @@ In the API route, you pass `req.queryConfig` to `query.graph`. `queryConfig` has
590
667
591
668
### Test it Out
592
669
593
-
To test it out, start your Medusa application and send a `GET` request to the `/customs` API route. A list of records are retrieved with the specified fields in the middleware.
670
+
To test it out, start your Medusa application and send a `GET` request to the `/customs` API route. A list of records is retrieved with the specified fields in the middleware.
594
671
595
672
```json title="Returned Data"
596
673
{
@@ -607,6 +684,6 @@ Try passing one of the Query configuration parameters, like `fields` or `limit`,
607
684
608
685
<Note>
609
686
610
-
Learn more about [specifing fields and relations](!api!/store#select-fields-and-relations) and [pagination](!api!/store#pagination) in the API reference.
687
+
Learn more about [specifying fields and relations](!api!/store#select-fields-and-relations) and [pagination](!api!/store#pagination) in the API reference.
0 commit comments