-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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(visitor-plugin-common): avoid reading from null values #9709
Conversation
🦋 Changeset detectedLatest commit: 51798c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0; | ||
const relevant = grouped[typeName].filter(Boolean); | ||
|
||
if (relevant.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relevant.length === 0
will be true
for [null]
so it exits before declaring hasUnions
.
Tests are passing but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a failing test case?
PR algolia breaks on forks because it needs write access. I wouldn’t worry about this |
I think I've found the combination that makes it fail. It works when you do at least one of these:
When this whole situation happens, the I don't have enough context to understand why this array now contains |
For some reason, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
Description
tl;dr
visitor-plugin-common
tries to read from a variable that can be null in some situations. It's probably hard to reproduce since I'm using custom stuff. However, the change in this PR is just making the code more robust by moving 1 line that reads from a variable after an existing null check.While working on codegen for Shopify/Hydrogen, I found that the following fragment breaks when using
typescript-operation
in@graphql-codegen/cli
v4 and v5. It used to work in v3.3.1:It throws with
[Codegen] Cannot read properties of null (reading 'union')
, which comes from this line:In the code above,
grouped
contains the following data:The fragment for CartLine is correct, but the selection set for "nodes {...}" (ComponentizableCartLine here) has a null value which probably wasn't there in 3.3.1. Even though the type says it should be a string, truth is it can also be null because that's what is returned here.
It works when adding anything next to the fragment spread (the null value becomes a string):
I'm not sure what changed since 3.3.1, perhaps it's the type of data that comes from other packages different from
visitor-plugin-common
, but I think it's not bad to make this package more robust and filter out null values.The change in this PR is simply moving the offending line a few lines below after an existing check for null values.
Type of change
How Has This Been Tested?
Quite a manual test, but the code change is just making it more robust so it can't break anything else.
Test Environment:
@graphql-codegen/...
: ^5Checklist: