aribouius / jsonapi-react

A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
MIT License
149 stars 28 forks source link

Filtering based on relationships #54

Closed spartanrein closed 2 years ago

spartanrein commented 2 years ago

Hi,

I'm wondering if we could use jsonapi-react supports filtering on nested relationships like below.

For example a filter param like: api/payments?include=serviceRequests.company&filter=has(serviceRequests,any(company.companyName,'superman','test))

response to above is as follows:

"data": [
        {
            "type": "payments",
            "id": "222",
            "attributes": {
                "totalAmount": 7720.00,
            },
            "relationships": {
                "serviceRequests": {
                    "links": {
                        "self": "/api/payments/222/relationships/serviceRequests",
                        "related": "/api/payments/222/serviceRequests"
                    },
                    "data": [
                        {
                            "type": "companyServiceRequests",
                            "id": "8"
                        }
                    ]
                }
            },
            "links": {
                "self": "/api/payments/222"
            }
        }
    ],
    "included": [
        {
            "type": "companyServiceRequests",
            "id": "8",
            "attributes": {
                "companyId": 21,
                "paymentId": 222,
            },
            "relationships": {
                "company": {
                    "links": {
                        "self": "/api/payments/8/relationships/company",
                        "related": "/api/payments/8/company"
                    },
                    "data": {
                        "type": "companies",
                        "id": "21"
                    }
                },
                "payment": {
                    "links": {
                        "self": "/api/payments/8/relationships/payment",
                        "related": "/api/payments/8/payment"
                    }
                }
            },
            "links": {
                "self": "/api/payments/8"
            }
        },
        {
            "type": "companies",
            "id": "21",
            "attributes": {
                "companyName": "superman",
            },
            "links": {
                "self": "/api/companies/21"
            }
        },
    ],

Many thanks in advance for your help!

aribouius commented 2 years ago

Hi @spartanrein,

Unfortunately the JSON:API spec does not define the URL structure for nested relationship filtering, so the implementation for it tends to vary across different backend frameworks.

While this library does not directly support generating the URL format you outlined in your comment above (using object notation), you should be still be able to achieve it.

useQuery(['payments', {
  include: 'serviceRequests.company',
  filter: "has(serviceRequests,any(company.companyName,'superman','test))"
}])
spartanrein commented 2 years ago

Thanks @aribouius