-
Notifications
You must be signed in to change notification settings - Fork 446
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
feat(codegen): generate SanityQueries interface in @sanity/codegen #7304
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…6997) * fix(deps): update dependency @sanity/client to ^6.21.1 (#7215) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * feat(typegen): groq/define module with a defineQuery helper * feat(typegen): Recognize queries that are wrapped in defineQuery calls * feat(typegen): Add overloadClientMethods option to @sanity/codegen * refactor(codegen): Return the typename of a generated type node so we can store it for later use * feat(typegen): Add generateTypeMap helper to TypeGenerator * feat(typegen): Allow codegen cli to generate the SanityQueries type map * docs(typegen): Add docs about defineQuery to groq README * refactor(groq): Allow defineQuery to live in the groq package * refactor(groq): Use the correct groq import in codegen tests * fix(codegen): Only use defineQuery when it comes from the groq package * refactor(codegen): Avoid changing the signature for generateTypeNodeTypes * refactor(codegen): Use typeNode to reference types in typeGenerator * feat(codegen): Handle duplicate query strings ---------
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
No changes to documentation |
Component Testing Report Updated Aug 2, 2024 2:20 PM (UTC)
|
rexxars
approved these changes
Aug 2, 2024
cngonzalez
pushed a commit
that referenced
this pull request
Aug 20, 2024
…6997) (#7304) * fix(deps): update dependency @sanity/client to ^6.21.1 (#7215) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * feat(typegen): groq/define module with a defineQuery helper * feat(typegen): Recognize queries that are wrapped in defineQuery calls * feat(typegen): Add overloadClientMethods option to @sanity/codegen * refactor(codegen): Return the typename of a generated type node so we can store it for later use * feat(typegen): Add generateTypeMap helper to TypeGenerator * feat(typegen): Allow codegen cli to generate the SanityQueries type map * docs(typegen): Add docs about defineQuery to groq README * refactor(groq): Allow defineQuery to live in the groq package * refactor(groq): Use the correct groq import in codegen tests * fix(codegen): Only use defineQuery when it comes from the groq package * refactor(codegen): Avoid changing the signature for generateTypeNodeTypes * refactor(codegen): Use typeNode to reference types in typeGenerator * feat(codegen): Handle duplicate query strings --------- Co-authored-by: Romeo Van Snick <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR is just forwarding #6997 by @romeovs (with help from @sgulseth ), so that we can run all the CI testing flows that otherwise are skipped/fail on PRs from forked branches.
Check #6997 for all the deets, the gist of it is that it allows typegen that are much more ✨ magical ✨ and easier to use.
Today typegen requires you to instrument your
client.fetch
calls with the generated query response:This burden compounds as the
client.fetch
itself might be abstracted away in higher level functions. For apps like onnext
it's common to define asanityFetch
function and must remember to forward theQueryResponse
generic.Now, given these requirements:
@sanity/client
is on a recent version (v6.21.0
or later) that has the newSanityQueries
interface.defineQuery
utility fromgroq
instead ofgroq
@sanity/codegen
is configured to generate theSanityQueries
interface, it's opt-in by setting"overloadClientMethods": true
insanity-typegen.json
Then it's far less work to use the generated typings, and the DevEx is that it "just works"✨✨
What to review
This feature brings a lot of value, and I struggle to find a use case where you want it disabled. Should we make
overloadClientMethods
true by default and rather opt-out if necessary?The only cost to using this is potentially that typegen takes longer than needed if the feature isn't used for whatever reason, AFAIK.
If someone were to update
sanity
on a project that already use generics withclient.fetch
then the generic they pass takes precedence, so it shouldn't cause conflicts with codebases that already use typegen.Testing
I've made a canary release that can be used to e2e test in userland:
npm i --save-exact [email protected]
.To ensure that we don't regress any code that today rely on being able to import groq in ESM like:
and in CJS:
We added a testing suite that runtime checks these in node: https://github.com/sanity-io/sanity/tree/3b2329cf640954f308676a4b02f3fda701c65587/packages/groq/test
These tests makes us feel comfortable that we can introduce the new
defineQuery
utility as a named export in ESM:and CJS:
You can even mix them:
Notes for release
It's now possible to use TypeGen without manually passing around generics into
client.fetch
calls 🥳Update
@sanity/client
so you're onv6.21.0
or later. Add"overloadClientMethods": true
to yoursanity-typegen.json
, and wrap your query strings in the newdefineQuery
utility fromgroq
: