graphql - 可以是字符串或对象的值的Graphql标量类型?
问题描述
我有一个用于注册的 api,它返回字符串或 null 的错误,
error: 'Email already use' or error: null
我如何在架构中构建它?我现在拥有的是:
const typeDefs = gql`
type Mutation {
signUp(email: String, password: String): String
}
`;
由于 typeof null 是对象,我怎样才能在 graphql 中做到这一点?
signUp(email: String, password: String): String || Object
帮助?
解决方案
GraphQL 具有返回错误值的标准语法,您的架构不需要直接考虑这一点。
在您的架构中,我将“无条件”返回您通常期望返回的任何类型:
type UserAccount { ... }
type Query {
me: UserAccount # or null if not signed in
}
type Mutation {
signUp(email: String!, password: String!): UserAccount!
}
如果不成功,您将返回一个空字段值(即使架构理论上声称它不应该)和一个错误。
{
"errors": [
{
"message": "It didn’t work",
"locations": [ { "line": 2, "column": 3 } ],
"path": [ "signUp" ]
}
]
}
推荐阅读
- python - django2.1 发送电子邮件失败:ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:833)
- php - PHP - 代码为数据库中的每条记录创建单独的表
- textures - 如何在纹理中正确绘制线条
- azure-pipelines-release-pipeline - 我可以从 VSTS 添加发布设置文件以在 Microsoft azure 上部署项目吗
- javascript - 初始化 WebSocket 时 IE11 上的 SecurityError
- email-validation - 如何为订阅按钮创建实时电子邮件验证器?
- redux-form - 输入模拟更改在提供事件对象的酶中不起作用
- javascript - 试图将产生的前两个数字乘以特定数字,有没有办法在不使用嵌套循环的情况下做到这一点?
- sql - SQL - 使用频率计算激励(模式)
- dsl - 我可以设置功能在空手道中运行的顺序吗?