Skip to content

graphql-codegen-client-preset doesn't work with absolute path aliases in Vite on Windows #524

@nathan-knight

Description

@nathan-knight

Describe the bug
Configuring Vite with a path alias (and TypeScript) causes it to create an invalid relative path.
For instance, setting this in vite.config.ts

  resolve: {
    alias: [
      {
        find: "@test/ui",
        replacement: path.resolve(import.meta.dirname, "./src"),
      },
    ],
  },

And this in tsconfig.json:

    "paths": {
      "@test/ui/*": ["src/*"]
    },

Then importing import { graphql } from '@test/ui/gql'; causes it to throw errors like:

Failed to resolve import "./../../../../../src/gql/graphql" from "src/App.tsx". Does the file exist?

If it must internally transform it to a relative import, it should become ./gql/graphql in this case since App.tsx is in the src directory.

I created a CodeSandbox but it ends up working there while doing the equivalent locally on Windows does not.
https://codesandbox.io/p/devbox/swc-graphql-plugin-path-alias-issue-repro-2v6j8d

If I switch it to use a relative import, it works.

Expected behavior
It should work on Windows like it does on other platforms

Additional context
If I had to guess the cause, it would be that it doesn't handle Windows paths like C:\\workspace\project correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions