docs: add Query documentation (#9079)

- Replace remote query documentation with new Query documentation
- Add redirect from old remote query to new query documentation
- Update remote query usages across docs to use new query usage.
This commit is contained in:
Shahed Nasser
2024-09-10 12:31:47 +00:00
committed by GitHub
parent 1d8dd54014
commit e9b5f76f9a
31 changed files with 437 additions and 635 deletions
@@ -14,17 +14,18 @@ In this document, you'll learn how to calculate a product variant's price with t
You'll need the following resources for the taxes calculation:
1. Remote query to retrieve the product's variants' prices for a context. Learn more about that in [this guide](../price/page.mdx).
1. Query to retrieve the product's variants' prices for a context. Learn more about that in [this guide](../price/page.mdx).
2. The Tax Module's main service to get the tax lines for each product.
```ts
// other imports...
import {
ModuleRegistrationName,
ContainerRegistrationKeys,
} from "@medusajs/utils"
// In an API route, workflow step, etc...
const remoteQuery = container.resolve("remoteQuery")
const query = container.resolve(ContainerRegistrationKeys.QUERY)
const taxModuleService = container.resolve(
ModuleRegistrationName.TAX
)
@@ -34,16 +35,10 @@ const taxModuleService = container.resolve(
## Step 1: Retrieve Prices for a Context
After resolving the resources, use the remote query to retrieve the products with the variants' prices for a context:
After resolving the resources, use Query to retrieve the products with the variants' prices for a context:
```ts
// other imports...
import {
remoteQueryObjectFromString,
} from "@medusajs/utils"
// ...
const query = remoteQueryObjectFromString({
const { data: products } = await query.graph({
entryPoint: "product",
fields: [
"*",
@@ -52,18 +47,16 @@ const query = remoteQueryObjectFromString({
],
variables: {
filters: {
id,
id: "prod_123",
},
"variants.calculated_price": {
context: {
region_id,
currency_code,
region_id: "region_123",
currency_code: "usd",
},
},
},
})
const products = await remoteQuery(query)
```
<Note>
@@ -3,29 +3,29 @@ sidebar_label: "Get Product Variant Prices"
---
export const metadata = {
title: `Get Product Variant Prices using Remote Query`,
title: `Get Product Variant Prices using Query`,
}
# {metadata.title}
In this document, you'll learn how to retrieve product variant prices in the Medusa application using the [remote query](!docs!/advanced-development/modules/remote-query).
In this document, you'll learn how to retrieve product variant prices in the Medusa application using the [Query](!docs!/advanced-development/modules/query).
<Note title="Why use the Remote Query?">
<Note title="Why use Query?">
The Product Module doesn't provide pricing functionalities. The Medusa application links the Product Module's `ProductVariant` data model to the Pricing Module's `PriceSet` data model.
So, to retrieve data across the linked records of the two modules, you use the remote query.
So, to retrieve data across the linked records of the two modules, you use Query.
</Note>
## Retrieve All Product Variant Prices
To retrieve all product variant prices, retrieve the product using the remote query and include among its fields `variants.prices.*`.
To retrieve all product variant prices, retrieve the product using Query and include among its fields `variants.prices.*`.
For example:
```ts highlights={[["6"]]}
const query = remoteQueryObjectFromString({
const { data: products } = await query.graph({
entryPoint: "product",
fields: [
"*",
@@ -40,9 +40,6 @@ const query = remoteQueryObjectFromString({
},
},
})
// `result` is array of products
const result = await remoteQuery(query)
```
Each variant in the retrieved products has a `prices` array property with all the product variant prices. Each price object has the properties of the [Pricing Module's Price data model](/references/pricing/models/Price).
@@ -59,7 +56,7 @@ Learn more about prices calculation in [this Pricing Module documentation](../..
</Note>
To retrieve calculated prices of variants based on a context, retrieve the products using remote query and:
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).
@@ -67,7 +64,7 @@ To retrieve calculated prices of variants based on a context, retrieve the produ
For example:
```ts highlights={[["6"], ["12"], ["13"], ["14"], ["15"], ["16"], ["17"]]}
const query = remoteQueryObjectFromString({
const { data: products } = await query.graph({
entryPoint: "product",
fields: [
"*",
@@ -76,7 +73,7 @@ const query = remoteQueryObjectFromString({
],
variables: {
filters: {
id,
id: "prod_123",
},
"variants.calculated_price": {
context: {
@@ -86,9 +83,6 @@ const query = remoteQueryObjectFromString({
},
},
})
// `result` is array of products
const result = await remoteQuery(query)
```
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.