-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Make keyFields a readonly array #9339
Conversation
@julienfouilhe: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/ |
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.
Seems reasonable, especially since mutating these arrays is unlikely to produce desirable results (whatever the goal might be). Thanks @julienfouilhe!
@benjamn Typescript seems to be having trouble discriminating KeySpecifier in some cases now though, I'll come up with something to fix this! |
@benjamn I added a isReadonlyArray helper, it was the easiest/most simple solution I could find. Tell me if you think of something smarter! |
src/cache/inmemory/helpers.ts
Outdated
|
||
export const isReadonlyArray = (a: any): a is ReadonlyArray<any> => Array.isArray(a) |
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.
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 rename the helper to isArray?
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.
export const isArray = (a: any): a is ReadonlyArray<any> | Array<any> => Array.isArray(a)
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.
@sztadii you're right, done!
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.
@julienfouilhe I am only worried that someday TS will change the behavior of read-only arrays.
Sometimes maybe we will prefer to use a mutable array.
Can we change isArray to something like that
export const isArray = (a: any): a is any[] | readonly any[] => Array.isArray(a)
?
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.
@sztadii done! 👍
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.
LGTM, thanks again @julienfouilhe!
These changes should now be available from npm in |
Just a small typing change.
The current behaviour prevents doing things like this: