Closed scottrippey closed 10 months ago
A PoC was created in this branch: sanity-typed This implemented several of the API methods above, making them strongly typed according to the Sanity Schema. Here are my conclusions about this approach.
I was able to leave the GroqD API intact, with only 2 notable changes
q
(with no type info), you'd create a "typed q" via export const q = getTypedQ<SanitySchema>()
. This simply returned the global q
object, but with strong schema type info.grab(object)
API, where it is now grab(q => object)
. The q
here is again the global q
object, but bound with better type info.For the most part, strongly-typed schema gives a GREAT developer experience!
filterByType('type')
, grab({ ... })
, and q('field')
methods, which are the most used API methods in this app..filter(string)
or .sort(string)
or conditional grab, which are hard to strongly-type.
@sanity-typed/groq
does this for entire queries)string
as a fallback.This might be a prohibiting factor.
InferSchemaValues
from @sanity-typed/types
is painfully slow.
tsconfig.tsbuildinfo
caching is used, rebuilds are fastThe shape of the inferred schema is pretty basic:
InferSchemaValues
. All the performance problems were alleviated.groqd
queries. Developers might be inclined to manually-type schemas, if it gives auto-completion and type-checks to their queries.InferSchemaValues
output into a flattened structure. I tried compiling the file using tsc --declaration
but it just outputs the same InferSchemaValues<typeof config>
code. I could not find a way to do this anywhere. Some good discussions were found here: https://github.com/microsoft/TypeScript/issues/34556const typecheckSchema: SanitySchemaInferred = {} as unknown as SanitySchemaCompiled;
.After working through this PoC, Grant and I had a discussion that led to an interesting conclusion.
So, this conclusion is leading us to a follow-up PoC -- making Zod optional/swappable, and relying on Schema types and static type checks instead of runtime checks! See #202.
Update: this has been implemented as the groq-builder
package in groqd
GroqD currently supports strongly-typed output types.
However, it is completely unaware of the input types as defined in the Sanity Schema.
Here are some tasks for creating a POC for strongly-typed input types:
@sanity-typed/types
q
that's bound to this schemaAPI methods to make strongly-typed:
q
filter
filterByType
grab
grab$
grabOne
grabOne$
select
slice
order
deref
score
nullable