Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LuigiClaudio/705a4fb0a428154c4d9069f76b1eabcc to your computer and use it in GitHub Desktop.
Save LuigiClaudio/705a4fb0a428154c4d9069f76b1eabcc to your computer and use it in GitHub Desktop.
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