docs: document auto-generated types (#13050)
This commit is contained in:
1 parent
5a01ef89ea
commit
0d8c2c4056
6 files changed
+205
-2
No files matched your search
@@ -11132,6 +11132,85 @@ To learn more about the different concepts useful for building plugins, check ou
|
||||
- [Plugins](https://docs.medusajs.com/learn/fundamentals/plugins/index.html.md)
|
||||
|
||||
|
||||
# Automatically Generated Types
|
||||
|
||||
In this chapter, you'll learn about the types Medusa automatically generates under the `.medusa` directory and how you should use them.
|
||||
|
||||
## What are Automatically Generated Types?
|
||||
|
||||
Medusa automatically generates TypeScript types for:
|
||||
|
||||

|
||||
|
||||
1. Data models collected in the [Query's graph](https://docs.medusajs.com/learn/fundamentals/module-links/query#querying-the-graph/index.html.md). These types provide you with auto-completion and type checking when using Query.
|
||||
- Generated data model types are located in the `.medusa/types/query-entry-points.d.ts` file.
|
||||
2. Modules registered in the [Medusa container](https://docs.medusajs.com/learn/fundamentals/medusa-container/index.html.md). These types provide you with auto-completion and type checking when resolving modules from the container.
|
||||
- Generated module registration names are located in the `.medusa/types/modules-bindings.d.ts` file.
|
||||
|
||||
***
|
||||
|
||||
## How to Trigger Type Generation?
|
||||
|
||||
The Medusa application generates these types automatically when you run the application with the `dev` command:
|
||||
|
||||
```bash npm2yarn
|
||||
npm run dev
|
||||
```
|
||||
|
||||
So, if you add a new data model or module and you don't find it in auto-completion or type checking, you can run the `dev` command to regenerate the types.
|
||||
|
||||
***
|
||||
|
||||
## How to Use the Generated Types?
|
||||
|
||||
The generated types are only meant for auto-completion and type checking.
|
||||
|
||||
For example, consider you have a Brand Module with a `Brand` data model. Due to the auto-generated types, you can do the following:
|
||||
|
||||
### Query
|
||||
|
||||
```ts
|
||||
const { data: [brand] } = await query.graph({
|
||||
entity: "brand",
|
||||
fields: ["*"],
|
||||
filters: {
|
||||
id: "brand_123",
|
||||
},
|
||||
})
|
||||
|
||||
// brands has the correct type, so you can access its properties with auto-completion and type checking
|
||||
console.log(brand.name)
|
||||
```
|
||||
|
||||
### Container
|
||||
|
||||
```ts
|
||||
const brandModuleService = req.scope.resolve("brand")
|
||||
|
||||
// brandModuleService has the correct type, so you can access its methods with auto-completion and type checking
|
||||
const brand = await brandModuleService.retrieveBrand("brand_123")
|
||||
```
|
||||
|
||||
### Don't Import the Generated Types
|
||||
|
||||
The generated types are only meant to help you in your development process by providing auto-completion and type checking. You should not import them directly in your code.
|
||||
|
||||
Since you don't commit the `.medusa` directory to your version control system or your production environment, importing these types may lead to build errors.
|
||||
|
||||
Instead, if you need to use a data model's type in your customizations, you can use the [InferTypeOf](https://docs.medusajs.com/learn/fundamentals/data-models/infer-type/index.html.md) utility function, which infers the type of a data model based on its properties.
|
||||
|
||||
For example, if you want to use the `Brand` data model's type in your customizations, you can do the following:
|
||||
|
||||
```ts
|
||||
import { InferTypeOf } from "@medusajs/framework/types"
|
||||
import { Brand } from "../modules/brand/models/brand" // relative path to the model
|
||||
|
||||
export type BrandType = InferTypeOf<typeof Brand>
|
||||
```
|
||||
|
||||
You can then use the `BrandType` type in your customizations, such as in workflow inputs or service method outputs.
|
||||
|
||||
|
||||
# Medusa Container
|
||||
|
||||
In this chapter, you’ll learn about the Medusa container and how to use it.
|
||||
@@ -11281,6 +11360,16 @@ A [module](https://docs.medusajs.com/learn/fundamentals/modules/index.html.md),
|
||||
|
||||
Learn more about the module's container in [this chapter](https://docs.medusajs.com/learn/fundamentals/modules/container/index.html.md).
|
||||
|
||||
***
|
||||
|
||||
## Container Registration Keys Auto-Completion
|
||||
|
||||
When you resolve a resource from the Medusa container, you can use auto-completion to find the registration key of the resource.
|
||||
|
||||
This is possible due to Medusa's auto-generated types, which are generated in the `.medusa` directory when you run the `dev` command.
|
||||
|
||||
Learn more in the [Auto-Generated Types](https://docs.medusajs.com/learn/fundamentals/generated-types/index.html.md) chapter.
|
||||
|
||||
|
||||
# Add Columns to a Link Table
|
||||
|
||||
|
||||
Reference in new issue
Block a user