GraphiQL: How to populate all fields
Asked Answered
R

3

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?

Rajput answered 24/9, 2018 at 7:35 Comment(1)
there's no way to do this imo.Vacillating
S
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...
  }
}
Shericesheridan answered 24/5, 2019 at 21:42 Comment(2)
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?Sonya
github.com/graphql/graphiql/blob/… 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.Mcdonnell
P
0

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
}
Preemption answered 22/10, 2020 at 14:2 Comment(0)
C
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

Cost answered 26/9, 2022 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.