GitHub
Twitter

Config

With great power, comes great configurability

Parser config for array type.

PropTypeDefault
fieldsobject{}
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
array: {
fields: {},
conditions: undefined,
filter: () => false,
}
}

Parser config for blockContent type.

PropTypeDefault
namestring"content"
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
typesfunction(types: array) => types
Parser Config
{
blockContent: {
name: 'content',
fields: defaultFields,
conditions: undefined,
filter: () => false,
types: (types) => types,
}
}
Primative
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 ⬇.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
Primative
// Uses the reference primative for `defaultFields`
const defaultFields = '../primative/reference';

Parser config for cards type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
Parser Config
{
cards: {
fields: defaultFields,
conditions: undefined,
}
}
Primative
const defaultFields = {
'...': true,
};

Parser config for components type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
typesfunction(types: array) => types
Parser Config
{
reference: {
fields: defaultFields,
conditions: undefined,
types: (types) => types,
}
}
Primative
const defaultFields = {};

Parser config for image type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
Parser Config
{
image: {
fields: defaultFields,
conditions: undefined,
}
}
Primative
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.

Parser Config
{
jsonType: {
filter: () => false,
}
}
PropTypeDefault
filterfunction() => false

Parser config for link type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
link: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
Primative
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.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
media: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
Primative
const defaultFields = {
'...': true,
image,
};

Parser config for reference type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
reference: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
Primative
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.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
seo: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
Primative
const defaultFields = {
seoTitle: 'coalesce(seo.seoTitle, @.title)',
seoImage: `seo.seoImage{..., asset->}`,
seoDescription: 'seo.seoDescription',
hasCustomSeoTitle: 'defined(seo.seoTitle)',
};

Parser config for slug type.

PropTypeDefault
fieldsobjectdefaultFields
conditionsobjectNo default value
filterfunction() => false
Parser Config
{
slug: {
fields: defaultFields,
conditions: undefined,
filter: () => false,
}
}
Primative
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:

  1. name - the name of the schema field (ie. your name: definition in the schema)

  2. 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->
}
💡
See something that is out of date or that could be improved?Please let the team know!1. You can create a Github issue2. Pull down the repo and create a PR with your suggested changes implimented.3. Or just let someone know in the R&P Slack Channel.We love making things better.