I have seen that inserting an Input Type is recommended in the context of mutations but does not say anything about queries.

For instance, in learn tutorial just say:

This is particularly valuable in the case of mutations, where you might want to pass in a whole object to be created

I have this query:

type query {
    person(personID: ID!): Person
    brazilianPerson(rg: ID!): BrazilizanPerson
    foreignerPerson(passport: ID!): ForeignerPerson
}

Instead of having a different type just because of the name (rg, passport) of the fields, or put one more argument like type in query, I could not just have the Person with an documentNr field and do an Input type like that?

input PersonInput {
   documentNr : ID!
   type: PersonType # this type is Foreign or Brazilian and with this I k 
}

PersonType is a enum and with him I know if the document is a rg or a passport.

1 Answers

1
Daniel Rearden On Best Solutions

No, there is nothing incorrect about your approach. The GraphQL spec allows any field to have an argument and allows any argument to accept an Input Object Type, regardless of the operation. In fact, the differences between a query and a mutation are largely symbolic.

It's worth pointing out that any field can accept an argument -- not just ones at the root level. So if it suited your needs, you could easily set up a schema that would allow queries like:

query {
  person(id: 1) {
    powers(onlyMutant: true) {
      name
    }
  }
}