如何在graphql中记录错误?
希望你能通过上面的模式理解我的意思。我想知道是否有任何代码生成器可以支持以这种方式记录的错误,这样我就可以减少在客户端和服务器端编写的代码 我不想像下面这样定义错误如何在graphql中记录错误?,graphql,Graphql,希望你能通过上面的模式理解我的意思。我想知道是否有任何代码生成器可以支持以这种方式记录的错误,这样我就可以减少在客户端和服务器端编写的代码 我不想像下面这样定义错误 type Query { """ ErrorCode: EMAIL_DUPLICATED type EmailDuplicatedError { email: String! source: UserSource! }
type Query {
"""
ErrorCode: EMAIL_DUPLICATED
type EmailDuplicatedError {
email: String!
source: UserSource!
}
enum UserSource {
Google
Facebook
Github
}
"""
register(email: String!, password: String!): AccessToken!
}
"""
The AccessToken scalar type is a string of 16 characters.
"""
scalar AccessToken
因为我希望在errors字段中响应错误,api只显示成功后可以得到的结果
感谢您花时间阅读本文。有一些方法可以使用GraphQL进行错误处理,我向您推荐两种方法:
- 使用响应扩展:
GraphQL JSON
有一个名为response.error
的字段,您可以使用该字段设置extensions
字段,如:code
{
“错误”:{
“错误”:[
{
“消息”:“示例”,
“扩展”:{
“代码”:“您的代码”
}
}
]
}
}
- 使用联合体: 萨沙·所罗门(Sasha Solomon)的一篇媒体文章谈到了这一点:
type Query {
register(email: String!, password: String!): RegisterResponse
}
type RegisterResponse {
accessToken: AccessToken
error: EmailDuplicatedError
}
type EmailDuplicatedError {
email: String!
source: UserSource!
}
enum UserSource {
Google
Facebook
Github
}
"""
The AccessToken scalar type is a string of 16 characters.
"""
scalar AccessToken
我现在正在使用extensions.code。问题是我不知道如何在graphql中记录这一点,因此前端开发人员可以提前知道他们可能遇到的错误,这样他们就可以成功地处理失败。我想找到一个代码生成器,它可以生成类型脚本类型,以促进第二种方法的前端和后端开发,它给查询带来了负担,这就是为什么我想避免它。
type User {
id: ID!
name: String
}
type Suspended {
reason: String
}
type IsBlocked {
message: String
blockedByUser: User
}
type UnavailableInCountry {
countryCode: Int
message: String
}
"User, or reason we can't get one normally"
union UserResult = User | IsBlocked | Suspended
type Query {
user: UserResult!
}