4.2.4. Retrieve Brand linked to Product using Query

Example ChapterThis chapter covers how to retrieve the brand linked to a product using Query as a step of the "Extend Models" chapter .

What is Query?#

Query is a utility that retrieves data across modules and their links. It’s registered in the Medusa container under the ContainerRegistrationKeys.QUERY (query) registration name.


Retrieve Brand of Product API Route#

You'll create an API route that retrieves the brand of a product. You'll use this in a later chapter.

Create the file src/api/admin/products/[id]/brand/route.ts with the following content:

src/api/admin/products/[id]/brand/route.ts
7} from "@medusajs/framework/utils"8
9export const GET = async (10  req: MedusaRequest,11  res: MedusaResponse12) => {13  const query = req.scope.resolve(14    ContainerRegistrationKeys.QUERY15  )16
17  const { data: [product] } = await query.graph({18    entity: "product",19    fields: ["brand.*"],20    filters: {21      id: req.params.id,22    },23  })24
25  res.json({ brand: product.brand })26}

In this example, you retrieve a product by its ID with its brand, and return the brand in the response.

query.graph Parameters#

The graph method of Query 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.
    • A relation or linked model's name, such as brand. You suffix the name with .* to retrieve all its properties.
  • filters: An object of filters to apply on the retrieved data model's properties.
NoteFilters currently don't work on models of another module, such as brand in this example.

Test it Out#

To test the API route out, first, retrieve the authentication token of your admin user by sending a POST request to /auth/user/emailpass:

Code
1curl -X POST 'http://localhost:9000/auth/user/emailpass' \2-H 'Content-Type: application/json' \3--data-raw '{4    "email": "admin@medusa-test.com",5    "password": "supersecret"6}'

Make sure to replace the email and password with your user's credentials.

Then, send a GET request to /admin/products/:id/brand:

Code
1curl 'http://localhost:9000/admin/products/prod_123/brand' \2-H 'Authorization: Bearer {token}'

This returns the product's brand if it has one. For example:

Example Response
1{2  "brand": {3    "id": "123",4    "name": "Acme",5    // ...6  }7}

Retrieve Products of a Brand#

An example of retrieving the products of a brand:

Code
1const query = req.scope.resolve(2  ContainerRegistrationKeys.QUERY3)4
5const { data: [brand] } = await query.graph({6  entity: "brand",7  fields: ["products.*"],8  filters: {9    id: req.params.id,10  },11})

In this case, since a brand has multiple products, you specify the plural name of the Product data model (products) in fields.

The retrieved brand now has a products field, which is an array of products linked to it:

Example Response
1{2  "brand": {3    "products": [4      // ...5    ]6  }7}

Summary#

By following the examples of the previous chapters, you:

  • Defined a link between the Brand and Product modules's data models, as if you're extending the Product model to add a brand.
  • Created a link between brand and product records.
  • Queried the brand linked to a product, and vice versa.

Next Steps#

In the next chapters, you'll learn how to customize the Medusa Admin to show brands.

Was this chapter helpful?
Edit this page