docs: general improvements and fixes (#13515)
This commit is contained in:
@@ -8591,15 +8591,15 @@ The value of `req.query.name` is the value passed in `?name=John`, for example.
|
||||
|
||||
You can apply validation rules on received query parameters to ensure they match specified rules and types.
|
||||
|
||||
Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/api-routes/validation#how-to-validate-request-query-paramters/index.html.md).
|
||||
Learn more in the [API Route Validation](https://docs.medusajs.com/learn/fundamentals/api-routes/validation#how-to-validate-request-query-parameters/index.html.md) chapter.
|
||||
|
||||
***
|
||||
|
||||
## Request Body Parameters
|
||||
|
||||
The Medusa application parses the body of any request having a JSON, URL-encoded, or text request content types. The request body parameters are set in the `MedusaRequest`'s `body` property.
|
||||
The Medusa application parses the body of any request with JSON, URL-encoded, or text content types. The request body parameters are set in the `MedusaRequest`'s `body` property.
|
||||
|
||||
Learn more about configuring body parsing in [this guide](https://docs.medusajs.com/learn/fundamentals/api-routes/parse-body/index.html.md).
|
||||
Learn more about configuring body parsing in the [Body Parsing](https://docs.medusajs.com/learn/fundamentals/api-routes/parse-body/index.html.md) chapter.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -8649,7 +8649,7 @@ This returns the following JSON object:
|
||||
|
||||
You can apply validation rules on received body parameters to ensure they match specified rules and types.
|
||||
|
||||
Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/api-routes/validation#how-to-validate-request-body/index.html.md).
|
||||
Learn more in the [Body Validation](https://docs.medusajs.com/learn/fundamentals/api-routes/validation#how-to-validate-request-body/index.html.md) chapter.
|
||||
|
||||
|
||||
# Configure Request Body Parser
|
||||
@@ -9695,7 +9695,7 @@ The `startup` script will run every time you run the Medusa application.
|
||||
|
||||
# Seed Data with Custom CLI Script
|
||||
|
||||
In this chapter, you'll learn how to seed data using a custom CLI script.
|
||||
In this chapter, you'll learn how to seed data using a [custom CLI script](https://docs.medusajs.com/learn/fundamentals/custom-cli-scripts/index.html.md).
|
||||
|
||||
## How to Seed Data
|
||||
|
||||
@@ -9703,7 +9703,9 @@ To seed dummy data for development or demo purposes, use a custom CLI script.
|
||||
|
||||
In the CLI script, use your custom workflows or Medusa's existing workflows, which you can browse in [this reference](https://docs.medusajs.com/resources/medusa-workflows-reference/index.html.md), to seed data.
|
||||
|
||||
### Example: Seed Dummy Products
|
||||
***
|
||||
|
||||
## Example: Seed Dummy Products
|
||||
|
||||
In this section, you'll follow an example of creating a custom CLI script that seeds fifty dummy products.
|
||||
|
||||
@@ -9757,8 +9759,8 @@ export default async function seedDummyProducts({
|
||||
|
||||
So far, in the script, you:
|
||||
|
||||
- Resolve the Sales Channel Module's main service to retrieve the application's default sales channel. This is the sales channel the dummy products will be available in.
|
||||
- Resolve the Logger to log messages in the terminal, and Query to later retrieve data useful for the seeded products.
|
||||
- Resolve the [Sales Channel Module](https://docs.medusajs.com/resources/commerce-modules/sales-channel/index.html.md)'s service to retrieve the application's default sales channel. This is the sales channel the dummy products will be available in.
|
||||
- Resolve the [Logger](https://docs.medusajs.com/learn/debugging-and-testing/logging/index.html.md) to log messages in the terminal, and Query to later retrieve data useful for the seeded products.
|
||||
- Initialize some default data to use when seeding the products next.
|
||||
|
||||
Next, replace the `TODO` with the following:
|
||||
@@ -9768,7 +9770,7 @@ const productsData = new Array(productsNum).fill(0).map((_, index) => {
|
||||
const title = faker.commerce.product() + "_" + index
|
||||
return {
|
||||
title,
|
||||
is_giftcard: true,
|
||||
is_giftcard: false,
|
||||
description: faker.commerce.productDescription(),
|
||||
status: ProductStatus.PUBLISHED,
|
||||
options: [
|
||||
@@ -9834,7 +9836,7 @@ logger.info(`Seeded ${products.length} products.`)
|
||||
|
||||
You create the generated products using the `createProductsWorkflow` imported previously from `@medusajs/medusa/core-flows`. It accepts the product data as input, and returns the created products.
|
||||
|
||||
Only thing left is to create inventory levels for the products. So, replace the last `TODO` with the following:
|
||||
The only thing left is to create [inventory levels](https://docs.medusajs.com/resources/commerce-modules/inventory/concepts#inventorylevel/index.html.md) for the product variants. So, replace the last `TODO` with the following:
|
||||
|
||||
```ts title="src/scripts/demo-products.ts"
|
||||
logger.info("Seeding inventory levels.")
|
||||
@@ -9864,7 +9866,7 @@ await createInventoryLevelsWorkflow(container).run({
|
||||
logger.info("Finished seeding inventory levels data.")
|
||||
```
|
||||
|
||||
You use Query to retrieve the stock location, to use the first location in the application, and the inventory items.
|
||||
You use [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query/index.html.md) to retrieve the first stock location in the application and the inventory items.
|
||||
|
||||
Then, you generate inventory levels for each inventory item, associating it with the first stock location.
|
||||
|
||||
@@ -14095,25 +14097,31 @@ npx medusa db:migrate
|
||||
|
||||
# Query Context
|
||||
|
||||
In this chapter, you'll learn how to pass contexts when retrieving data with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query/index.html.md).
|
||||
In this chapter, you'll learn how to pass context when retrieving data with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query/index.html.md).
|
||||
|
||||
## What is Query Context?
|
||||
|
||||
Query context is a way to pass additional information when retrieving data with Query. This data can be useful when applying custom transformations to the retrieved data based on the current context.
|
||||
Query context is additional information you pass to Query when retrieving data. You can use it to apply custom transformations to the returned data based on the current context.
|
||||
|
||||
For example, consider you have a Blog Module with posts and authors. You can accept the user's language as a context and return the posts in the user's language. Another example is how Medusa uses Query Context to [retrieve product variants' prices based on the customer's currency](https://docs.medusajs.com/resources/commerce-modules/product/guides/price/index.html.md).
|
||||
For example, consider a Blog Module with posts and authors. You can send the user's language as context to Query to retrieve posts in the user's language.
|
||||
|
||||
Medusa also uses Query Context to [retrieve product variants' prices based on the customer's currency](https://docs.medusajs.com/resources/commerce-modules/product/guides/price/index.html.md).
|
||||
|
||||
***
|
||||
|
||||
## How to Use Query Context
|
||||
## How to Pass Query Context
|
||||
|
||||
The `query.graph` method accepts an optional `context` parameter that can be used to pass additional context either to the data model you're retrieving (for example, `post`), or its related and linked models (for example, `author`).
|
||||
The `query.graph` method accepts an optional `context` parameter. You can use this to pass additional context to the data model you're retrieving (for example, `post`) or its related and linked models (for example, `author`).
|
||||
|
||||
You initialize a context using `QueryContext` from the Modules SDK. It accepts an object of contexts as an argument.
|
||||
You can initialize a context using `QueryContext` from the Modules SDK. It accepts an object of contexts as an argument.
|
||||
|
||||
For example, to retrieve posts using Query while passing the user's language as a context:
|
||||
For example, to retrieve posts using Query while passing the user's language as context:
|
||||
|
||||
```ts highlights={highlights1}
|
||||
import { QueryContext } from "@medusajs/framework/utils"
|
||||
|
||||
// ...
|
||||
|
||||
```ts
|
||||
const { data } = await query.graph({
|
||||
entity: "post",
|
||||
fields: ["*"],
|
||||
@@ -14123,11 +14131,13 @@ const { data } = await query.graph({
|
||||
})
|
||||
```
|
||||
|
||||
In this example, you pass in the context a `lang` property whose value is `es`.
|
||||
In this example, you pass a `lang` property with the value `es` in the context. You create the context using `QueryContext`.
|
||||
|
||||
Then, to handle the context while retrieving records of the data model, in the associated module's service you override the generated `list` method of the data model.
|
||||
### How to Handle Query Context
|
||||
|
||||
For example, continuing the example above, you can override the `listPosts` method of the Blog Module's service to handle the context:
|
||||
To handle the Query context passed while retrieving records of your data models, override the [generated list method](https://docs.medusajs.com/resources/service-factory-reference/methods/list/index.html.md) of the associated module's service.
|
||||
|
||||
For example, continuing the example above, you can override the `listPosts` method of the Blog Module's service to handle the Query context:
|
||||
|
||||
```ts highlights={highlights2}
|
||||
import { MedusaContext, MedusaService } from "@medusajs/framework/utils"
|
||||
@@ -14166,19 +14176,17 @@ class BlogModuleService extends MedusaService({
|
||||
export default BlogModuleService
|
||||
```
|
||||
|
||||
In the above example, you override the generated `listPosts` method. This method receives as a first parameter the filters passed to the query, but it also includes a `context` property that holds the context passed to the query.
|
||||
In the above example, you override the generated `listPosts` method. This method receives the filters passed to `query.graph` as its first parameter. The first parameter includes a `context` property that holds the Query context alsopassed to `query.graph`.
|
||||
|
||||
You extract the context from `filters`, then retrieve the posts using the parent's `listPosts` method. After that, if the language is set in the context, you transform the titles of the posts.
|
||||
You extract the context from `filters`, then retrieve the posts using the parent's `listPosts` method. If the language is set in the context, you transform the post titles.
|
||||
|
||||
All posts returned will now have their titles appended with "en español".
|
||||
|
||||
Learn more about the generated `list` method in [this reference](https://docs.medusajs.com/resources/service-factory-reference/methods/list/index.html.md).
|
||||
|
||||
### Using Pagination with Query
|
||||
|
||||
If you pass pagination fields to `query.graph`, you must also override the `listAndCount` method in the service.
|
||||
If you pass pagination fields to `query.graph`, you must also override the [generated listAndCount method](https://docs.medusajs.com/resources/service-factory-reference/methods/listAndCount/index.html.md) in the service.
|
||||
|
||||
For example, following along with the previous example, you must override the `listAndCountPosts` method of the Blog Module's service:
|
||||
For example, following the previous example, you must override the `listAndCountPosts` method of the Blog Module's service:
|
||||
|
||||
```ts
|
||||
import { MedusaContext, MedusaService } from "@medusajs/framework/utils"
|
||||
@@ -14206,7 +14214,7 @@ class BlogModuleService extends MedusaService({
|
||||
)
|
||||
|
||||
if (context.lang === "es") {
|
||||
result.posts = posts.map((post) => {
|
||||
result[0] = result[0].map((post) => {
|
||||
return {
|
||||
...post,
|
||||
title: post.title + " en español",
|
||||
@@ -14221,15 +14229,15 @@ class BlogModuleService extends MedusaService({
|
||||
export default BlogModuleService
|
||||
```
|
||||
|
||||
Now, the `listAndCountPosts` method will handle the context passed to `query.graph` when you pass pagination fields. You can also move the logic to transform the posts' titles to a separate method and call it from both `listPosts` and `listAndCountPosts`.
|
||||
Now, the `listAndCountPosts` method will handle the context passed to `query.graph` when you pass pagination fields. You can also move the logic to transform the post titles to a separate method and call it from both `listPosts` and `listAndCountPosts`.
|
||||
|
||||
***
|
||||
|
||||
## Passing Query Context to Related Data Models
|
||||
|
||||
If you're retrieving a data model and you want to pass context to its associated model in the same module, you can pass them as part of `QueryContext`'s parameter, then handle them in the same `list` method.
|
||||
If you're retrieving a data model and want to pass Query context to its associated model in the same module, pass them as part of `QueryContext`'s parameter. Then, you can handle them in the same `list` method.
|
||||
|
||||
For linked data models, check out the [next section](#passing-query-context-to-linked-data-models).
|
||||
To pass Query context to linked data models, check out the [next section](#passing-query-context-to-linked-data-models).
|
||||
|
||||
For example, to pass a context for the post's authors:
|
||||
|
||||
@@ -14292,15 +14300,15 @@ class BlogModuleService extends MedusaService({
|
||||
export default BlogModuleService
|
||||
```
|
||||
|
||||
The context in `filters` will also have the context for `author`, which you can use to make transformations to the post's authors.
|
||||
The context in `filters` will also include the context for `author`, which you can use to transform the post's authors.
|
||||
|
||||
***
|
||||
|
||||
## Passing Query Context to Linked Data Models
|
||||
|
||||
If you're retrieving a data model and you want to pass context to a linked model in a different module, pass to the `context` property an object instead, where its keys are the linked model's name and the values are the context for that linked model.
|
||||
If you're retrieving a data model and want to pass Query context to a linked model in a different module, pass an object to the `context` property instead. The object's keys should be the linked model's name, and the values should be the Query context for that linked model.
|
||||
|
||||
For example, consider the Product Module's `Product` data model is linked to the Blog Module's `Post` data model. You can pass context to the `Post` data model while retrieving products like so:
|
||||
For example, consider the Product Module's `Product` data model is linked to the Blog Module's `Post` data model. You can pass context to the `Post` data model while retrieving products:
|
||||
|
||||
```ts highlights={highlights5}
|
||||
const { data } = await query.graph({
|
||||
@@ -14314,9 +14322,9 @@ const { data } = await query.graph({
|
||||
})
|
||||
```
|
||||
|
||||
In this example, you retrieve products and their associated posts. You also pass a context for `post`, indicating the customer's language.
|
||||
In this example, you retrieve products and their associated posts. You also pass Query context for `post`, indicating the customer's language.
|
||||
|
||||
To handle the context, you override the generated `listPosts` method of the Blog Module as explained [previously](#how-to-use-query-context).
|
||||
To handle the context, override the generated `listPosts` method of the Blog Module as explained [previously](#how-to-handle-query-context).
|
||||
|
||||
|
||||
# Query
|
||||
|
||||
Reference in New Issue
Block a user