#how to expand collection products relation from store api

33 messages ยท Page 1 of 1 (latest)

vernal scaffold
#

When we try to access products of product collection with expand: "collection.products" using list products, we get an error

Is it possible to expand the relation ?

We are using the collection as a super product with parent product and children within the same collection as a workaround for variants specific images

tardy pulsar
#

Looks like collections doesn't support that expand query

#
GitHub

Building blocks for digital commerce. Contribute to medusajs/medusa development by creating an account on GitHub.

GitHub

Building blocks for digital commerce. Contribute to medusajs/medusa development by creating an account on GitHub.

#

You could open a PR with that change and I can guide you through the process.

vernal scaffold
#

Hello @tardy pulsar,
Thank you for your response. I'm a bit nervous since this is my first contribution, but I'm eager to do my best and contribute to MedusaJs. I appreciate any guidance you can offer.

tardy pulsar
#

Nothing to be nervous at all. ๐Ÿ™‚ Open up a PR and send em my way, I can guide you along

#

This basically:

export class StoreGetCollectionsParams extends FindParams {...}
vernal scaffold
tardy pulsar
#

nice! ๐Ÿ”ฅ

#

you don't have to recreate the FindParams class

#

you can just import it import { FindParams } from "../../../../types/common"

#

you'll have to change the folder path depending on where you are

vernal scaffold
#

Ok I will do it right away

#

Done and committed to PR @tardy pulsar

tardy pulsar
#

nice, you just have to remove this now

vernal scaffold
#

Yes! alright

#

Done ๐Ÿ™‚

tardy pulsar
#

perfect!

vernal scaffold
#

Yes of course, I have to finish something and I will do it

tardy pulsar
#

cool, and this is the last thing.

#

just add the changset, you can copy the same as the screenshot ^

#

Should be good to go from there ๐Ÿ™‚

vernal scaffold
#

Ok, thank you

vernal scaffold
#

@tardy pulsar I got some delay but I will do it tomorrow. ๐Ÿ‘Œ

tardy pulsar
#

Looking forward to it @vernal scaffold ! ๐ŸŽ‰

vernal scaffold
#

Hello @tardy pulsar,
I added this test to : integration-tests\api_tests_\store\collections.js
describe("/store/collections?

expand=products&fields=handle,name,description", () => {
beforeEach(async () => {
await productSeeder(dbConnection)
})

afterEach(async () => {
  const db = useDb()
  await db.teardown()
})

it("lists collections with products relation expanded and allowed fields", async () => {
  const api = useApi()

  const response = await api.get(
    "/store/collections?expand=products&fields=handle,name,description"
  )

  expect(response.data).toMatchSnapshot({
    collections: [
      {
        id: "test-collection2",
        created_at: expect.any(String),
        updated_at: expect.any(String),
        handle: expect.any(String),
        name: expect.any(String),
        description: expect.any(String),
        products: expect.any(Array),
      },
      {
        id: "test-collection1",
        created_at: expect.any(String),
        updated_at: expect.any(String),
        handle: expect.any(String),
        name: expect.any(String),
        description: expect.any(String),
        products: expect.any(Array),
      },
      {
        id: "test-collection",
        created_at: expect.any(String),
        updated_at: expect.any(String),
        handle: expect.any(String),
        name: expect.any(String),
        description: expect.any(String),
        products: expect.any(Array),
      },
    ],
    count: 3,
    limit: 10,
    offset: 0,
  })
})

})

and when I run yarn test:integration:api,
I am getting this error : (message too long)

#

integration-tests-api:test:integration: Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
integration-tests-api:test:integration: at Object.continueSession (D:\Developpement\ghilas\medusa-core\medusa-dev\node_modules\pg\lib\sasl.js:25:11)
integration-tests-api:test:integration: at Client._handleAuthSASLContinue (D:\Developpement\ghilas\medusa-core\medusa-dev\node_modules\pg\lib\client.js:318:14)
integration-tests-api:test:integration: at Connection.emit (node:events:513:28)
integration-tests-api:test:integration: at D:\Developpement\ghilas\medusa-core\medusa-dev\node_modules\pg\lib\connection.js:161:16
integration-tests-api:test:integration: at Parser.parse (D:\Developpement\ghilas\medusa-core\medusa-dev\node_modules\pg-protocol\dist\parser.js:40:17)
integration-tests-api:test:integration: at Socket.<anonymous> (D:\Developpement\ghilas\medusa-core\medusa-dev\node_modules\pg-protocol\dist\index.js:11:42)
integration-tests-api:test:integration: at Socket.emit (node:events:513:28)
integration-tests-api:test:integration: at addChunk (node:internal/streams/readable:324:12)
integration-tests-api:test:integration: at readableAddChunk (node:internal/streams/readable:297:9)
integration-tests-api:test:integration: at Readable.push (node:internal/streams/readable:234:10)
integration-tests-api:test:integration: ERROR: command finished with error: command (D:\Developpement\ghilas\medusa-core\medusa-dev\integration-tests\api) yarn run test:integration exited (1)
command (D:\Developpement\ghilas\medusa-core\medusa-dev\integration-tests\api) yarn run test:integration exited (1)

Tasks: 13 successful, 14 total
Cached: 13 cached, 14 total
Time: 23.271s

ERROR run failed: command exited (1)

#

I committed the test to let you run it if possible

tardy pulsar
#

before you do anything else, run:
yarn && yarn build from the root

and then you will have to cd into this folder

cd integration-tests/api

and then run
yarn test:integration __tests__/store/collections.js

#

I've also added some comments on the PR

#

@vernal scaffold

vernal scaffold
#

Thank you riqwan ๐Ÿ™‚ I will do it right away