10

Is there a way (or a shortcut) to populate all fields of a Query?

Let's take https://graphql.org/swapi-graphql/ as an example.

I know that by ctrl + space I can invoke the autosuggest. enter image description here

But what if I wanted to populate all of the fields (name, height etc..) without having to manually type them out?

Dimo
  • 467
  • 6
  • 13

3 Answers3

4

Execute the query without specifying the child fields

graphiql will kindly fill in each field for you. In your case:

{
    person(personID: 1)
}

will autocomplete to

{
  person(personID: 1) {
    name
    birthYear
    eyeColor
    gender
    hairColor
    height
    mass
    # etc...
  }
}
bryce
  • 698
  • 10
  • 11
  • Upvoted as this is a super useful feature when exploring a schema using graphiql. However I've noticed it doesn't seem to populate all fields by default. This seems to be undocumented and there is nothing remarkably different in the field specifications in the backend between those that appear and those that don't - any ideas? – digitalacorn Jun 13 '20 at 15:07
  • 4
    https://github.com/graphql/graphiql/blob/b538f6e6f39b954cdf0c264f7adfc3e98666f802/packages/graphiql/src/utility/fillLeafs.ts#L41 It seems to autofill 'id', 'edges' and 'node' fields. If none of those are found, it adds all leaves it can find on the type. – j2L4e Mar 11 '21 at 12:02
1

Generally, if you enter the base type without the paramaters like shown below the fields will populate when you "Ctrl-Enter or press the play button" directly after it. Strangely, I don't see it happening on that example url provided but it is functioning for me using a GraphpliQL interface that I am using through Prismic.

{
    person
}
timtyrrell
  • 21
  • 1
0

There is not a way to do that in the context you are hoping for. Graphql was designed with the idea that you know exactly what you're querying for. What you can do though, is write it down once and then save it on your notes so you have that query for the future.

Alternatively, if you're working with an autogenerated model schema based on schema.graphql (ex: aws amplify project), you can go into the schema files and copy their pre-generated queries, mutations, etc., based on your defined input types.

These are usually found in your backends' src/graphql/ folder. See example string for this arbitrary model:

example of autogen query

Eagl3
  • 199
  • 1
  • 15