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

feat(codegen): Allow query reponse types to be overridden through SanityQueries #858

Merged
merged 3 commits into from
Jul 11, 2024

Conversation

romeovs
Copy link
Contributor

@romeovs romeovs commented Jun 19, 2024

This PR is one of two PR's that implements my feature request (sanity-io/sanity#6934) in sanity-io/sanity, with the help of some pointers from @sgulseth.

The companion PR that implements the code generation for the newly introduced SanityQueries lives at sanity-io/sanity#6997.

It sets up the @sanity/client package to have an interface SanityQueries that @sanity/codegen (or other packages) can hook into assign custom query types automatically based on the queries that are found in the codebase.

This makes it possible to write code that is fully self-typing:

import {defineQuery} from "groq"
import {createClient} from "@sanity/client"

const client = createClient({ /* ...  */ })

// Define a query as a static string
const posts = defineQuery(`*[_type = "post"] { title, tags }`)

// This will automatically be type correctly as PostsResult
const resp = await client.fetch(query)


// The following will be generated by @sanity/codegen
declare module "@sanity/client" {
  type PostsResult = {
    title?: string
    tags?: string[]
  }

  export interface SanityQueries {
    "*[_type = \"post\"] { title, tags }": PostsResult
  }
}

I have tried to set things up so that they are backwards compatible. Users can still manually
pass the types they want to the query, and the type signatures don't change. I have not touched any existing tests and type tests.

For a way to test the type changes, create a file like the above example and ensure all the types line up with what's expected.

There is also a TS Playground that summarizes the changes made by this PR and the companion PR.

src/types.ts Show resolved Hide resolved
@romeovs romeovs force-pushed the feat/define-groq-query-typemap branch from 50da0d9 to 1207aeb Compare June 20, 2024 07:31
src/types.ts Show resolved Hide resolved
@romeovs romeovs force-pushed the feat/define-groq-query-typemap branch from 1207aeb to 75630a9 Compare June 20, 2024 12:10
@romeovs romeovs marked this pull request as ready for review June 20, 2024 12:12
@romeovs romeovs changed the title Allow query reponse types to be overridden through SanityQueries feat(codegen): Allow query reponse types to be overridden through SanityQueries Jun 20, 2024
@sgulseth sgulseth requested a review from stipsan June 27, 2024 09:45
Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

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

This is awesome 💖 let's ship 🚀

@stipsan stipsan enabled auto-merge (squash) July 11, 2024 07:42
@stipsan stipsan merged commit c25d51a into sanity-io:main Jul 11, 2024
16 checks passed
This was referenced Jul 11, 2024
@romeovs romeovs deleted the feat/define-groq-query-typemap branch July 11, 2024 19:40
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants