Query relationhips that intersect

Hi,

I have three custom types : zones, brands and products. The product type has a relationship to a zone, and to a brand.

How can I retrieve a list of all zones where at least one product is related to the zone and related to brand X ? Also, retrieve all brands for which at least one product is related to this brand and related to zone X ?

I can use graphQL or the SDK or anything else. We are using PHP

Thank you.

1 Like

Hi @ludovic!!

You could do it in different ways. Something that comes to mind is:

  1. Add Content relationships fields, not only to ‘Products’, but also to ‘Zones’ and also to ‘Brands’.
  2. Reference each other. If Product 1 references Zone 4 then Zone 4 also needs to reference Product 1.
  3. Then create a GraphQL query where you call the three Custom types and only call their Content relationships fields respectively.

Hi,

Thank you for your answer, unfortunately it is not possible to add relationships to brands because there are like 10k products and it seems that a relationship field can only link to one document. There are of course many products for one pair of zone/brand.

Could you show me an example of GraphQL query where I can intersect zones or brands with the products for a given brand / zone ?

Thank you

Hi Ludovic,

Did you ever manage to resolve this?

Are you talking talking about querying a ‘zone’ where at least one ‘product’ has that ‘zone’ as a content relationship?

So like querying by content relationship just? AllZones(where: product = 123718237) or something like that? Can you help clarify?

But to me, it seems like this is possible to:

  1. Query all the products with a link to brand X (might have to be multiple queries due to pagination of the API)
  2. Add some logic to you code to map and filter the list down to all the linked zones of that result.

And vice-verse with the brand & zone.

Hi,

Forget about the "product" because it is a bad example. Let's use "office" instead.

What we want is "List all the zones that have at least one office for brand X in the zone".

For instance, with two zones, Germany and France, and two brands, Microsoft and Apple, I would like to query the zones where there is at least one Microsoft office.

So if I have

  • Office 1, Microsoft, France
  • Office 2, Microsoft, Germany
  • Office 3, Apple, France

I want a query that takes Microsoft as an argument and would return France and Germany zone documents, because both have at least one document of prismic-type Office with a relationship to the Microsoft entity.
If the query argument is Apple, it would only return the France document.

What we do now is something like : query all offices related to the zone, map over the offices and if has a relationship to the desired brand, keep the related zone in an array (if not already in). Then we have all our zones for the brand.

But I would like to know if it is possible to to that directly in the query.

Sorry, this was a made up example, I don't remember the entities very well since I have not worked on that project for a while.

Thanks for the extra detail this really clears things up.

I think the idea stays the same you would have to use the where argument to query by Offices with a relationship linked to brand Microsoft and then filter on your front end of your project from what the content relationship zone contains.

Unfortunately though there would be no way to Have Microsoft as the query argument and return the zone.

Yup this is what we do.

Thanks for clarifying that it is not possible to have the desired results in one single query.

Cheers!

No problem, sorry it took so long to get back to you.

Thanks.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.