Cms: FR: GraphQL support for more advanced relatedTo parameters

Created on 2 Nov 2019  Â·  8Comments  Â·  Source: craftcms/cms

Description

For filtering interfaces, it's often necessary to filter by more than one category at a time. In a current project, I need to filter events by location as well as event type. It often makes sense that selected options in the same category are "or" filters and selected options across a second category would create an "and" filter. Example:

Event Location
[X] NYC
[ ] Chicago
[ ] Los Angeles

Event Type
[X] Craft Meetup
[X] PHP Meetup
[ ] Javascript Meetup

For this example, I'm looking for _either_ Craft Meetups _or_ PHP Meetups in New York.

In CraftQL, my $relatedTo variable would look like this:

[{"element":[3896]},{"element":[3858,3863]}]

However in Craft's current GraphQL implementation, I'm limited to integers only.

enhancement graphql

Most helpful comment

Okay. As of this commit it's possible to use a bit more fluent syntax when querying for elements based on their relations.

Assuming the original example, the way to fetch this content using an element query would be

{% set entries = craft.entries()
    .eventLocation([3896])
    .eventType([3858, 3863])
    .all() %}

And, since the ELI5 version of GraphQL is "wrapper for element queries", with native GraphQL API you can now do

{
  entries (eventLocation: [3896], eventType: [3858, 3863]) {
    title   
  }
}

Also possible to use the ['and', <ID>, <ID>] syntax in those parameters, obviously.

Hopefully, this achieves what you wanted to do @curtishenson , @daltonrooney?

All 8 comments

@andris-sevcenko Thanks so much for the GraphQL improvements coming to 3.4. Can’t wait to test them out! I was wondering if this FR has any chance of making it in. It’s the _last piece_ we’re missing to use the native GraphQL API on our next project.

I’ll definitely take a closer look at it next week!

@daltonrooney are you sure that's working as intended for you? I'm pretty sure that using both "and" and "or" is not possible, so you end up selecting all javascript meetups in new york as well in the example above? In other words, it's exactly the same as using [3896,3858,3863] as the relatedTo parameter.

Sources:

@andris-sevcenko I'm pretty sure it's working as expected in CraftQL, yeah. (This was not documented and not easy to figure out, btw!) Here's one of the sites we're working on right now.

If you choose "Data, Information, and Research" from the Issues dropdown, and "Report" from the Type dropdown, you'll get 7 results. If you add a second Type (try "Article") you'll get 10 results -- all the Reports _and_ Articles in the Data, Information, and Research category. (But not results from other categories or other types).

I'll work on a reduced test case this weekend for a better example. And I realize you can't promise feature parity with CraftQL, but we work on these kinds of interfaces a lot and CraftQL doesn't seem to be getting much support these days, so we would love to be able to rely on the built-in API. 😎

I use this feature in CraftQL as well. Here's the original issue where it was talked about and merged in, maybe that will help give it some context: https://github.com/markhuot/craftql/issues/144 The pull request is in there too.

For clarity, I do believe this is a feature needed in native GraphQL support for Craft. The debate is only how to provide it as I think there are more intuitive ways to go about this, which we're discussing internally. This is still slated for the 3.4 release.

Okay. As of this commit it's possible to use a bit more fluent syntax when querying for elements based on their relations.

Assuming the original example, the way to fetch this content using an element query would be

{% set entries = craft.entries()
    .eventLocation([3896])
    .eventType([3858, 3863])
    .all() %}

And, since the ELI5 version of GraphQL is "wrapper for element queries", with native GraphQL API you can now do

{
  entries (eventLocation: [3896], eventType: [3858, 3863]) {
    title   
  }
}

Also possible to use the ['and', <ID>, <ID>] syntax in those parameters, obviously.

Hopefully, this achieves what you wanted to do @curtishenson , @daltonrooney?

Wow - yes, I think that should work perfectly and the syntax is definitely much more clear. I’ll give it a try and let you know if I have any trouble with it. So excited for the GraphQL enhancements coming in 3.4!

On Dec 22, 2019, at 11:18 PM, Andris Sevcenko notifications@github.com wrote:


Okay. As of this commit it's possible to use a bit more fluent syntax when querying for elements based on their relations.

Assuming the original example, the way to fetch this content using an element query would be

{% set entries = craft.etries.eventLocation([3896]).eventType([3858,3863]).all()
And, since the ELI5 version of GraphQL is "wrapper for element queries", with native GraphQL API you can now do

{
entries (eventLocation: [3896], eventType: [3858,3863]) {
title
}
}
Also possible to use the ['and', , ] syntax in those parameters, obviously.

Hopefully, this achieves what you wanted to do @curtishenson , @daltonrooney?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Was this page helpful?
0 / 5 - 0 ratings