Add ability to filter on related nodes. For example, getting only User
s which have associated Track
s:
{
viewer {
allUsers(filter: ???) {
edges {
node {
slug
tracks {
edges {
node {
title
}
}
}
}
}
}
}
}
Without filter, this could return:
{
"data": {
"viewer": {
"allUsers": {
"edges": [
{
"node": {
"slug": "user-without-tracks",
"tracks": {
"edges": []
}
}
},
{
"node": {
"slug": "user-with-tracks",
"tracks": {
"edges": [
{
"node": {
"title": "Some Awesome Track"
}
}
]
}
}
}
]
}
}
}
}
user-without-tracks
shouldn't be returned.
Maybe this relates to https://github.com/graphcool/feature-requests/issues/2.
Very good point. This is already WIP. 🎉
I needed this as well to show posts related to tags, so i created this (rather ugly but working) temporary solution in angular 2.
https://gist.github.com/emolr/f2eaf46612ca7bbb47b6a814180da719
It does the job for now, but it is pretty slow compared to a single request, but it works, so I'll spend the extra time on a good loading animation. It might help someone needing the same functionality until it's implemented
My temporary approach have some limitations though.
When i began implementing search i found that i can't both have my search filter: {id_in: ['1', '2']}
while using my tag posts lookup ids as they work on the same filter argument.
So dream scenario would be something like filter: {AND: [id_in: ['1','2'], tagId_in: ['73', '23']]}
I would then expect it to return only the posts which has related tags with id 73
& 23
and id 1
og 2
, and if both post 1
and 2
has tags 73
and 23
the query would return both posts
I'd really like this too - and is core to the kind of structure I'm wishing to manage within GraphQL. Here's some details my use-case…
I am representing a deep tree structure, with each node called say "Item
" that has a collection of child Item
's and a single parent Item
.
I'd like to be able query for:
item
with a parent ID of x
.item
with a parent ID of undefined
.children
items including a specified depth of grand-children into the tree.Thanks!
Hey guys - any clue as to whether this is something that might be implemented in the near-ish future? I need to make a decision somewhat soon as to whether to use Graph.cool, and this is a blocker for me.
Thanks!
Hey @philcockfield! We're already working on this feature. We're expecting to release this in January. Hope that works for you?
Meanwhile @philcockfield you might be able to filter client-side: https://github.com/RelateNow/relate/blob/9dc89b460410b878bc2574af6043f79f86a1f5b3/containers/Users.js#L16
That's awesome @schickling. I really want to stick with graphcool.
What is the query parameters going to look like. Can you post your current API design/intentions? Even if they my change 😃
@sedubois - thanks for that stop-gap for filtering on the client. To be clear, that's pulling the entire data-set, then just running filters client side right?
That will certainly let me proceed with GraphCool for everything else, then refactor this when this feature is complete (in Jan)
@philcockfield yes, should work while you don't have too much data.
We are starting the implementation of this feature this week.
Feedback is highly appreciated at https://gist.github.com/sorenbs/dec072b8b4df0ab6cbd441861a53eaf9
This is now possible using both the Simple and the Relay API. 🎉
You can read more about this feature here: https://www.graph.cool/docs/reference/simple-api/filtering-by-field-xookaexai0#relation-filters
Thanks @schickling for that documentation!
Let's say I have a User model and a Track model, with a one-to-many tracks
relationship. How would I query all users which have at least one track?
This works but is a bit smelly:
{
allUsers(filter: {
tracks_some: {
id_not: "dummy"
}
}) {
givenName
}
}
Something like that is the way to go. A different solution:
{
allUsers(filter: {
tracks_some: {
# query all that have been created in the past (2017-02-06-16:13:39 is now)
createdAt_lt: "2017-02-06-16:13:39"
}
}) {
givenName
}
}
OK. Now I actually have a second relation retreats
with model Retreat. How do I select all users who have at least one track or at least one retreat? This gives an empty list:
{
allUsers(filter: {
OR: [{
retreats_some: {
id_not: "dummy"
}
tracks_some: {
id_not: "dummy"
}
}]
}) {
givenName
}
}
{
allUsers(filter: {
OR: [{
retreats_some: {
id_not: "dummy"
}
}, {
tracks_some: {
id_not: "dummy"
}
}]
}) {
givenName
}
}
You need to pass the different conditions as elements of the list argument OR
.
Ahh that was nasty, those two missing braces 😄 thanks @marktani!
Most helpful comment
I needed this as well to show posts related to tags, so i created this (rather ugly but working) temporary solution in angular 2.
https://gist.github.com/emolr/f2eaf46612ca7bbb47b6a814180da719
It does the job for now, but it is pretty slow compared to a single request, but it works, so I'll spend the extra time on a good loading animation. It might help someone needing the same functionality until it's implemented
My temporary approach have some limitations though.
When i began implementing search i found that i can't both have my search
filter: {id_in: ['1', '2']}
while using my tag posts lookup ids as they work on the same filter argument.
So dream scenario would be something like
filter: {AND: [id_in: ['1','2'], tagId_in: ['73', '23']]}
I would then expect it to return only the posts which has related tags with id
73
&23
and id1
og2
, and if both post1
and2
has tags73
and23
the query would return both posts