Skip to content

Instantly share code, notes, and snippets.

@LuigiClaudio
Last active June 20, 2020 13:47
Show Gist options
  • Save LuigiClaudio/8550a10080efa8e1b8f934d5ca64565c to your computer and use it in GitHub Desktop.
Save LuigiClaudio/8550a10080efa8e1b8f934d5ca64565c to your computer and use it in GitHub Desktop.
exports.createSchemaCustomization = ({ actions, schema }) => {
actions.createTypes([
`
type StoreProductsJson implements Node {
product: [StoreProductsJsonProduct]
}
type StoreProductsJsonProduct {
title: String
isActive: Boolean
productId: String
subtitle: String
discount: Float
shortDescription: String
description: String
group: String
category: String
categoryList: [StoreProductsJsonProductCategoryList]
}
type StoreProductsJsonProductCategoryList {
category: String
}
interface StoreProductsInterface @nodeInterface {
id: ID!
productId: String
printful: PrintfulProduct
cloudinary: [CloudinaryMedia]
cms: StoreProductsJsonProduct,
currency: String,
}
`,
schema.buildObjectType({
name: 'StoreProducts',
interfaces: ['Node', 'StoreProductsInterface'],
extensions: {
childOf: {
type: 'PrintfulProduct',
},
infer: true,
},
fields: {
id: 'ID!',
productId: {
type: 'String',
resolve: (source, _args, context) => {
const parent = context.nodeModel.getNodeById({ id: source.parent });
return parent.id;
},
},
cms: {
type: 'StoreProductsJsonProduct',
resolve: (source, _args, context) => {
const parent = context.nodeModel.getNodeById({ id: source.parent });
const cmsJson = context.nodeModel.getAllNodes({
type: 'StoreProductsJson',
});
const cmsProduct = cmsJson.find((i) => i.product);
const productsInCms = Object.values(cmsProduct.product).map((products) => {
return products;
});
const product = productsInCms.find((i) => {
return i.productId === parent.id;
});
return product;
},
},
printful: {
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.id);
},
},
cloudinary: {
type: ['CloudinaryMedia'],
resolve: (source, _args, context) => {
const parent = context.nodeModel.getNodeById({ id: source.parent });
const allAssets = context.nodeModel.getAllNodes({
type: 'CloudinaryMedia',
});
const filtered = allAssets.filter((i) => {
return i.tags.includes(parent.id);
});
if (filtered.length) {
const filteredWithContext = filtered.filter(
(withContext) => withContext.context,
);
filteredWithContext.sort((a, b) => {
if (a.context.custom.index && b.context.custom.index) {
return a.context.custom.index.localeCompare(
b.context.custom.index,
);
}
return a - b;
});
return filteredWithContext;
}
return filtered;
},
},
currency: {
type: 'String',
resolve: (_source, _args, context) => {
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 !== 'PrintfulProduct') {
return;
}
actions.createNode({
id: createNodeId(`StoreProducts-${node.id}`),
parent: node.id,
internal: {
type: 'StoreProducts',
contentDigest: node.internal.contentDigest,
},
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment