Created
May 18, 2020 20:33
-
-
Save LuigiClaudio/705a4fb0a428154c4d9069f76b1eabcc to your computer and use it in GitHub Desktop.
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
exports.createSchemaCustomization = ({ actions, schema }) => { | |
actions.createTypes([ | |
` | |
interface StoreProducts @nodeInterface { | |
id: ID! | |
productTitle: String | |
productId: String | |
printfulProduct: PrintfulProduct | |
currency: String | |
} | |
`, | |
schema.buildObjectType({ | |
name: 'StoreProductsMarkdown', | |
interfaces: ['Node', 'StoreProducts'], | |
extensions: { | |
childOf: { | |
type: 'MarkdownRemark', | |
}, | |
infer: true, | |
}, | |
fields: { | |
id: 'ID!', | |
productTitle: { | |
type: 'String', | |
resolve: (source, _args, context) => { | |
const parent = context.nodeModel.getNodeById({ id: source.parent }); | |
return parent.frontmatter.productTitle; | |
}, | |
}, | |
productId: { | |
type: 'String', | |
resolve: (source, _args, context) => { | |
const parent = context.nodeModel.getNodeById({ id: source.parent }); | |
return parent.frontmatter.productId; | |
}, | |
}, | |
printfulProduct: { | |
type: 'PrintfulProduct', | |
resolve: (source, _args, context) => { | |
const parent = context.nodeModel.getNodeById({ id: source.parent }); | |
return context.nodeModel | |
.getAllNodes({ type: 'PrintfulProduct' }) | |
.find((p) => p.id === parent.frontmatter.productId); | |
}, | |
}, | |
currency: { | |
type: 'String', | |
resolve: (_source, _args, context) => { | |
// Currency will always be the same across variants so we can reduce it and move it away from variants and up to product | |
const currencies = context.nodeModel | |
.getAllNodes({ type: 'PrintfulVariant' }) | |
.map((variant) => variant.currency); | |
const currency = [...new Set(currencies)].toString(); | |
return currency; | |
}, | |
}, | |
}, | |
}), | |
]); | |
}; | |
exports.onCreateNode = ({ node, actions, createNodeId }) => { | |
if (node.internal.type !== 'MarkdownRemark') { | |
return; | |
} | |
actions.createNode({ | |
id: createNodeId(`StoreProductsMarkdown-${node.id}`), | |
parent: node.id, | |
internal: { | |
type: 'StoreProductsMarkdown', | |
contentDigest: node.internal.contentDigest, | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment