Best way to build dynamic query with graphql in sitecore jss with nextjs
I am building search query for search result page like below.
let freeTextQuery = '';
if (keyword) {
freeTextQuery = `{
name: "itemcontent_s"
value: "${keyword.toLowerCase()}"
operator: CONTAINS
}`;
}
let templateQuery = '';
props.fields.FilterTemplates.map((template) => {
templateQuery += `{
name: "itemtemplatename_s"
value: "${template.name}"
operator: EQ
}`
});
let pageTemplateQuery = '';
if (templateQuery !== '') {
pageTemplateQuery = `{
OR: [
${templateQuery}
]}`;
}
const query = gql`
{
search(
first:50
where: {
AND: [
{
name: "_path"
value: "0000-0000-0000-000-0000000000"
operator: CONTAINS
}
${freeTextQuery ? freeTextQuery : '{}'}
${pageTemplateQuery ? pageTemplateQuery : baseTemplateQuery}
]
}
) {
total
results {
name
}
}
}`;
is this the proper way or is there any other way to concatenate dynamic query ?
Read more here: Source link