Skip to content

Instantly share code, notes, and snippets.

@sorenbs
Created January 2, 2017 14:30
Show Gist options
  • Save sorenbs/dec072b8b4df0ab6cbd441861a53eaf9 to your computer and use it in GitHub Desktop.
Save sorenbs/dec072b8b4df0ab6cbd441861a53eaf9 to your computer and use it in GitHub Desktop.

API

field_every: FilterCondition # condition must be true for all nodes
field_some: FilterCondition # condition must be true for at least 1 node
field_none: FilterCondition # condition must be false for all nodes
field_is_null: Boolean # is the relation field null

Examples

# Get all Friends that are not in Group A and not in Group B and attend Event X

User(id: me){
  friends(filter:{AND: [
    {groups_every: {id_not: "group-a-id"}},
    {groups_every: {id_not: "group-b-id"}},
    {attended_some: {id: "event-x-id"}}
  ]})
}

# Get all Users that are in two groups A and B where the current user is also a member of A and B

allUsers(filter:{AND:[
  {groups_every: {id: "group-a-id", members_some: {id_neq: "current-user-id"}}},
  {groups_every: {id: "group-v-id", members_some: {id_neq: "current-user-id"}}}
]})

# All pictures where the author is not my friend.

allPictures(filter: { author { friends_every: { id_not: “my_id” } } })

# All pictures without author.

allPictures(filter: { author_is_null: true })

# All pictures with author.

allPictures(filter: { author_is_null: false })
@philcockfield
Copy link

philcockfield commented Jan 3, 2017

This is looking great guys! You're covering some ideas I wasn't originally worried about, but now I see you're thinking, they all look important.

So to test this against the scenario I've been envisaging, which is a deep tree-like structure:

image

https://github.com/graphcool/feature-requests/issues/20#issuecomment-268099118

Query all root level items (items with no parents):

allItems(filter:{
  {items_every: {parent_is_null: true}},
})

All children of a specific item:

allItems(filter:{
  {items_every: {parent: "my-parent-id"}},
})

Missing (?)

What I'm needing to do, which I don't think is accounted for in this API is reaching deeply into he item's descendent children.

Imagine I have a tree represented as say a traditional file-system folder like in OSX. And I had the root folder ID. How could I say, "get me all descendent items"? Or "all descendent items 5 levels down"?

@sorenbs
Copy link
Author

sorenbs commented Jan 3, 2017

Thanks Phil!

Your "missing" requirement is a little cumbersome to do with GraphQL, but is clearly possible as long as you accept a limit to the nesting. To get 5 levels you could make this query:

allItems(filter:{
  {items_every: {parent: "my-parent-id"}},
}){
  items {
    otherField
    items {
      otherField
      items {
        otherField
        items {
          otherField
        }
      }
    }
  }
}

@philcockfield
Copy link

@sorenbs, oh - I think that would actually work fine as I'd really just to want to go a few levels deep, then re-query if the user ended up digging deeper in the UI.

Super cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment