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.

NoteLearn more about selecting fields and relations in the API Reference.

For example, send the following request to retrieve the list of products with their brands:

Code
1curl 'http://localhost:9000/admin/products?fields=+brand.*' \2--header 'Authorization: Bearer {token}'
TipMake sure to replace {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:

Example Product Object
1{2  "id": "prod_123",3  // ...4  "brand": {5    "id": "01JEB44M61BRM3ARM2RRMK7GJF",6    "name": "Acme",7    "created_at": "2024-12-05T09:59:08.737Z",8    "updated_at": "2024-12-05T09:59:08.737Z",9    "deleted_at": null10  }11}

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.

NoteLearn more about Query in this chapter.

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:

src/api/admin/brands/route.ts
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 of model.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.

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:

Code
1curl 'http://localhost:9000/admin/brands' \2-H 'Authorization: Bearer {token}'
TipMake sure to replace {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:

Example Response
1{2  "brands": [3    {4      "id": "123",5      // ...6      "products": [7        {8          "id": "prod_123",9          // ...10        }11      ]12    }13  ]14}

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.

Was this chapter helpful?
Edit this page