Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A typed, zero-dependency schema generator and query builder for Sanity.

License

NotificationsYou must be signed in to change notification settings

danielroe/sanity-typed-queries

Repository files navigation

A typed query generator for Sanity

A zero-dependency schema generator and query builder that is fully-typed and works in JavaScript and TypeScript.

Features

  • 📚Documentation: Sanity documentation appears as you type.
  • 💪TypeScript: Written in TypeScript.

Progress

  • Fully typed schema builder
  • Query builder (working with string, boolean, number), ordering, projections
  • Inferred type of arrays
  • Support for object types with subfields
  • Custom mappings ("prop": my.prop)
  • Resolving image and file types
  • Resolving custom object/document types
  • Distinguish object/document types within valid field types
  • Additional query filters
  • Querying multiple types of document

Help and contributions are welcome.

Quick Start

First installsanity-typed-queries:

yarn add sanity-typed-queries# or npmnpm install sanity-typed-queries --save

Schema definition

Now you will need to generate your Sanity schema documents using the schema builder. You will get documentation as you type, and enforced compliance with Sanity's schema builder, such as being able to see validation rules applicable to the type of field you are creating, and so on.

schema/author.js:

import{defineDocument}from'sanity-typed-queries'const{ document}=defineDocument('author',{name:{type:'string',validation:Rule=>Rule.required(),},biography:{type:'text',rows:4,},yearOfBirth:{type:'number',},})exportdefaultdocument

This is equivalent to defining the following schema:

exportdefault{name:'author',title:'Author',type:'document',fields:[{name:'name',title:'Name',type:'string',validation:Rule=>Rule.required(),},{name:'biography',title:'Biography',type:'text',rows:4,},{name:'yearOfBirth',title:'Year Of Birth',type:'number',},],}

Query builder

For more documentation, seethis GROQ/query builder cheat sheet.

You can also export a query builder from the same file.

import{defineDocument}from'sanity-typed-queries'const{ document, builder}=defineDocument('author',{// ...})// Export your query builder for use elsewhereexport{builder}exportdefaultdocument

You can use this builder elsewhere to generate the appropriate types and GROQ queries. For example:

import{builderasauthorBuilder}from'./cms/schema/author.js'const[query,type]=authorBuilder.pick('name').first().use()// *[_type == 'author'][0].nameconstqueryString=query// stringtypeAuthorName=typeoftype

If you're using the Sanity client, you might use it like this:

importsanityClientfrom'@sanity/client'import{author}from'./cms/schema/author.js'const[query,type]=author.pick('name').first().use()constclient=sanityClient(config)// Promise<string>constresult=client.fetch<typeoftype>(query)

Custom types

You can export utility objects or documents for reference within other schemas.

schema/tag.js:

import{defineObject}from'sanity-typed-queries'const{ tag, object}=defineObject('tag',{// ...})export{tag}exportdefaultobject

Then you can pass that when defining documents that reference it.

schema/author.js:

import{defineObject}from'sanity-typed-queries'import{tag}from'./tag'const{ builder, document}=defineDocument('author',{tag:{type:'tag',},},[tag])exportdefaultdocument

Inspirations

Projects I've found helpful are:

Contributors

This has been developed to suit my needs but additional use cases and contributions are very welcome.

License

MIT License - Copyright © Daniel Roe

About

A typed, zero-dependency schema generator and query builder for Sanity.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp