medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.22k stars 2.66k forks source link

[Bug]: `remoteQueryConfig.fields` of `MedusaRequest` should resolve proper type from generic body #9976

Open noubase opened 3 weeks ago

noubase commented 3 weeks ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@dnd-kit/modifiers": "^7.0.0",
    "@medusajs/admin-sdk": "2.0.2",
    "@medusajs/cli": "2.0.2",
    "@medusajs/framework": "2.0.2",
    "@medusajs/medusa": "2.0.2",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "axios-retry": "^4.5.0",
    "contentful-management": "11.35.1",
    "contentful-rich-text-html-parser": "^1.5.13",
    "lodash": "^4.17.21",
    "pg": "^8.13.1",
    "react-icons": "^5.3.0",
    "xmlrpc": "^1.3.2"
  },
  "devDependencies": {
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/lodash": "^4.17.12",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "medusa-test-utils": "rc",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.18.0

Database and its version

postgres (PostgreSQL) 16.3

Operating system name and version

Mac OS X

Browser name

No response

What happended?

You now get an error when trying to use fields from a remoteQueryConfig request within the RemoteQuery graph.

image

Expected behavior

fields should resolve the correct type from the generic body.

Actual behavior

It's just a string[] array. I have to use @ts-ignore to avoid errors during the build.

Link to reproduction repo

No need for

thetutlage commented 3 weeks ago

Hello @noubase 👋

Can you please share this custom route or a repo that I can use to reproduce the issue?

thetutlage commented 2 weeks ago

Closing since no response from the issue reporter and not actionable as well. However, feel free to leave a comment if you are still having the same issue and can provide a reproduction of the same :)

noubase commented 1 week ago

There is absolutely no need in custom repo

Create a route like this

type AdminGetOrdersParamsType = z.infer<typeof AdminGetOrdersParams>;
const AdminGetOrdersParams = createFindParams({
  limit: 15,
  offset: 0,
})

export async function GET(
    req: MedusaRequest<AdminGetOrdersParamsType>,
    res: MedusaResponse
): Promise<void> {

  const q = req.scope.resolve(ContainerRegistrationKeys.QUERY)
  const {data} = await q.graph({
    entity: 'order',
    fields: req.remoteQueryConfig.fields,
    filters: {
      id: req.params.id
    }
  }, {throwIfKeyNotFound: true, throwIfRelationNotFound: true})
  res.json(data)
}

And you will immediately get an error

image
noubase commented 1 week ago

That's because fields is always just an array of strings

image
noubase commented 1 week ago

And fields in RemoteQueryInput is a sophisticated generic type

image
vholik commented 1 week ago

@thetutlage Can confirm that on the newest 2.0 version MedusaRequest generic do not populate with correct fields type