- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
2.2.3. Guide: Query Product's Brands
In the previous chapters, you defined a link between the custom Brand Module and Medusa's Product Module, then extended the create-product flow to link a product to a brand.
In this chapter, you'll learn how to retrieve a product's brand (and vice-versa) in two ways: Using Medusa's existing API route, or in customizations, such as a custom API route.
Approach 1: Retrieve Brands in Existing API Routes#
Medusa's existing API routes accept a fields
query parameter that allows you to specify the fields and relations of a model to retrieve. So, when you send a request to the List Products, Get Product, or any product-related store or admin routes that accept a fields
query parameter, you can specify in this parameter to return the product's brands.
For example, send the following request to retrieve the list of products with their brands:
{token}
with your admin user's authentication token. Learn how to retrieve it in the API reference.Any product that is linked to a brand will have a brand
property in its object:
By using the fields
query parameter, you don't have to re-create existing API routes to get custom data models that you linked to core data models.
Approach 2: Use Query to Retrieve Linked Records#
You can also retrieve linked records using Query. Query allows you to retrieve data across modules with filters, pagination, and more. You can resolve Query from the Medusa container and use it in your API route or workflow.
For example, you can create an API route that retrieves brands and their products. If you followed the Create Brands API route chapter, you'll have the file src/api/admin/brands/route.ts
with a POST
API route. Add a new GET
function to the same file:
1// other imports...2import {3 MedusaRequest,4 MedusaResponse,5} from "@medusajs/framework/http"6 7export const GET = async (8 req: MedusaRequest,9 res: MedusaResponse10) => {11 const query = req.scope.resolve("query")12 13 const { data: brands } = await query.graph({14 entity: "brand",15 fields: ["*", "products.*"],16 })17 18 res.json({ brands })19}
This adds a GET
API route at /admin/brands
. In the API route, you resolve Query from the Medusa container. Query has a graph
method that runs a query to retrieve data. It accepts an object having the following properties:
entity
: The data model's name as specified in the first parameter ofmodel.define
.fields
: An array of properties and relations to retrieve. You can pass:- A property's name, such as
id
, or*
for all properties. - A relation or linked model's name, such as
products
(use the plural name since brands are linked to list of products). You suffix the name with.*
to retrieve all its properties.
- A property's name, such as
graph
returns an object having a data
property, which is the retrieved brands. You return the brands in the response.
Test it Out#
To test the API route out, send a GET
request to /admin/brands
:
{token}
with your admin user's authentication token. Learn how to retrieve it in the API reference.This returns the brands in your store with their linked products. For example:
Summary#
By following the examples of the previous chapters, you:
- Defined a link between the Brand and Product modules's data models, allowing you to associate a product with a brand.
- Extended the create-product workflow and route to allow setting the product's brand while creating the product.
- Queried a product's brand, and vice versa.
Next Steps: Customize Medusa Admin#
Clients, such as the Medusa Admin dashboard, can now use brand-related features, such as creating a brand or setting the brand of a product.
In the next chapters, you'll learn how to customize the Medusa Admin to show a product's brand on its details page, and to show a new page with the list of brands in your store.