Summary
I’m struggling to discover how to build a query in graphql. The goal of the query is to query the products that are referenced by an offer that was updated after some date. The issue is that by modeling the content in a way that works well for editing, it seems to have complicating the filtering available to us.
I’m syncing 3 types of data in fibery to another system that I’ll psuedo-model below.
Schema
Type 1: Category (plan to sync all of these)
- name: string
- parent_id: string
Type 2: Product (only want to sync the products linked from the offers we sync)
- name: string
- category: Category (relation to the category type)
Type 3: Offer (only want to sync the last N days of offers)
- title: string
- call_out: string
- offer_type: Category | Product (single link relationship to either of these two types)
Schema Screenshot
Schema Goal
The goal with this offer model is so that we can support writing an offer about a category when there isn’t an associated product, but if there is an associated product we can use its category. We require a category be linked to the offer in one way or the other. This model works well from an editing perspective, but has caused an issue with filtering.
We have lots of offers and typically only care about the more recent ones for most use cases. So, we are syncing the last N days of offers by update date to another system. The issue is that I only want to sync the products that has an offer that links to it.
Issue
This query below gets me close, but it only filters down to offers that link to either a Product or Category record. While I could use this to filter to the product records returned after the fact, it requires a lot more paging and it seems like a gap in usability of these link fields that support different record types. I also wonder if I’m just missing another way of doing what I want.
I feels like I’d really want to go about it from the opposite direction of filtering to product records that are linked to from offer records updated after some date that are linked to from the offer.offerType
field. I couldn’t get close on this type of query.
Note: I cannot do this as a multi-step process due to the integration framework I’m required to leverage. So, I need to execute two queries independent of each other and return it in a single session.
Query Example
query MyQuery{
allOffers(filter: {offerType: {exists: "true"}, _updatedAt: {gt: "2023-01-01"}}) {
id
offerTitle
callOutText
offerType {
... on ProductRecord {
id
name
categories {
id
name
}
_modelApiKey
}
... on CategoryRecord {
id
name
_modelApiKey
}
}
}
}
Example Data Returned
Here is a sample of some records returned. You’ll see offers linked to a product and some linked to a category
{
"id": "151881809",
"offerTitle": "LEGO DOTS Stitch-on Patch",
"callOutText": "$3",
"offerType": {
"id": "151882274",
"name": "LEGO DOTS Stitch-on Patch",
"categories": [
{
"id": "146444288",
"name": "Toys & Hobbies"
}
],
"_modelApiKey": "product"
}
},
{
"id": "151881838",
"offerTitle": "Lee Men's Extreme Motion Crossroad Cargo Shorts",
"callOutText": "$18",
"offerType": {
"id": "146444353",
"name": "Shorts",
"_modelApiKey": "category"
}
},
{
"id": "151881731",
"offerTitle": "Nike Air Max Men's 97 OG Shoes",
"callOutText": "$74",
"offerType": {
"id": "146444352",
"name": "Shoes",
"_modelApiKey": "category"
}
},