Search code examples
graphqlgraphql-ruby

Graphql complex boolean queries


I understand the principles of querying via graphql from the docs you could search:

{
  "hero": {
    "name": "R2-D2"
  }
}

but how about you want to do something a bit more intricate such as:

{
  "hero": {
    "name": "R2-D2 AND C-3PO AND BB-8 NOT K-2SO"
  }
}

is there any way to pass a string like this and get the appropriate results?


Solution

  • No, there isn't.

    You can read through the GraphQL spec and see what it does and doesn't define. In particular the spec doesn't define any sort of filtering, any sort of expression language, or any sort of Boolean combinators. (There is no native way to say the equivalent of SQL's WHERE NAME='foo' without a field resolver explicitly adding it.)

    What GraphQL allows for field arguments is sufficiently open-ended that you can build richer queries on top of it, but that's very specific to some application or library. Two prominent examples are the GitHub GraphQL API (which tends to allow exact-match queries on selected fields but nothing richer) and the Prisma API (which has an involved multi-level object scheme to replicate SQL queries).