Back to Medusa

{metadata.title}

www/apps/resources/app/lint/rules/no-wildcard-with-specific-fields/page.mdx

2.18.02.9 KB
Original Source

export const metadata = { title: no-wildcard-with-specific-fields - ESLint plugin rules, }

{metadata.title}

This rule disallows combining the * wildcard with specific top-level fields in a query.graph or useQueryGraphStep field selection, because * silently drops those specific field selections.

<Note>

This rule is available since Medusa v2.18.0.

</Note>

Severity

warn. This rule is enabled in the recommended preset.

What it Targets

This rule targets usages of query.graph and useQueryGraphStep anywhere in your project. It reports a fields array that contains both the * wildcard and one or more specific top-level fields.

The following code is reported by the rule:

ts
const { data } = await query.graph({
  entity: "cart",
  fields: ["*", "total"],
})

Remove the specific top-level fields if you want all of the entity's own fields, since * already selects them:

<Note title="Important">

It's highly discouraged to use * in production code, as it can lead to performance issues with large data models. It's better to explicitly list the fields you need.

</Note>
ts
const { data } = await query.graph({
  entity: "cart",
  fields: ["*"],
})

Or remove * and list only the fields you need, including any computed fields:

ts
const { data } = await query.graph({
  entity: "cart",
  fields: ["id", "total", "subtotal"],
})

Relation selections such as items.* or items.title are not affected by this rule. They are scoped to a relation and are kept even when * is present, so the following is valid:

ts
const { data } = await query.graph({
  entity: "cart",
  fields: ["*", "items.*", "items.title"],
})

Why it's Important

The * wildcard selects all of an entity's own scalar fields. It does not include computed fields such as total or subtotal. When you combine * with a specific top-level field like "total", the specific field is silently ignored and the response won't contain it.

Listing only the fields you need, without *, is the safest approach when you require computed or non-default fields.

Fixable

This rule isn't auto-fixable.

Turn it Off

To turn off this rule, set it to off in your ESLint configuration:

ts
import { defineConfig } from "eslint/config"
import medusa from "@medusajs/eslint-plugin"

export default defineConfig([
  ...medusa.configs.recommended,
  {
    rules: {
      "@medusajs/no-wildcard-with-specific-fields": "off",
    },
  },
])

Or disable it for a single line using an inline comment:

ts
// eslint-disable-next-line @medusajs/no-wildcard-with-specific-fields
const { data } = await query.graph({ entity: "cart", fields: ["*", "total"] })