I have a document type article and one of the slices is a slice that contains a content relationship link to html_banner document type.
I’m trying to query all of the articles that contain this slice with a link to a certain html_banner document by it’s ID, so I can replace it with a different document ID.
But it’s not working and giving me an error unexpected field 'my.article.html_banner'
I think it’s because this isn’t a root level content-relationship link, and instead it’s within a slice. But I’m not sure.
Does anyone know how I can accomplish this?
Edit:
I asked chatgpt, and it gave me a query where I specifiy the slice type html_banner and then the relationship field which is also html_banner. I’m still getting the same error.
Indeed, it’s likely because it’s within a slice. You could fetch all the articles and then look for the banner kind of like this:
import { createClient, isFilled } from "@prismicio/client";
const client = createClient();
async function findArticlesWithBanner(targetId: string) {
const articles = await client.getAllByType("article", {
// Only fetch what you need for speed
fetch: ["article.body"],
pageSize: 100, // tune as needed
});
return articles.filter((doc) =>
(doc.data.body ?? []).some((slice: any) => {
if (slice.slice_type !== "html_banner") return false;
const inPrimary =
isFilled.contentRelationship(slice.primary?.html_banner) &&
slice.primary.html_banner.id === targetId;
const inItems = Array.isArray(slice.items) &&
slice.items.some(
(it: any) =>
isFilled.contentRelationship(it.html_banner) &&
it.html_banner.id === targetId
);
return inPrimary || inItems;
})
);
}
You’ll need to adapt this and might need to tweak it depending on your set-up, but it might be a more straightforward way to get the fields you need. Not sure what you mean by “so I can replace it with a different document ID” though, so if my answer doesn’t work for that, feel free to give me more context