Skip to content

escaped backticks in template literals aren't parsable by codegen #2272

@smeijer

Description

@smeijer

Describe the bug
Codegen doesn't know how to handle backtick escaping in template literals.

To Reproduce
Define your schema in a js or ts file, with the help of graphql-tag. And add a field description that uses backtickets to highlight some code in your document browser. (graphql playground for example).

  1. My GraphQL schema:
import gql from 'graphql-tag';

export default gql`
  type Query {
    user(id: ID!): User!
  }

  type User {
    id: ID!
    "Choose a nice username, so users can \`@mention\` you."
    username: String!
    email: String!
  }
`;

Error thrown:

✖ GraphQL request
    Syntax Error: Invalid character escape sequence: \`.

    GraphQL request (7:42)
    6:   id: ID!
    7:   "Choose a nice username, so users can \`@mention\` you."
                                                ^
    8:   username: String!

Sandbox: https://codesandbox.io/s/codegen-backtick-escape-issue-qn1hb

  1. My codegen.yml config file:
schema: schema.js
documents: operations.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-operations

Expected behavior
Having User with a generated type like:

export type User = {
  __typename?: "User";
  id: Scalars["ID"];
  /** Choose a nice username, so users can `@mention` you. */
  username: Scalars["String"];
  email: Scalars["String"];
};

Environment:

  • OS: codesandbox
  • @graphql-codegen/add: 1.3.1
  • @graphql-codegen/cli: 1.3.1
  • @graphql-codegen/typescript: 1.3.1
  • @graphql-codegen/typescript-operations: 1.3.1
  • NodeJS: v10.16.0

Additional context
In the sanbox posted above, you can change the codegen.yml to use schema.graphql instead of schema.js to see it work. That's the desired outcome, only I need to be able to use .js / .ts files with graphql-tag.

Writing my descriptions like I do in the sandbox, does work, as Apollo parses it and graphql playground displays it. It's only codegen that doesn't compile it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions