-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[relay-compiler] Type-safe updaters for TypeScript #4370
[relay-compiler] Type-safe updaters for TypeScript #4370
Conversation
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.
Thanks for working on this! I've left some comments, but overall I think this should be possible to land.
website/docs/guided-tour/updating-data/imperatively-modifying-linked-fields.md
Outdated
Show resolved
Hide resolved
...gen/tests/generate_typescript/fixtures/updatable-fragment-spread-and-regular-spread.expected
Show resolved
Hide resolved
...gen/tests/generate_typescript/fixtures/updatable-fragment-spread-and-regular-spread.expected
Show resolved
Hide resolved
export type updatableFragmentSpreadAndRegularSpread_2_updatable_user$key = { | ||
readonly " $data"?: updatableFragmentSpreadAndRegularSpread_2_updatable_user$data; | ||
readonly $updatableFragmentSpreads: FragmentRefs<"updatableFragmentSpreadAndRegularSpread_2_updatable_user">; | ||
}; |
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.
In Flow this object, and updatableFragmentSpreadAndRegularSpread_2_updatable_user$data
above is inexact (...
). I don't have a good sense of why, but presumably that means we need some TypeScript equivalent? Or maybe it can be removed from the Flow types?
https://flow.org/en/docs/types/objects/#exact-and-inexact-object-types
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.
How would such an inexact type be typed in Typescript, would it just be a union of
original_obj & { [k: string]: unknown };
Might need more input from some TypeScript wizard before I add this...
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.
Maybe we can first start with understanding why its inexact in Flow? What breaks if we make it inexact? Is this specific to updatable fragments, or are all fragment keys inexact in Flow?
Maybe the semantics of TypeScript are different and we don't actually need them to be inexact in TypeScript.
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.
In other places inexact objects are used for fragments, if their MaskStatus is unmasked. I know too little about how fragments work to infer why it would be important to have the same characteristics when updating a fragment...
I tried tracing the git log for an explanation of why an InexactObject was being used, but haven't found anything. @rbalicki2 you seem to have added the code, do you perhaps remember why the InexactObject was being used?
.../relay-typegen/tests/generate_typescript/fixtures/updatable-fragment-spread-multiple.graphql
Outdated
Show resolved
Hide resolved
...egen/tests/generate_typescript/fixtures/updatable-operation-plural-field-no-spreads.expected
Show resolved
Hide resolved
...en/tests/generate_typescript/fixtures/updatable-operation-plural-field-with-spreads.expected
Outdated
Show resolved
Hide resolved
...n/tests/generate_typescript/fixtures/updatable-operation-assignable-fragment-plural.expected
Outdated
Show resolved
Hide resolved
...-typegen/tests/generate_typescript/fixtures/updatable-operation-assignable-fragment.expected
Outdated
Show resolved
Hide resolved
d09276d
to
10f87a7
Compare
@captbaritone I addressed everything that I could, please give it another look 🙏 |
@tobias-tengler Thanks for the quick turn around. Left a few more comments. Have you tried running this with a real TypeScript project? Does it generate valid types that appear to work with a minimal example of using typesafe updaters? Would love to have validation that this works end to end. |
a2b96ad
to
b07e96d
Compare
@captbaritone I've built an example here. While testing I noticed that the I've fixed this in dbcc8bb. Do you think that is fine? Meta probably uses another transform, if this hasn't led to issues for you yet, right? If we want to go forward like this, I can also submit a PR for Next.js to update their transform. Note: The example project is only runnable, since I moved the |
cd7ae1e
to
c57c0d5
Compare
aa5fafb
to
9528ab8
Compare
@captbaritone sorry for the ping, I'm sure you're very busy, but I don't want this to stall. Could you take a look at my above comments or point out another member of the Relay Team that could do so? |
Thanks for the ping and sorry for the delay. I'll look in the morning! |
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.
This is great! Can we split the addition of hashes into its own PR? After that I can import.
@captbaritone I moved the addition of the node to the following PR: #4409 |
@captbaritone @alunyov bump 🙈 |
@captbaritone has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@monicatang merged this pull request in 9c7b92a. |
Will this be released soon? This is a very valuable fix for us. |
I think the team is thinking about cutting a 15.1.0 release soon, see #4440 |
TypeScript supports specifying different getter and setter types since 5.1, so I've updated the type generation logic around
@updatable
and@assignable
to work with TypeScript.I've also updated the notice on the website and made the documentation around type-safe updaters available externally (if this is not wanted, just revert 33612d2).
Companion PR for
@types/relay-runtime
: DefinitelyTyped/DefinitelyTyped#66013 (Merged)Closes #4212 #3965