apollographql / graphql-tag

A JavaScript template literal tag that parses GraphQL queries
MIT License
2.32k stars 177 forks source link

Regular expression in gql template literal. #681

Open albertly opened 3 years ago

albertly commented 3 years ago

I am trying to use graphql-constraint-directive with apollo graphql server. 1.

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]
}
`

It didn't work for me. No match. Regular expression is correct. The sample works with this one @constraint(pattern: "^[0-9a-zA-Z]$")*.

So I realized that something wrong with ^05\d([-]{0,1})\d{7}$ in my template literal typeDefs.

When I've changed to: 2.

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]
}
`

I've got an error: GraphQLError: Syntax Error: Invalid character escape sequence: \d.

3. When I've changed to

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}`;

I've got the same error: GraphQLError: Syntax Error: Invalid character escape sequence: \d.

Is it a bug? Or how to deal with this situation?

glasser commented 2 years ago

Moving this to the graphql-tag repository.