Skip to content

Commit f77696e

Browse files
authored
docs: documet withDeleted (medusajs#12841)
* docs: documet withDeleted * fixes * small fix
1 parent 035ef9e commit f77696e

File tree

3 files changed

+169
-28
lines changed

3 files changed

+169
-28
lines changed

www/apps/book/app/learn/fundamentals/module-links/query/page.mdx

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ The method returns an object that has a `data` property, which holds an array of
7171
}
7272
```
7373

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).
77+
78+
For example:
79+
80+
```ts title="src/workflows/query.ts"
81+
import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
82+
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
83+
84+
const myWorkflow = createWorkflow(
85+
"my-workflow",
86+
() => {
87+
const { data: posts } = useQueryGraphStep({
88+
entity: "post",
89+
fields: ["id", "title"],
90+
})
91+
92+
return new WorkflowResponse({
93+
posts,
94+
})
95+
}
96+
)
97+
```
98+
99+
You can learn more about this step in the [useQueryGraphStep](!resources!/references/helper-steps/useQueryGraphStep) reference.
100+
101+
The rest of this chapter uses the `graph` method to explain the different usages of Query, but the same principles apply to `useQueryGraphStep`.
102+
74103
---
75104

76105
## Querying the Graph
@@ -98,7 +127,7 @@ const { data: posts } = await query.graph({
98127
})
99128
```
100129

101-
`.*` 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.
102131

103132
For example:
104133

@@ -137,7 +166,7 @@ In the example above, you retrieve all products linked to a post.
137166

138167
### Apply Filters and Pagination on Linked Records
139168

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.
141170

142171
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.
143172

@@ -170,7 +199,7 @@ const { data: productCustoms } = await query.graph({
170199
In the object passed to the `graph` method:
171200

172201
- 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:
174203
- `*` to retrieve the link table's fields. This is useful if the link table has [custom columns](../custom-columns/page.mdx).
175204
- `product.*` to retrieve the fields of a product record linked to a `Post` record.
176205
- `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
243272
- `listX` if you don't pass [pagination parameters](#apply-pagination). For example, `listPosts`.
244273
- `listAndCountX` if you pass pagination parameters. For example, `listAndCountPosts`.
245274

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.
247276

248277
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.
249278

@@ -418,9 +447,57 @@ The `order` property is an object whose keys are property names, and values are
418447

419448
---
420449

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 } = await query.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+
export const 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 } = await query.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+
421498
## Configure Query to Throw Errors
422499

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.
424501

425502
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`.
426503

@@ -459,7 +536,7 @@ const { data: posts } = await query.graph({
459536
})
460537
```
461538

462-
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`.
463540

464541
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.
465542

@@ -538,7 +615,7 @@ The `validateAndTransformQuery` accepts two parameters:
538615
4. `order`: The fields to order the returned items by in ascending or descending order.
539616
2. A Query configuration object. It accepts the following properties:
540617
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.
542619
3. `allowed`: An array of fields and relations allowed to be passed in the `fields` query parameter.
543620
4. `defaultLimit`: A number indicating the default limit to use if no limit is provided. By default, it's `50`.
544621

@@ -554,7 +631,7 @@ As of [Medusa v2.2.0](https://github.com/medusajs/medusa/releases/tag/v2.2.0), `
554631

555632
</Note>
556633

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:
558635

559636
export const queryConfigHighlights = [
560637
["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
590667

591668
### Test it Out
592669

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.
594671

595672
```json title="Returned Data"
596673
{
@@ -607,6 +684,6 @@ Try passing one of the Query configuration parameters, like `fields` or `limit`,
607684

608685
<Note>
609686

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.
611688

612689
</Note>

www/apps/book/generated/edit-dates.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const generatedEditDates = {
6666
"app/learn/fundamentals/module-links/custom-columns/page.mdx": "2025-03-11T13:29:54.752Z",
6767
"app/learn/fundamentals/module-links/directions/page.mdx": "2025-03-17T12:52:06.161Z",
6868
"app/learn/fundamentals/module-links/page.mdx": "2025-04-17T08:50:17.036Z",
69-
"app/learn/fundamentals/module-links/query/page.mdx": "2025-04-18T11:13:02.240Z",
69+
"app/learn/fundamentals/module-links/query/page.mdx": "2025-06-26T16:01:59.548Z",
7070
"app/learn/fundamentals/modules/db-operations/page.mdx": "2025-04-25T14:26:25.000Z",
7171
"app/learn/fundamentals/modules/multiple-services/page.mdx": "2025-03-18T15:11:44.632Z",
7272
"app/learn/fundamentals/modules/page.mdx": "2025-03-18T07:51:09.049Z",

0 commit comments

Comments
 (0)