Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to use an Input Type for a graphql Query?

Tags:

graphql

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.

like image 295
ProfessorX Avatar asked Apr 10 '19 20:04

ProfessorX


People also ask

How do you input type in GraphQL?

To make your schema simpler, you can use “input types” for this, by using the input keyword instead of the type keyword. id: ID! Here, the mutations return a Message type, so that the client can get more information about the newly-modified Message in the same request as the request that mutates it.

Is it possible to use variables in a GraphQL query?

There are a variety of different variable data types available on Contentful's GraphQL API. As well as the standard data types such as String, Int and DateTime, you can also pass variables to a query that are entry-specific and API-specific.

Is it possible to use inheritance with GraphQL input types?

No, the spec does not allow input types to implement interfaces. And GraphQL type system in general does not define any form of inheritance (the extends keyword adds fields to an existing type, and isn't for inheritance).

What are the 2 most common actions in GraphQL?

From the point of view of the client, the most common GraphQL operations are likely to be queries and mutations.


1 Answers

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
    }
  }
}
like image 153
Daniel Rearden Avatar answered Oct 23 '22 17:10

Daniel Rearden