question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using absolute paths for importing GraphQL causes an error

See original GitHub issue

Absolute paths configuration:

// .babelrc
{
  "plugins": [
    "import-graphql"
  ]
}
// jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "."
  }
}

Import code:

import {qPosts} from 'schema/post.graphql';

Gives the error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at Array.map (<anonymous>)

If using relative import path there is no error. e.g.

import {qPosts} from '../schema/post.graphql';

I have two projects, one using version 2.8.1 and one using 2.7.0 with the same .babelrc and jsconfig.json both cannot use absolute imports with babel-plugin-import-graphql but work using relative imports.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:15
  • Comments:8

github_iconTop GitHub Comments

1reaction
derocommented, Jun 30, 2022

The problem is this plugin doesn’t take the set baseUrl into account. I worked around the issue in my project by dynamically setting the nodePath option to the absolute path of the baseUrl like this:

// .babelrc.js
const path = require('path')
const tsconfig = require('./tsconfig.json')

module.exports = (api) => {
  api.cache.using(() => process.env.NODE_ENV)

  return {
    "presets": ["next/babel"],
    "plugins": [
      /**
       * The `babel-plugin-import-graphql` plugin doesn't take
       * `tsconfig` or `jsconfig` `baseUrl` value into account,
       * which is why we need to manually supply it as an absolute
       * path here to ensure relative *.graphql imports work as expected.
       *
       * e.g.
       *
       * import MY_QUERY from 'graphql/queries/<...>'
       */
      ["import-graphql", { "nodePath": path.resolve(tsconfig.compilerOptions.baseUrl) }],
      ...
      ...
0reactions
joshuabakercommented, Nov 23, 2022

We ended up just using graphql-tag in .js files. I wrote a script to convert them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · detrohutt/babel-plugin-import-graphql
Contribute to detrohutt/babel-plugin-import-graphql development by creating an account on ... using absolute paths for importing GraphQL causes an error.
Read more >
How to change relative paths to absolute paths for imports
I have a React project set up using webpack, eslint, flow and jest. Here is a documentation of the places that require configurations...
Read more >
Using eslint with typescript - Unable to resolve path ...
In my eslintrc.js config file, the "import/resolver" object needed to sit within the "rules" node, not the " ...
Read more >
Avoiding Import Issues in TypeScript Monorepos
Importing elements from the same library through the library's path mapping/through a barrel will cause issues for sure, whether that is circular ...
Read more >
Configuring absolute paths in React for Web without ...
We are already accustomed to having autocomplete when we are going to import the files using relative paths. This error happens because VSCode ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found