Skip to content

Preview Release Update #10

Compare
Choose a tag to compare
@olivermrbl olivermrbl released this 18 Sep 08:19
· 432 commits to develop since this release
56ee4d6

Update existing project

Ensure your Medusa dependencies in package.json are using the preview tag:

{
  "dependencies": {
    "@medusajs/medusa": "preview",
    "@medusajs/pricing": "preview",
    "@medusajs/product": "preview",
    ...
  }
}

To ensure an upgrade to a new version is completed correctly, run the following sequence of commands:

rm -rf node_modules
rm yarn.lock // or package-lock.json

yarn // If you are using yarn berry, you need to create the lock-file first

Highlights

Introduced a new query tool

We have introduced a new tool, Query, for querying data across modules in your application. Over time, it will replace Remote Query, which has been deprecated in the latest version. The underlying query engine of Query and Remote Query is the same, but we have changed the query API and eliminated redundant configurations that had built up over time.

There are two significant differences between the two tools:

  • The result returned by Query takes the same shape regardless of the query input, which is different from that of Remote Query's that differed depending on whether pagination was applied or not.
  • The call signature of Query is query.graph({ ... }), which is different from Remote Query's query({ ... })

Query Result

With Query, you will always receive a result with data and metadata. The data contains your requested resources and the metadata contains information about the applied pagination. We recommend consuming it like so:

const { data, metadata } = await query...

Call signature

With Query, you query data across your modules with query.graph({ ... }).

For example, in an API Route, the difference between how you consume Query vs. Remote Query is as follows:

// API Route
-const query = req.container.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
- 
-const result = await query({ ... })
+const query = req.container.resolve(ContainerRegistrationKeys.QUERY)
+
+const { data, metadata } = await query.graph({ ... })

Migrating to Query

To migrate from Remote Query to Query, the following changes are needed:

  • Resolve query instead of remoteQuery from the dependency container
  • Use query.graph({ ... }) instead of query({ .. })
  • Update query options to fit the new format*

*The changes to the query options format are:

  • Entrypoint has been renamed: entryPoint -> entity
  • Filters has been moved to a top-level option: variables: { filters: { ... } } -> filters: { ... }
  • Pagination has been moved to a top-level option: variables: { pagination: { ... } } -> pagination: { ... }
  • Context has been moved to a top-level option: variables: { context: { ... } } -> context: { ... }
  • Variables has been removed entirely from the API

Here's an example using all options:

const { data, metadata } = await query.graph({
  entity: "product",
  fields: ["id", "title", "variants.calculated_price"],
  filters: { 
    variants: { 
      sku: "SHIRT-1234" 
    },
  },
  pagination: { take: 10, skip: 0 },
  context: {
    variants: {
      calculated_price: QueryContext({ currency_code: "usd" })
    }
  }
})

In this example, we:

  • Retrieve the first 10 products that match our query
  • Only with the fields: id, title, and the calculated price of variants, variants.calculated_price
  • Filtered by product variants sku
  • With computed variant prices based on a dynamic QueryContext

Alongside the tool, we have shipped a new option, types, to our CLI commands start and develop. If specified, we will attempt to generate types for all data models in existing and custom modules in your project and place them in a new top-level folder .medusa in your project. The types should significantly improve the developer experience of Query by giving you intellisense of the data you can query in your application.

You can read more about Query in our documentation.

Remote Query will be removed in a later preview version and will not be part of the official release of Medusa 2.0. However, because the required changes are minimal, we recommend upgrading now to avoid issues in the future.

Introduced observability

We have introduced observability into our framework, enabling traces for HTTP requests, workflow executions, and database queries. Our instrumentation is built on top of OpenTelemetry, which allows you to export traces to any platform compatible with OpenTelemetry events.

Read more about tracing in Medusa and how to get started in our documentation.

Features

Bugs

Documentation

Other Changes

  • docs: Rename remoteQuery to query and add db instrumentation flag by @thetutlage in #9159

Full Changelog: v2.0.9-preview...v2.0.10