#how to expand collection products relation from store api
33 messages ยท Page 1 of 1 (latest)
Looks like collections doesn't support that expand query
You need to extend this class (https://github.com/medusajs/medusa/blob/develop/packages/medusa/src/api/routes/store/collections/list-collections.ts#L121)
with this class (https://github.com/medusajs/medusa/blob/develop/packages/medusa/src/types/common.ts#L399)
You could open a PR with that change and I can guide you through the process.
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.
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 {...}
This is the pull request @tardy pulsar ๐ซฃ https://github.com/medusajs/medusa/pull/4608
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
nice, you just have to remove this now
perfect!
2 more things:
- Can you write a test similar to this in the same file? - https://github.com/medusajs/medusa/blob/develop/integration-tests/api/__tests__/store/collections.js#L61
Write ?expand=somerelation&field=somefield at the end of the url and check if the response is expected
Yes of course, I have to finish something and I will do it
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 ๐
Ok, thank you
@tardy pulsar I got some delay but I will do it tomorrow. ๐
Looking forward to it @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
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
Thank you riqwan ๐ I will do it right away