我正在 TypeScript 中创建一个 GraphQL 自定义标量并构建序列化/parseValue/parseLiteral 函数。我希望我的标量将传入值与正则表达式相匹配,但是当我将
if (value.test(yearMonthDayRegex)) {}
作为条件时,我收到 Apollo 运行时错误:
TypeError: value.test is not a function
at GraphQLScalarType.coerceType (/Users/mej/Documents/elastigraph/elastigraph/packages/core/src/lib/gqlScalarTypes.ts:30:20)
at coerceInputValueImpl (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/utilities/coerceInputValue.js:151:26)
at coerceInputValueImpl (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/utilities/coerceInputValue.js:117:34)
at coerceInputValueImpl (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/utilities/coerceInputValue.js:117:34)
at coerceInputValue (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/utilities/coerceInputValue.js:32:10)
at coerceVariableValues (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/execution/values.js:132:69)
at getVariableValues (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/execution/values.js:45:21)
at buildExecutionContext (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/execution/execute.js:280:63)
at execute (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/[email protected]/node_modules/graphql/execution/execute.js:116:22)
at /Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/@[email protected]/node_modules/@envelop/core/cjs/orchestrator.js:376:33
有没有办法将正则表达式功能合并到这个自定义标量中?还是我错过了关于 TS 中正则表达式的更基本的东西?我的正则表达式看起来像
const yearMonthDayRegex = /^d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
.