evenchange4 / graphql.macro

Compile GraphQL AST at build-time with babel-plugin-macros.
MIT License
239 stars 21 forks source link

Create React App: Cannot read property 'startsWith' of undefined #87

Closed nagman closed 5 years ago

nagman commented 5 years ago

Hi,

I'm using graphql.macro@1.3.5 with create-react-app@16.8, and when I try to import loader from graphql.macro, it throws me this:

TypeError: graphql.macro: Cannot read property 'startsWith' of undefined Learn more: https://www.npmjs.com/package/graphql.macro
    at Array.forEach (<anonymous>)
    at Array.forEach (<anonymous>)

I'm not really sure how to setup babel macros, so I may have forgotten a dependency (by the way, is graphql really needed?).

Here's my config:

package.json

{
  ...
  "dependencies": {
    ...
    "graphql": "^14.2.1",
    "graphql-request": "^1.8.2",
    "graphql.macro": "^1.3.5",
    "react": "^16.8.5",
    "react-scripts": "2.1.8",
    "typescript": "3.3.4000"
    ...
  },
  ...
}

.babelrc

{
  "plugins": ["graphql.macro"]
}

src/components/App.tsx

...
import {loader} from 'graphql.macro';
console.log(loader); // the error is thrown at this point
...
jissereitsma commented 5 years ago

I've bumped into the same error. However, it seems that there is some magic going on during compilation that throws this error. Do not try to dump loader to the console. Do you get the same error when you are simply using the loader, i.e. loader("./path/to/some.graphql")?

In my case, I'm getting no error when using loader("./path/to/some.graphql") but the error only appears as soon as I'm creating a new variable const myGraphQlFile = "./path/to/some/graphql"; loader(myGraphQlFile);.

jissereitsma commented 5 years ago

@nagman Why did you close this? Did you find a solution?

nagman commented 5 years ago

Actually it's been a while since I've resolved it.

But I forgot to close this thread, and now I don't remember how I solved it.

Here's my config: package.json

"graphql": "^14.2.1",
"graphql-request": "^1.8.2",
"graphql.macro": "^1.3.5",

no .babelrc

in react

import { loader } from 'graphql.macro';
const getThingsQuery = loader('./getThings.graphql');

getThings.graphql

{
  getThings {
    id
  }
}
jissereitsma commented 5 years ago

Thanks for the ping back. No solution for me unfortunately, but I'm glad it was solved on your end :)

biggytech commented 1 year ago

Same here, happening when I try to use path to file from a variable. I'd wanted to load a bunch of graphql-files from an array, but it doesn't allow that :(