12

Need to check whether an email is available or taken during the user sign-up process. The goal is to quickly query, using GraphQL, the API server and have it tell us if the email is available or taken.

What is the general best practice on a simple boolean-ish type of situation using GraphQL?

Below is what I have come up with but I am unsure if this is a good practice or not and want to hear feedback on a better practice on queries like this.

Request:

query {
  emailExists(email:"jane@doe.com") {
    is
  }
}

Response:

{
  "data": {
    "emailExists": {
      "is": true
    }
  }
}
Chad Taylor
  • 337
  • 1
  • 3
  • 9

1 Answers1

30

A "query" is just a field on what happens to be the Query type. A field can return any output type, including scalars -- it doesn't need to return an object. So it's sufficient to have a schema like:

type Query {
  emailExists(email: String!): Boolean!
}

The only reason to prefer an object type would be if you anticipated wanting to add additional fields in the future (i.e. something other than your current is field).

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183