sanity-generator
Advanced tools
Comparing version 0.0.2-4 to 0.0.2-6
{ | ||
"name": "sanity-generator", | ||
"version": "0.0.2-4", | ||
"version": "0.0.2-6", | ||
"main": "./build/index.js", | ||
"typings": "./build/types/index.d.ts", | ||
"files": [ | ||
"build/" | ||
], | ||
"bin": { | ||
@@ -7,0 +10,0 @@ "sanity-generator": "build/cli/main.js", |
@@ -1,43 +0,25 @@ | ||
# Sanity Generator | ||
Sanity Generator is a codegen tool for [Sanity](https://www.sanity.io) to automatically generate GROQ queries from a schema perspective. | ||
Sanity Generator aims to simplify the process of defining schemas and queries when working with [Sanity](https://www.sanity.io/) CMS. | ||
**Note: Currently in beta. Be cautious when using it in production.** | ||
It is based on the assumption that a given document schema shape, is not that different from it's corresponding query shape. Moreover it assumes that if you need to reshape a specific field type, you probably would like to do this on all occurences of that type through out all queries. | ||
## Minimal Configuration | ||
Certainly, this can be done with simple exporting, importing and composing of template literals. But this is quite repetitivie, and error prone. Sanity Generator is a CLI tool that aims to automate this while still providing all flexiblities of GROQ. | ||
To get started, define your document schemas and queries like this: | ||
```TypeScript | ||
// sanity-generator.config.ts | ||
export default createConfig({ | ||
schemas: { | ||
page: pageSchema, | ||
}, | ||
queries: { | ||
getPages: ({ schemas }) => /* groq */ ` | ||
*[_type == "${schemas.page.name}"] { | ||
${schemas.page.projection} | ||
} | ||
`, | ||
}, | ||
}); | ||
``` | ||
**Disclaimer: This is still Beta. Use with caution** | ||
When you run `$ npx sanity-generator`, it creates queries with a spread operator to return all fields. | ||
## Basic Usage | ||
```TypeScript | ||
// sanity-generator/queries/getPages.ts | ||
export const getPages = /* groq */ ` | ||
*[_type == "page"] { | ||
... | ||
} | ||
` | ||
``` | ||
Install from npm: | ||
## Resolver for Custom Types (aka. the actual use case) | ||
`$ npm install sanity-generator --save-dev` | ||
Define resolvers for field types from your Sanity schema. These resolvers are functions that return custom GROQ projections: | ||
`$ yarn add sanity-generator --dev` | ||
Create a config file: | ||
`$ npx sanity-generator init` | ||
Customize the config: | ||
```TypeScript | ||
@@ -47,6 +29,9 @@ // sanity-generator.config.ts | ||
schemas: { | ||
// All sanity document schemas you want to query go here. | ||
// The object key is for later referencing. | ||
page: pageSchema, | ||
}, | ||
resolvers: { | ||
// Resolve all field types of "localString" with this projection... | ||
// 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 */ ` | ||
@@ -57,2 +42,3 @@ "${name}": coalesce(${name}[$lang], ${name}.en) | ||
queries: { | ||
// Every property in here will be exported as a GROQ query. The function recieves a single argument that is an object with all processed schemas. Here you can get the projection with all resolvers applied, and for convinece also the schema name. | ||
getPages: ({ schemas }) => /* groq */ ` | ||
@@ -67,6 +53,19 @@ *[_type == "${schemas.page.name}"] { | ||
If you run `$ npx sanity-generator` now, it expands the projection to transform all types with their associated resolver: | ||
## How it works | ||
Sanity Generator simple 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 custom resolver. (Both with option `inlineResolver: true`) | ||
```TypeScript | ||
// sanity-generator/queries/getPages.ts | ||
// Generated query with no resolver | ||
export const getPages = /* groq */ ` | ||
*[_type == "page"] { | ||
... | ||
} | ||
` | ||
``` | ||
```TypeScript | ||
// Same query, with resolver | ||
export const getPages = /* groq */ ` | ||
@@ -92,1 +91,29 @@ *[_type == "page"] { | ||
``` | ||
## 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 typesupport 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. | |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
116
6507
2
0
2