beardeadclown
beardeadclown

Reputation: 377

Can I shorten graphql queries?

Suppose I have following query:

query a($f: Int) {
  a {
    b(f: $f) {
      c {
        d {
          e
        }
      }
    }
  }
}

Can I somehow make it smaller? Sort of like this

query a($f: Int) {
  a.b(f: $f).c.d {
    e
  }
}

Upvotes: 0

Views: 335

Answers (2)

Herku
Herku

Reputation: 7666

A syntax like this is discussed since 2016 in this issue, but it is unlikely that it will ever be implemented. The added complexity to the GraphQL language is probably not worth the benefits, as this only applies in very few use cases. It does not really save data nor does it massively save typing. You don't need any of the whitespace though (this is why I say it doesn't really save data. It only saves the last closing bracket).

query a($f: Int){
  a{b(f: $f){c{d{
    e
  }}}}
}

Upvotes: 1

FloWy
FloWy

Reputation: 984

First of all, I would see such an easy query but really deep query as a bad design choice for GraphQL because it takes a lot of requests to get to the final result. However, GraphQL does not limit the depth or complexity of the operations. Furthermore, a common use case for nested queries is recursive queries like comments. Still, there are better solutions such as using Directives and Fragments.

But, to come back to your question, there is no real way to make these queries much smaller. But to keep your arguments small and reusable you can look at fragments.

Upvotes: 1

Related Questions