Apollo GraphQL - 用户友好的类型验证错误
Apollo GraphQL - User-friendly type-validation errors
我目前正在构建一个将 Apollo 用于我的 GraphQL API 的应用程序。正如我们所知,GraphQL 为某些字段提供了类型和非空检查。假设我希望 foo
字段成为 Int
并且它是不可空字段,我们可以在模式(或类型定义)
中执行此操作
foo: Int!
产生这种错误
"message": "Variable \"$input\" got invalid value \"foo\"; Expected type Int; Int cannot represent non 32-bit signed integer value: foo"\
但是,假设我想将消息自定义为
"message": "Foo is wrong"
是否可以更改默认错误消息?如果您在解析器中检查不可为 null 在技术上是可行的,但我认为类型也不可能。
稍微更改在标量的序列化或解析期间抛出的错误消息的唯一方法是提供这些标量的自定义实现。例如,您可以为您选择的标量复制 source code:
const { GraphQLScalarType } = require('graphql')
const inspect = require('graphql/jsutils/inspect').default
function serializeID(rawValue) {
const value = serializeObject(rawValue);
if (typeof value === 'string') {
return value;
}
if (Number.isInteger(value)) {
return String(value);
}
throw new TypeError(`ID cannot represent value: ${inspect(rawValue)}`);
}
function coerceID(value) {
if (typeof value === 'string') {
return value;
}
if (Number.isInteger(value)) {
return value.toString();
}
throw new TypeError(`Oh no! ID cannot represent value: ${inspect(value)}`);
}
const ID = new GraphQLScalarType({
name: 'ID',
description:
'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
serialize: serializeID,
parseValue: coerceID,
parseLiteral(ast) {
return ast.kind === Kind.STRING || ast.kind === Kind.INT
? ast.value
: undefined;
},
})
const resolvers = {
ID,
/* the rest of your resolvers */
}
现在验证消息将改为:
Variable \"$id\" got invalid value true; Expected type ID; Oh no! ID cannot represent value: true
不过,无法更改消息的第一部分。
也就是说,验证错误意味着您的代码出现问题,无论是在客户端(验证输入时)还是在服务器端(验证输出时)。用户可能不应该看到这些错误,而应该显示一些通用的错误消息。
我目前正在构建一个将 Apollo 用于我的 GraphQL API 的应用程序。正如我们所知,GraphQL 为某些字段提供了类型和非空检查。假设我希望 foo
字段成为 Int
并且它是不可空字段,我们可以在模式(或类型定义)
foo: Int!
产生这种错误
"message": "Variable \"$input\" got invalid value \"foo\"; Expected type Int; Int cannot represent non 32-bit signed integer value: foo"\
但是,假设我想将消息自定义为
"message": "Foo is wrong"
是否可以更改默认错误消息?如果您在解析器中检查不可为 null 在技术上是可行的,但我认为类型也不可能。
稍微更改在标量的序列化或解析期间抛出的错误消息的唯一方法是提供这些标量的自定义实现。例如,您可以为您选择的标量复制 source code:
const { GraphQLScalarType } = require('graphql')
const inspect = require('graphql/jsutils/inspect').default
function serializeID(rawValue) {
const value = serializeObject(rawValue);
if (typeof value === 'string') {
return value;
}
if (Number.isInteger(value)) {
return String(value);
}
throw new TypeError(`ID cannot represent value: ${inspect(rawValue)}`);
}
function coerceID(value) {
if (typeof value === 'string') {
return value;
}
if (Number.isInteger(value)) {
return value.toString();
}
throw new TypeError(`Oh no! ID cannot represent value: ${inspect(value)}`);
}
const ID = new GraphQLScalarType({
name: 'ID',
description:
'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
serialize: serializeID,
parseValue: coerceID,
parseLiteral(ast) {
return ast.kind === Kind.STRING || ast.kind === Kind.INT
? ast.value
: undefined;
},
})
const resolvers = {
ID,
/* the rest of your resolvers */
}
现在验证消息将改为:
Variable \"$id\" got invalid value true; Expected type ID; Oh no! ID cannot represent value: true
不过,无法更改消息的第一部分。
也就是说,验证错误意味着您的代码出现问题,无论是在客户端(验证输入时)还是在服务器端(验证输出时)。用户可能不应该看到这些错误,而应该显示一些通用的错误消息。