如何处理返回布尔值的 GraphQL 查询? [英] How to approach a GraphQL query that returns a boolean value?

查看:30
本文介绍了如何处理返回布尔值的 GraphQL 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户注册过程中需要检查电子邮件是否可用或被占用.目标是使用 GraphQL 快速查询 API 服务器,并让它告诉我们电子邮件是否可用或已被占用.

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.

对于使用 GraphQL 的简单布尔类型情况的一般最佳实践是什么?

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.

请求:

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

回复:

{
  "data": {
    "emailExists": {
      "is": true
    }
  }
}

推荐答案

查询"只是 Query 类型的字段.一个字段可以返回任何输出类型,包括标量——它不需要返回一个对象.因此,拥有如下架构就足够了:

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!
}

选择对象类型的唯一原因是,如果您预计将来要添加其他字段(即当前 is 字段以外的其他字段).

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).

这篇关于如何处理返回布尔值的 GraphQL 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆