detrohutt / babel-plugin-import-graphql

Enables import syntax for .graphql and .gql files
https://www.npmjs.com/package/babel-plugin-import-graphql
MIT License
314 stars 22 forks source link

Fails when importing graphql files from node_modules #92

Closed GiancarlosIO closed 2 years ago

GiancarlosIO commented 2 years ago

When I try to import a graphql from a library, it gives the following error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

The import that gives the error is something like:

import MY_QUERY from '@my-lib/web/graphql/myQuery.graphql';

Is this a bug or a limitation? If it's the latter it would be good to have it documented in the readme.

Thanks!

GiancarlosIO commented 2 years ago

I have solved this by using the babel-plugin-module-resolver and transforming the library path from absolute to relative:

// babel.config.js
plugins: [
    [
      'module-resolver',
      {
        root: ['./src'],
        alias: {
          '@': './src',
          // needed to compile graphql files because babel-plugin-graphql-import doesn't support imports from node_modules.
          // related https://github.com/detrohutt/babel-plugin-import-graphql/issues/79
          '@my-local-lib/web': '../web',
          '@my-remote-lib/web': './node_modules/my-lib/web',
        },
      },
    ],
  ],