Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriber for category created not having access to data #9650

Closed
renequez opened this issue Oct 17, 2024 · 7 comments
Closed

Subscriber for category created not having access to data #9650

renequez opened this issue Oct 17, 2024 · 7 comments

Comments

@renequez
Copy link

Bug report

Describe the bug

Added a subscriber on the product-category.created event and if I try to retrieve the new category's data with retrieveProductCategory, the result only holds the id, with the rest of the category's data missing:

{
  "id": "pcat_01JADXBS150J9W5P0YBX625AE0",
  "mpath": "pcat_01JADXBS150J9W5P0YBX625AE0",
  "parent_category_id": null,
  "category_children": []
}

System information

Medusa version (including plugins): rc 6
Node.js version: 20.17.0
Database: PostgreSQL 16.4
Operating system: Windows 11
Browser (if relevant): Edge

Steps to reproduce the behavior

  1. Start a new Medusa project with npx create-medusa-app@rc.
  2. Create the subscriber from the code snippets below.
  3. Create a category in the Admin Panel.

Expected behavior

I'd expect to have the rest of the category data in the result, like title, handle, whatever field was completed in the create form and is also in the database.

Code snippets

import { SubscriberArgs } from '@medusajs/framework';
import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils';

import type { SubscriberConfig } from '@medusajs/framework';

export default async function categoryCreateHandler({
   event: { data },
   container,
}: SubscriberArgs<{ id: string }>) {
   const productModuleService = container.resolve(Modules.PRODUCT);
   const logger = container.resolve(ContainerRegistrationKeys.LOGGER);

   logger.info('Created category..');

   const category = await productModuleService.retrieveProductCategory(data.id);

   logger.info(JSON.stringify(category, null, 2));
}

export const config: SubscriberConfig = {
   event: 'product-category.created',
};
@renequez
Copy link
Author

renequez commented Oct 18, 2024

The same issue is happening with product-category.updated. The retrieved category shows the old data instead of the updated information.

@olivermrbl
Copy link
Contributor

olivermrbl commented Oct 20, 2024

@carlos-r-l-rodrigues, @adrien2p – this is an interesting case.

I just ran two different scenarios:

  • Retrieve product category without field selection (as presented in the issue description)
const category = await productModuleService.retrieveProductCategory(data.id);
logger.info(JSON.stringify(category, null, 2));

This consistently returns the old data of the product category.

  • Retrieve product category with field selection
const category = await productModuleService.retrieveProductCategory(
  data.id, 
  { select: ["id", "name"] }
);
logger.info(JSON.stringify(category, null, 2));

This returns the expected updated category.

Any idea what might be happening here? Afaict, it cannot be related to MikroORM caching and the identity map. We don't use the former, and at this point, the identity map is flushed. Let me know if I am missing something.

@olivermrbl
Copy link
Contributor

@renequez, we'll look into this. In the meantime, I recommend you use Query whenever you need to access data in your application. In your case, you would need to do something like:

const query = container.resolve("query")
const { data: result } = await query.graph({
  entity: "product_category",
  filters: { id: data.id },
  fields: ["id", "name", ...]
})

Read more here.

@adrien2p
Copy link
Member

@olivermrbl the behaviour described here sounds indeed weird, let me have a look tomorrow

@linear linear bot added type: chore and removed version: 2.0 labels Oct 21, 2024
@adrien2p
Copy link
Member

Hi @renequez, thank you for reporting that one 💪
I ve created a fix and I am generating a preview that you can then play with if you want #9688

@renequez
Copy link
Author

Hi guys, thanks for the prompt answers 🚀

I've installed the snapshot from #9688. It works as expected when using Query as @olivermrbl suggested, but if I use retrieveProductCategory there's still a weird behavior: if I don't pass a select, it brings me just the id, mpath, parent_category_id and category_children columns. If i pass select, then it works fine 👌

All in all, I'll start using Query whenever I need to retrieve data. Is this the recommended way, basically to avoid the retrieve functions of module services?

@olivermrbl
Copy link
Contributor

@renequez, yes, I recommend using Query so you can make a habit of it. It has access to all data in the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants