New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sanity-generator

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanity-generator

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

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-86.96%
Maintainers
1
Weekly downloads
 
Created
Source

Sanity Generator

Sanity Generator is a codegen tool for Sanity to automatically generate GROQ queries from a schema perspective.

Note: Currently in beta. Be cautious when using it in production.

Minimal Configuration

To get started, define your document schemas and queries like this:

// sanity-generator.config.ts
export default createConfig({
  schemas: {
    page: pageSchema,
  },
  queries: {
    getPages: ({ schemas }) => /* groq */ `
        *[_type == "${schemas.page.name}"] {
          ${schemas.page.projection}
        }
      `,
  },
});

When you run $ npx sanity-generator, it creates queries with a spread operator to return all fields.

// sanity-generator/queries/getPages.ts
export const getPages = /* groq */ `
*[_type == "page"] {
    ...
}
`

Resolver for Custom Types (aka. the actual use case)

Define resolvers for field types from your Sanity schema. These resolvers are functions that return custom GROQ projections:

// sanity-generator.config.ts
export default createConfig({
  schemas: {
    page: pageSchema,
  },
  resolvers: {
    // Resolve all field types of "localString" with this projection...
    localeString: (name: string) => /* groq */ `
      "${name}": coalesce(${name}[$lang], ${name}.en)
    `,
  },
  queries: {
    getPages: ({ schemas }) => /* groq */ `
        *[_type == "${schemas.page.name}"] {
          ${schemas.page.projection}
        }
      `,
  },
});

If you run $ npx sanity-generator now, it expands the projection to transform all types with their associated resolver:

// sanity-generator/queries/getPages.ts
export const getPages = /* groq */  `
*[_type == "page"] {
  ...,
  "seoTitle": coalesce(seoTitle[$lang], seoTitle.en),
  sections[] {
    ...,
    gallerySection {
      ...,
      slides[] {
        ...,
        slide {
          ...,
          "title": coalesce(title[$lang], title.en)
        }
      }
    }
  }
}
`

Keywords

FAQs

Package last updated on 06 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc