System Information
- Strapi Version: 3.6.8
- Operating System: Windows
- Database: Mysql
- Node Version: 14.19.2
- NPM Version:
- Yarn Version:
For example, I have a “Product” that have a many-to-many relationship with “Tag”.
How do I query for a “Product” that’s related to both tag 1 and tag 2?
I have tried
#1:
{
products(where:{
tags: {
id: ["2", "3"]
}
}) {
id
tags{
id
value
}
}
}
#2:
{
products(where:{
_where: [
{
tags: "1"
},
{
tags: "2"
}
]
}) {
id
tags{
id
value
}
}
}
#1 give me products with tag 1 or tag 2 or both, and #2 give me no result.
GraphQL
I wish I can help u but I haven’t used the graphql version of strapi. I’ve had a look around and I think you might be able to use the in
filter <field>_in
I added the .id
because I think your tags field is an object if not remove it and try using tags_in: [1,2]
products(where: { "tags.id_in": [1,2] })
Link to docs GraphQL - Strapi Developer Documentation
Docs Example GraphQL - Strapi Developer Documentation
Rest API Solution
I wrote this at the start thinking u are using REST but didn't want to delete it someone might find it useful 😅
Hey, from what I see you are trying to filter your result to only get back products that have tags 1
or 2
and you can do that with API parameters.
Try the following
{ _where: {
tags: [1, 2]
}
}
If the tags field is an object try and you which to select the id
try the following
_where: {
_or: [
{ "tags.id": 1 },
{ "tags.id": 2 },
],
}
Links to REST API docs Content API - Strapi Developer Documentation