When developing a GraphQL custom scalar in TypeScript, I am working on implementing the serialize/parseValue/parseLiteral functions. In order to match incoming values against a regex, I tried using
if (value.test(yearMonthDayRegex)) {}
as a condition, but encountered an Apollo runtime error:
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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7908596879f869bb7c6c1d9c1d9c7">[email protected]</a>/node_modules/graphql/utilities/coerceInputValue.js:151:26)
at coerceInputValueImpl (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c0b1e0d1c041d002c5d5a425a425c">[email protected]</a>/node_modules/graphql/utilities/coerceInputValue.js:117:34)
at coerceInputValueImpl (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294e5b485941584569181f071f0719">[email protected]</a>/node_modules/graphql/utilities/coerceInputValue.js:117:34)
at coerceInputValue (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4c594a5b435a476b1a1d051d051b">[email protected]</a>/node_modules/graphql/utilities/coerceInputValue.js:32:10)
at coerceVariableValues (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8aedf8ebfae2fbe6cabbbca4bca4ba">[email protected]</a>/node_modules/graphql/execution/values.js:132:69)
at getVariableValues (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a7b2a1b0a8b1ac80f1f6eef6eef0">[email protected]</a>/node_modules/graphql/execution/values.js:45:21)
at buildExecutionContext (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33544152435b425f7302051d051d03">[email protected]</a>/node_modules/graphql/execution/execute.js:280:63)
at execute (/Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ff8edfeeff7eef3dfaea9b1a9b1af">[email protected]</a>/node_modules/graphql/execution/execute.js:116:22)
at /Users/mej/Documents/elastigraph/elastigraph/node_modules/.pnpm/@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b1baa2b1b8bba4ffb7bba6b194e7fae4fae2">[email protected]</a>/node_modules/@envelop/core/cjs/orchestrator.js:376:33
Does anyone know how to incorporate regex functionality into a custom scalar like this? Am I overlooking something basic about regex in TypeScript? The regex involved looks like this:
const yearMonthDayRegex = /^d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
.