Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update query usage across docs #9120

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 33 additions & 41 deletions www/apps/book/app/advanced-development/modules/query/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const metadata = {

In this chapter, you’ll learn about the Query utility and how to use it to fetch data from modules.

<Note>
<Note type="soon" title="In Development">

Remote Query is now deprecated in favor of Query. Follow this documentation to see the difference in the usage.
Query is in development and is subject to change in future releases.

</Note>

Expand All @@ -29,7 +29,7 @@ For example, create the route `src/api/store/query/route.ts` with the following
export const exampleHighlights = [
["13", "", "Resolve Query from the Medusa container."],
["15", "graph", "Run a query to retrieve data."],
["16", "entryPoint", "The name of the data model you're querying."],
["16", "entity", "The name of the data model you're querying."],
["17", "fields", "An array of the data model’s properties to retrieve in the result."],
]

Expand All @@ -49,7 +49,7 @@ export const GET = async (
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

const { data: myCustoms } = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: ["id", "name"],
})

Expand All @@ -61,7 +61,7 @@ In the above example, you resolve Query from the Medusa container using the `Con

Then, you run a query using its `graph` method. This method accepts as a parameter an object with the following required properties:

- `entryPoint`: The data model's name, as specified in the first parameter of the `model.define` method used for the data model's definition.
- `entity`: The data model's name, as specified in the first parameter of the `model.define` method used for the data model's definition.
- `fields`: An array of the data model’s properties to retrieve in the result.

The method returns an object that has a `data` property, which holds an array of the retrieved data. For example:
Expand All @@ -79,6 +79,14 @@ The method returns an object that has a `data` property, which holds an array of

---

## Querying the Graph

When you use the `query.graph` method, you're running a query through an internal graph that the Medusa application creates.

This graph collects data models of all modules in your application, including commerce and custom modules, and identifies relations and links between them.

---

## Retrieve Linked Records

Retrieve the records of a linked data model by passing in `fields` the data model's name suffixed with `.*`.
Expand All @@ -87,7 +95,7 @@ For example:

```ts highlights={[["6"]]}
const { data: myCustoms } = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: [
"id",
"name",
Expand All @@ -110,7 +118,7 @@ For example:

```ts highlights={[["6"]]}
const { data: myCustoms } = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: [
"id",
"name",
Expand All @@ -125,54 +133,36 @@ const { data: myCustoms } = await query.graph({

```ts highlights={[["6"], ["7"], ["8"], ["9"]]}
const { data: myCustoms } = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: ["id", "name"],
variables: {
filters: {
id: [
"mc_01HWSVWR4D2XVPQ06DQ8X9K7AX",
"mc_01HWSVWK3KYHKQEE6QGS2JC3FX",
],
},
filters: {
id: [
"mc_01HWSVWR4D2XVPQ06DQ8X9K7AX",
"mc_01HWSVWK3KYHKQEE6QGS2JC3FX",
],
},
})
```

The `query.graph` function accepts a `filters` property. You can use this property to filter retrieved records.

In the example above, you filter the `my_custom` records by multiple IDs.

<Note>

Filters don't apply on fields of linked data models from other modules.

</Note>

The `query.graph` function accepts a `variables` property. You can use this property to filter retrieved records.

<TypeList
types={[
{
name: "variables",
type: "`object`",
description: "Variables to pass to the query.",
children: [
{
name: "filters",
type: "`object`",
description: "The filters to apply on any of the data model's properties."
}
]
},
]}
sectionTitle="Apply Filters"
/>

---

## Sort Records

```ts highlights={[["5"], ["6"], ["7"]]}
const { data: myCustoms } = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: ["id", "name"],
variables: {
pagination: {
order: {
name: "DESC",
},
Expand All @@ -186,7 +176,9 @@ Sorting doesn't work on fields of linked data models from other modules.

</Note>

To sort returned records, pass an `order` property to `variables`.
The `graph` method's object parameter accepts a `pagination` property to configure the pagination of retrieved records.

To sort returned records, pass an `order` property to `pagination`.

The `order` property is an object whose keys are property names, and values are either:

Expand All @@ -202,16 +194,16 @@ const {
data: myCustoms,
metadata: { count, take, skip },
} = await query.graph({
entryPoint: "my_custom",
entity: "my_custom",
fields: ["id", "name"],
variables: {
pagination: {
skip: 0,
take: 10,
},
})
```

To paginate the returned records, pass the following properties to `variables`:
To paginate the returned records, pass the following properties to `pagination`:

- `skip`: (required to apply pagination) The number of records to skip before fetching the results.
- `take`: The number of records to fetch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,27 @@ const taxModuleService = container.resolve(
After resolving the resources, use Query to retrieve the products with the variants' prices for a context:

```ts
import { QueryContext } from "@medusajs/utils"

// ...

const { data: products } = await query.graph({
entryPoint: "product",
entity: "product",
fields: [
"*",
"variants.*",
"variants.calculated_price.*",
],
variables: {
filters: {
id: "prod_123",
},
"variants.calculated_price": {
context: {
filters: {
id: "prod_123",
},
context: {
variants: {
calculated_price: QueryContext({
region_id: "region_123",
currency_code: "usd",
},
},
}),
}
},
})
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ For example:

```ts highlights={[["6"]]}
const { data: products } = await query.graph({
entryPoint: "product",
entity: "product",
fields: [
"*",
"variants.*",
"variants.prices.*",
],
variables: {
filters: {
id: [
"prod_123",
],
},
filters: {
id: [
"prod_123",
],
},
})
```
Expand All @@ -59,32 +57,38 @@ Learn more about prices calculation in [this Pricing Module documentation](../..
To retrieve calculated prices of variants based on a context, retrieve the products using Query and:

- Pass `variants.calculated_price.*` in the `fields` property.
- Pass in the `variables` property a `variants.calculated_price` property whose value is the [calculation context object](../../../pricing/price-calculation/page.mdx#calculation-context).
- Pass a `context` property in the object parameter. Its value is an object of objects to sets the context for the retrieved fields.

For example:

```ts highlights={[["6"], ["12"], ["13"], ["14"], ["15"], ["16"], ["17"]]}
import { QueryContext } from "@medusajs/utils"

// ...

const { data: products } = await query.graph({
entryPoint: "product",
entity: "product",
fields: [
"*",
"variants.*",
"variants.calculated_price.*",
],
variables: {
filters: {
id: "prod_123",
},
"variants.calculated_price": {
context: {
filters: {
id: "prod_123",
},
context: {
variants: {
calculated_price: QueryContext({
region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS",
currency_code: "eur",
},
}),
},
},
})
```

The `variants.calculated_price` property of `variables` is an object that has a `context` property. `context`'s value is an object whose keys are price rules, such as `region_id`, and value is the rule's value in this context, such as the customer's region's ID.
For the context of the product variant's calculated price, you pass an object to `context` with the property `variants`, whose value is another object with the property `calculated_price`.

`calculated_price`'s value is created using the `QueryContext` utility function, passing it a [calculation context object](../../../pricing/price-calculation/page.mdx#calculation-context).

Each variant in the retrieved products has a `calculated_price` object. Learn more about its properties in [this Pricing Module guide](../../../pricing/price-calculation/page.mdx#returned-price-object).
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ export const GET = async (
data: digitalProducts,
metadata: { count, take, skip },
} = await query.graph({
entryPoint: "digital_product",
entity: "digital_product",
fields: [
"*",
"medias.*",
"product_variant.*",
...(fields || []),
],
variables: {
pagination: {
skip: offset,
take: limit,
},
Expand Down Expand Up @@ -1858,17 +1858,15 @@ async function digitalProductOrderCreatedHandler({
)

const { data: [digitalProductOrder] } = await query.graph({
entryPoint: "digital_product_order",
entity: "digital_product_order",
fields: [
"*",
"products.*",
"products.medias.*",
"order.*",
],
variables: {
filters: {
id: data.id,
},
filters: {
id: data.id,
},
})

Expand Down Expand Up @@ -2046,15 +2044,13 @@ export const GET = async (
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

const { data: [customer] } = await query.graph({
entryPoint: "customer",
entity: "customer",
fields: [
"orders.digital_product_order.products.*",
"orders.digital_product_order.products.medias.*",
],
variables: {
filters: {
id: req.auth_context.actor_id,
},
filters: {
id: req.auth_context.actor_id,
},
})

Expand Down Expand Up @@ -2109,14 +2105,12 @@ export const POST = async (
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

const { data: [customer] } = await query.graph({
entryPoint: "customer",
entity: "customer",
fields: [
"orders.digital_product_order.*",
],
variables: {
filters: {
id: req.auth_context.actor_id,
},
filters: {
id: req.auth_context.actor_id,
},
})

Expand All @@ -2125,14 +2119,12 @@ export const POST = async (
.map((order) => order.digital_product_order.id)

const { data: dpoResult } = await query.graph({
entryPoint: "digital_product_order",
entity: "digital_product_order",
fields: [
"products.medias.*",
],
variables: {
filters: {
id: customerDigitalOrderIds,
},
filters: {
id: customerDigitalOrderIds,
},
})

Expand Down
Loading
Loading