npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sanity-generator

v0.2.5

Published

Sanity Generator is a codegen tool for [Sanity](https://www.sanity.io) to automatically generate GROQ queries from a schema perspective.

Downloads

78

Readme

Test and Publish npm

Sanity Generator

Sanity Generator aims to simplify the process of defining schemas and queries when working with Sanity CMS.

It is built on the premise, that for most use cases, the shape of your query can be very similar to the shape of your document schema. Moreover, it assumes that if you need to reshape a specific field type, you probably would like to do this on all occurrences of that type throughout all queries.

Certainly, this can be done with simple exporting, importing, and composing of template literals. But this is quite repetitive and error-prone. Sanity Generator is a CLI tool that aims to automate this while still providing all the flexiblities of GROQ.

Disclaimer: This is still Beta. Use with caution

Basic Usage

Install from npm:

npm install --save-dev sanity-generator 

or

yarn add --dev sanity-generator 

Create a config file:

npx sanity-generator init

Customize the config:

// sanity-generator.config.ts
export default createConfig({
  schemas: {
    // All sanity document schemas you want to query go here.
    // The object key is for later referencing.
    page: pageSchema,
  },
  resolvers: {
    // All field types you want to apply a custom GROQ query go here.
    // The object key must match a field type.
    localeString: (name: string) => /* groq */ `
      "${name}": coalesce(${name}[$lang], ${name}.en)
    `,
  },
  queries: {
    // Every property in here will be exported as a GROQ query. 
    // The function receives a single argument that is an object with 
    // all processed schemas. Here you can get the projection with 
    // all resolvers applied, and for convenience also the schema name.
    getPages: ({ schemas }) => /* groq */ `
        *[_type == "${schemas.page.name}"] {
          ${schemas.page.projection}
        }
      `,
  },
});

Write the queries and resolver to disk:

npx sanity-generator generate

Filehandling and Limitations

Sanity Generator uses ts-node and if present the tsconfig.json from the present working directory to transpile all of you configs TypeScript dependencies. However, this might not be enough if your schemas depend on files that need a special loader like CSS files. For these scenarios you might need to transpile the schemas before using them with the generator.

Programatic Use

For szenarios as described before or more custom implementation, you can import the core functions into your node.js app.

import { createConfig, generate } from "sanity-generator";

const config = createConfig({
  // ...config
})

generate(...config)

How it works

Sanity Generator simply traverses all branches of the document schema. If a branch holds no types that have a corresponding resolver, it uses the spread operator (...). If a branch holds a type that should be resolved differently, it writes the corresponding projections just as far as needed.

Here is an example with the same query, without and with a custom resolver. (Both with option inlineResolver: true)

// Generated query with no resolver
export const getPages = /* groq */ `
*[_type == "page"] {
    ...
}
`
// Same query, with resolver
export const getPages = /* groq */  `
*[_type == "page"] {
  ...,
  "seoTitle": coalesce(seoTitle[$lang], seoTitle.en),
  sections[] {
    ...,
    slides[] {
      ...,
      "title": coalesce(title[$lang], title.en)
    }
  }
}
`

Options

CLI

npx sanity-generator

or

npx sg

| Command | Option | Description | | ---------- | ------------------ | ---------------------------------------------------------------------------------- | | generate | | Run the generator assuming the config is here: ~/sanity-generator.config.ts | | generate | --config -c | Specify a path to the config file | | init | | Create a default config |

Config

The module exports a createConfig function to provide better type support for the configuration object.

| Property | Default | Description | | --------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | | schemas | {} | See basic usage | | resolvers | {} | See basic usage | | queries | {} | See basic usage | | outPath | './sanity-generator` | Path to the destination folder for the queries | | inlineResolvers | false | By default, resolvers are imported as a function into the final query. Setting this to false, will inline the resolvers as a string into the query. |

ToDos

  • Example for programatic use in monorepo (PRs welcome)

Roadmap

  • Auto type generation for the queries.
  • Local resolvers for inline objects (objects that are not imported directly to the schema)