With great power, comes great configurability
Parser config for array
type.
Prop | Type | Default |
---|---|---|
fields | object | {} |
conditions | object | |
filter | function | () => false |
{
array: {
fields: {},
conditions: undefined,
filter: () => false,
}
}
Parser config for blockContent
type.
Prop | Type | Default |
---|---|---|
name | string | "content" |
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
types | function | (types: array) => types |
{
blockContent: {
name: 'content',
fields: defaultFields,
conditions: undefined,
filter: () => false,
types: (types) => types,
}
}
import { link } from './link';
const defaultFields = {
'...': true,
'markDefs[]': {
'...': true,
'_type == "link" =>': link,
},
};
A sub parser of reference
.
The below props are passed down from the reference
primative/config ⬇.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object |
// Uses the reference primative for `defaultFields`
const defaultFields = '../primative/reference';
Parser config for cards
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object |
{
cards: {
fields: defaultFields,
conditions: undefined,
}
}
const defaultFields = {
'...': true,
};
Parser config for components
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
types | function | (types: array) => types |
{
reference: {
fields: defaultFields,
conditions: undefined,
types: (types) => types,
}
}
const defaultFields = {};
Parser config for image
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object |
{
image: {
fields: defaultFields,
conditions: undefined,
}
}
const defaultFields = {
hotspot: true,
crop: true,
altText: true,
'asset->': {
_id: true,
mimeType: true,
originalFilename: true,
url: true,
metadata: {
lqip: true,
dimensions: {
aspectRatio: true,
width: true,
height: true,
},
},
},
};
Parser config for jsonType
type.
Probably needs a bunch of stuff added here. Probably best to have @Devin, the man, explain this the way he does best.
{
jsonType: {
filter: () => false,
}
}
Prop | Type | Default |
---|---|---|
filter | function | () => false |
Parser config for link
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
{
link: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
const defaultFields = {
'...': true,
url: `
select(
condition == 'internal' && page->_type == 'pageHome' => "/",
condition == 'internal' => page->path,
condition == 'external' => url,
condition == 'download' => file.asset->url
)
`,
label: `
select(
defined(label) => label,
condition == 'internal' => page->title,
condition == 'external' => url,
condition == 'download' => file.asset->originalFilename
)
`,
target: `
select(
condition == 'external' => target,
false
)`,
};
Parser config for media
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
{
media: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
const defaultFields = {
'...': true,
image,
};
Parser config for reference
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
{
reference: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
import mapConditions from '../utils/mapConditions';
import { image } from './image';
import { link } from './link';
const defaultFields = {
_type: true,
_key: '_id',
title: true,
slug: true,
url: 'path',
...mapConditions({
_type: {
post: {
publishedDate: true,
image,
},
form: {
'...': true,
},
navigation: {
'...': true,
'menu[]': link,
},
},
}),
};
Parser config for seo
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
{
seo: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
const defaultFields = {
seoTitle: 'coalesce(seo.seoTitle, @.title)',
seoImage: `seo.seoImage{..., asset->}`,
seoDescription: 'seo.seoDescription',
hasCustomSeoTitle: 'defined(seo.seoTitle)',
};
Parser config for slug
type.
Prop | Type | Default |
---|---|---|
fields | object | defaultFields |
conditions | object | |
filter | function | () => false |
{
slug: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
const defaultFields = { slug: 'slug.current' };
Custom parsers can be defined in your packages/config/parser.js
config file.
// packages/config/parser.js
const { image, link } = require('../query/dist/parser/primitives');
module.exports = {
custom: {
backgroundImage: (name) => ({
[name]: {
'...': true,
asset: '->',
},
}),
},
components: {
types: (types) =>
types.filter(({ name }) => !['heading', 'spacer', 'pageAnchor', 'tableBlock'].includes(name)),
},
blockContent: {
types: (types) => types.filter(({ name }) => !['pageAnchor', 'table'].includes(name)),
},
reference: {
conditions: {
_type: {
// ...
testimonial: {
'...': true,
logo: image,
image,
link,
},
team: {
title: true,
jobTitle: true,
image,
},
},
},
},
};
Custom parsers must be defined as a function returning a objectified GROQ query. The function is passed 2 parameters:
name
- the name
of the schema field (ie. your name:
definition in the schema)
type
– the full registry definition of the schema field (ie. the full schema definition)
Typically, it's advisable to set the key of the returning object to the name of the field itself.
Example
// Example schema
{
title: 'Movie',
name: 'movie',
type: 'document',
fields: [
{
title: 'Title',
name: 'title',
type: 'string'
},
{
title: 'Poster',
name: 'poster',
type: 'image'
parseType: 'customImageParser' // <--- maps to custom parser
},
{
title: 'Directors',
name: 'directors',
type: 'array',
of: [{type: 'string'}]
}
]
}
// Custom parsers
// name = 'poster'
// type = 'image'
module.exports = {
custom: {'
customImageParser: (name, type) => ({
[name]: {
'...': true,
asset: '->',
},
});
}
}
// GROQ output
poster: {
...,
asset->
}