【发布时间】:2021-02-23 14:34:38
【问题描述】:
我正在尝试将 graphql-constraint-directive 与 apollo graphql 服务器一起使用。 我需要把注册。像这样的模板文字中的表达式
const typeDefs = gql`
input ClaimInput {
id: String!
date: String!
phone: String @constraint(pattern: "^05\d([-]{0,1})\d{7}$")
email: String
invoice: String
doctorId: String
therapy: [TherapyInput]
files: [FileInput]
}
`
它对我不起作用。没有匹配。正则表达式是正确的。该示例适用于这个 @constraint(pattern: "^[0-9a-zA-Z]$")*.
所以我意识到我的模板字面量 typeDefs 中的 ^05\d([-]{0,1})\d{7}$ 有问题。
我已签入 Node REPL:
> v = `^05\d([-]{0,1})\d{7}$`
'^05d([-]{0,1})d{7}$'
> v = `^05\\d([-]{0,1})\d{7}$`
'^05\\d([-]{0,1})d{7}$'
当我把 \d \ 忽略。 当我把 \\d 我有 \\d.不是我需要的。
如何处理这种情况?
编辑: 我认为问题出在 'apollo-server' 的 gql
当我改为:
const typeDefs = gql`
input ClaimInput {
id: String!
date: String!
phone: String @constraint(pattern: "^05\\d([-]{0,1})\\d{7}$")
email: String
invoice: String
doctorId: String
therapy: [TherapyInput]
files: [FileInput]
}
`
我有一个错误: GraphQLError:语法错误:无效的字符转义序列:\d。
当我变成
const typeDefsRaw = String.raw`
input ClaimInput {
id: String!
date: String!
phone: String @constraint(pattern: "^05\d([-]{0,1})\d{7}$")
email: String
invoice: String
doctorId: String
therapy: [TherapyInput]
files: [FileInput]
}
`
const typeDefs = gql`${typeDefsRaw}`;
我遇到了同样的错误: GraphQLError:语法错误:无效的字符转义序列:\d。
【问题讨论】:
-
无论如何,我已经在 apollo-serve repo 中提交了一个新问题。 github.com/apollographql/apollo-server/issues/4727。如果有任何更新,我会让大家知道
-
为什么不使用 graphql-constraint-directive ?它是如何与阿波罗服务器相关的?为什么 apollo-server 必须修复一些扩展问题?
-
@xadm GraphQLError:语法错误:无效的字符转义序列:\d。 gql中的问题
-
'// 名称必须匹配 /^[_a-zA-Z][_a-zA-Z0-9]*$/ 根据 graphql-js' >> github.com/confuser/graphql-constraint-directive/blob/master/… ... 然后不支持任何转义...您必须在解析器级别对其进行限制
标签: javascript graphql apollo-server string-literals template-literals