Hello,
My goal is to have a middleware that converts a string holding a Handlebars template into a rendered template.
Lets say I have a collection called Houses. This collection holds a name, an address, a price attribute as well as a description attribute that is a relation to another collection called description template.
Description template collection has a an attribute name and another called template. The template attribute will hold a handlebars template like so:
The address and price placeholders are referring to the Houses collection attributes.
What I’m trying to do is when I query houses, I want the description field to be set to rendered templates:
Example:
House (address: “3rd street”, price: “10000”, name: “blue house”)
Description Template(name: “foo”, template: “The house located at {{address}} costs {{price}}.”)
Query {
houses {
data {
attributes: {
description {
attributes { template }
}
}
}
}
}
Response:
houses {
data {
attributes: {
description {
data {
attributes { template: The house located on 3rd treet costs 10000. }
}
}
}
}
}
Ideally I want my middleware to look something like that:
register({ strapi }) {
const extensionService = strapi.plugin("graphql").service("extension");
extensionService.use({
resolversConfig: {
"Query.descriptionTemplate": {
middlewares: [
async (next, parent, args, context, info) => {
const { template } = descriptionTempalate; // No idea how to access this data
return {
template: Handlebars.compile(template)(parent); // parent is the house object holding the address and price attributes that will be used to fill up the template place holders
};
}
]
}
})
}
Here are my issues:
-
Query.descriptionTemplate
middleware are only triggered whendescriptionTemplate
is queried at the higher level of the Query. If it’s a nested field aka a relation of a collection, the middleware is not triggered - How can I access the template attribute of the description collection from within the middleware.
Thank you for your help