How to Return All Fields for a GraphQL Query

A frequent GraphQL question is how to return all fields in a GraphQL query without specifying them all; see here or here or here. The conversation typically goes like this:

Q: “How can I return all the fields for a GraphQL query without specifying them? Can I just pass a wildcard character?”

A: “No, sorry. That’s not possible. That runs counter to the entire idea of GraphQL.”

Q: “But, that would be really useful for [this case] or [that other case].”

A: “I see your point, but it could also be abused, and it could cause [this problem] or [that other problem]. And it runs counter to the entire idea of GraphQL.”

And so it goes until one or the other party gets bored and leaves. And both sides have valid points. But the question remains.

I’ve been developing some GraphQL endpoints, and during development and testing it sure would be nice to get an entire object back from one of my queries. Specifically, the scenario that can be painful is:

  • I have a mutation to update an object
  • It requires posting the entire object as input
  • The object has a lot of fields, including nested fields, and it’s a pain to type the entire query
  • The schema is a bit in flux, so just saving the query as a snippet somewhere to paste back in to GraphiQL isn’t guaranteed to work

So I wrote gqall, pronounced JEE-call. It’s a command-line utility to run a GraphQL query and return all the fields. So you can type something like:

$ gqall https://fakerql.com/graphql "allPosts(count:1)"

And get back all the fields:

{
  "allPosts": [
    {
      "id": "cjngkb52m00ta28100t8qvier",
      "title": "Table",
      "body": "Et deleniti animi. Possimus natus vero quisquam omnis deleniti.",
      "published": false,
      "createdAt": "Tue Mar 13 2018 20:21:15 GMT+0000 (UTC)",
      "author": {
        "id": "cjngkb52o00tb28100b0rtxig",
        "firstName": "Anabel",
        "lastName": "Klocko",
        "email": "Jimmie78@yahoo.com",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg"
      }
    }
  ]
}

How does it work? You might have noticed that the Stack Overflow post above mentions GraphQL’s introspection capabilities. To run the query, gqall:

  1. Queries the specified GraphQL endpoint to get the schema
  2. Introspects the schema to get the fields for the query name you’ve specified
  3. Builds a new query by combining your query name with the fields
  4. Runs the query

gqall is installable from npm:

$ npm install -g gqall

It’s released under the MIT license and the source is available at https://github.com/hoop33/gqall.

It may not quell the question, but it does provide something of an answer. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.