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

fix(core): It is possible for featuredAsset to resolve to {id: ID} #3177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

taxilian
Copy link
Contributor

Description

We experienced an issue with trying to add products to an existing order; when we added a new product which was not previously in the order, the featuredAsset would resolve to an object with an ID and nothing else. We have this fix locally as a resolver, but it's simple and innocuous enough I thought I'd submit it for your consideration to help others.

If this isn't the right way to do this I apologize; we're still new-ish to this project. Let me know how to do it better and I'll fix it =]

Breaking changes

None

Screenshots

N/A

Checklist

📌 Always:

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have checked my own PR

👍 Most of the time:

  • I have added or updated test cases
    - (this might not be a bad idea, but I don't know how to reproduce it with a test case)
  • I have updated the README if needed N/A

Copy link

vercel bot commented Oct 31, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview Nov 16, 2024 0:10am

@taxilian
Copy link
Contributor Author

taxilian commented Nov 1, 2024

Hmm. These failing tests certainly suggest this isn't the correct solution for backend, but I'm at a bit of a loss since it works fine in my code. I'm attempting to reproduce the failure on my system so I can hopefully address it.

@taxilian
Copy link
Contributor Author

taxilian commented Nov 1, 2024

I am unable to reproduce this issue running e2e tests on my system and would appreciate any guidance.

@@ -32,6 +34,18 @@ export class OrderLineEntityResolver {
@Parent() orderLine: OrderLine,
): Promise<Asset | undefined> {
if (orderLine.featuredAsset !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the tests are failing because you are doing an exact !== check for undefined. So if orderLine.featuredAsset is null, then the "if" statement will be true, and the program will go to line 40 and attempt to access featuredAsset.preview, which results in the error you see in the e2e tests:

Error: Cannot read properties of null (reading 'preview')

So the solution should be to use !=, which will check for both null and undefined.

@taxilian taxilian force-pushed the fix/featuredAssetMissing branch 2 times, most recently from 43419f3 to e0665ef Compare November 14, 2024 21:02
@taxilian
Copy link
Contributor Author

we'll give that a try... if we're just checking for "truthyness" I'd rather it was specifically doing that, rather than the != which does the same thing but doesn't look like it.

Copy link
Member

@michaelbromley michaelbromley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, test are all passing now. I have one further suggestion to simplify things.

@@ -31,7 +33,19 @@ export class OrderLineEntityResolver {
@Ctx() ctx: RequestContext,
@Parent() orderLine: OrderLine,
): Promise<Asset | undefined> {
if (orderLine.featuredAsset !== undefined) {
if (!!orderLine.featuredAsset) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought more about this and I think it can be simplified even further to:

if (!orderLine.featuredAsset?.preview) {
  return orderLine.featuredAsset;
} else {
  return this.assetService.getFeaturedAsset(ctx, orderLine);
}

reasoning:

  1. less code
  2. fewer logical paths == easier to reason about
  3. if any future refactors occur to how we fetch a featured asset, they will automatically apply to this code path == easier maintenance.

The comment is still worth having though to explain why we explicitly check for the presence of the preview property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense; I was stuck on "change the least you need to" thinking that if featuredAsset is not truthy then we should avoid calling getFeaturedAsset, but I trust that you know better than I if there are any legitimate cases where that would happen, so I'll make and push the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I'm reading wrong, though, you have your logic backwards, and it should be:

if (!orderLine.featuredAsset?.preview) {
    return this.assetService.getFeaturedAsset(ctx, orderLine);
} else {
    return orderLine.featuredAsset;
}

Copy link

sonarcloud bot commented Nov 16, 2024

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

Successfully merging this pull request may close these issues.

2 participants