gpt4 book ai didi

graphql - 不允许在 GraphQL Schema 中使用空字符串

转载 作者:行者123 更新时间:2023-12-05 02:29:50 24 4
gpt4 key购买 nike

我们有一个字段需要类型为 String! 的不可空字符串,但是如果它是空字符串或只有空格的字符串,Apollo 将排除该字段。

我们尝试过使用标量类型 NonEmptyString 但没有成功。

有没有办法将非空字符串类型添加到我们的 Apollo Server graphql 架构中?

谢谢

最佳答案

您好,这应该可以使用自定义标量

export const NonEmptyString = new GraphQLScalarType({
name: 'NonEmptyString',
description: 'Non empty string',
serialize: (value: unknown): string => {
if (typeof value !== 'string' || value === '') { // or any custom validation
throw new Error('Wrong value type');
}

return value
},
parseValue: (value: unknown): string => {
if (typeof value !== 'string' || value === '') {
throw new Error('Wrong value type');
}

return value
},
});

关于graphql - 不允许在 GraphQL Schema 中使用空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72013470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com