Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created April 28, 2020 05:34
Show Gist options
  • Save kmelve/cbbe3c944e3b544941e4758dcaf61dda to your computer and use it in GitHub Desktop.
Save kmelve/cbbe3c944e3b544941e4758dcaf61dda to your computer and use it in GitHub Desktop.
Pizza Taxonomies with Sanity and Gatsby
// web/gatsby-node.js
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
SanityTopping: {
pizzas: {
type: ["SanityPizza"],
resolve(source, args, context, info) {
return context.nodeModel.runQuery({
type: 'SanityPizza',
query: {
filter: {
toppings: {
elemMatch: {
id: {
eq: source.id
}
}
},
},
},
})
},
},
},
}
createResolvers(resolvers)
}
// studio/schemas/schema.js
import createSchema from 'part:@sanity/base/schema-creator'
import schemaTypes from 'all:part:@sanity/base/schema-type'
export default createSchema({
name: 'default',
types: schemaTypes.concat([
{
name: 'topping',
type: 'document',
title: 'Topping',
fields: [
{
name: 'name',
type: 'string',
title: 'Name of Topping'
},
{
name: 'image',
type: 'image'
}
]
},
{
name: 'pizza',
type: 'document',
title: 'Pizza',
title: 'Name of Pizza',
fields: [
{
name: 'name',
type: 'string'
},
{
name: 'image',
type: 'image'
},
{
name: 'toppings',
type: 'array',
of: [{
type: 'reference',
to: [{type: 'topping'}]
}]
}
]
}
])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment