Created
April 28, 2020 05:34
-
-
Save kmelve/cbbe3c944e3b544941e4758dcaf61dda to your computer and use it in GitHub Desktop.
Pizza Taxonomies with Sanity and Gatsby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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