Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENOENT, when using # import syntax #284

Closed
dan-cooke opened this issue Feb 12, 2019 · 17 comments
Closed

ENOENT, when using # import syntax #284

dan-cooke opened this issue Feb 12, 2019 · 17 comments

Comments

@dan-cooke
Copy link

dan-cooke commented Feb 12, 2019

Unless I am missing something crucial, I can't seem to get the # import syntax to work.

Reproduction

I have created the following repo to reproduce the bug

As you can see in that repo,
src/index.js does the following:

  1. It imports typeDefs/index.graphql using the import-graphql babel plugin, this works as expected as the file is simply read as a string.

  2. It calls importSchema from graphql-import. This is where the problem occurs.

If you run that repo you will get the following error

Error: ENOENT: no such file or directory, open './store.graphql'
    at Object.openSync (fs.js:449:3)
    at Object.readFileSync (fs.js:349:35)
    at read (/Users/dancooke/Playground/graphql-import-bug/node_modules/graphql-import/dist/index.js:26:19)
    at /Users/dancooke/Playground/graphql-import-bug/node_modules/graphql-import/dist/index.js:197:43
    at Array.forEach (<anonymous>)
    at collectDefinitions (/Users/dancooke/Playground/graphql-import-bug/node_modules/graphql-import/dist/index.js:190:16)
    at importSchema (/Users/dancooke/Playground/graphql-import-bug/node_modules/graphql-import/dist/index.js:78:14)
    at Object.<anonymous> (/Users/dancooke/Playground/graphql-import-bug/build/index.js:28:45)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)

The issue

Looking at the stack trace, the problem lies with how graphql-import is resolving the path. At index.js:26:19.


var read = function (schema, schemas) {

    if (isFile(schema)) {
        
        console.log("schema path: " + schema); // schema path: ./store.graphql
        
        return fs.readFileSync(schema, { encoding: 'utf8' });
    }
    return schemas ? schemas[schema] : schema;
};

The read function is not being passed the correct path when using relative paths. As you can see it is being passed the path _as is. i.e ./store.graphql and obviously this is not right, as the graphql file does not exist relative to the module, but relative to my source code.

Other things I've tried

  1. I've tried removing the # import * from './store.graphql' from typeDefs/index.graphql`, this works correctly, indicating that the issue is purely when resolving # import syntax
  2. Tried .gql files
  3. Tried passing the path to importSchema directly, as opposed to a string.

Environment

node: 11.9
graphql-import: 0.7.1

@didaquis
Copy link

@danielcooke1996 Did you found any solution?

@dan-cooke
Copy link
Author

dan-cooke commented Feb 17, 2019

@didaquis
Copy link

Thanks

@simondch
Copy link

Same issue here.

node v10.15.1 graphql-import v0.7.1

I switched to babel-plugin-import-graphql to have something working but I really want to use graphql-import

@danielsimao
Copy link

Same issue here.

I'm really looking forward to have this issue solved. Can't event put babel-plugin-import-graphql to work.

@dan-cooke
Copy link
Author

Same issue here.

I'm really looking forward to have this issue solved. Can't event put babel-plugin-import-graphql to work.

When using babel-plugin-import-graphql make sure you have exact import syntax. read the docs closely.

It needs to be - #import 'File.gql'

Note there is no space between the hash and the import, and the filename has single quotes

@jackgray
Copy link

jackgray commented Apr 4, 2019

Having the same issue. Can't figure out why it worked before but not now though..

@danielsimao
Copy link

danielsimao commented Apr 4, 2019

Make sure you give the correct path. For example:

If your schema.graphql is in src/schema, make sure that when you a passing it to typeDefs in your GraphQL Server, you give it the full path from your GraphQL Server to your schema location: src/schemas/schema.graphql

@didaquis
Copy link

didaquis commented Apr 4, 2019

Maybe my repo could be an example for you: https://github.com/didaquis/react-graphql-apollo-example-server/tree/master/src/gql/schemas

@danielsimao
Copy link

Having the same issue. Can't figure out why it worked before but not now though..

@jackgray, do you need a more practical example?

@dan-cooke
Copy link
Author

dan-cooke commented Apr 5, 2019

@danielsimao

Make sure you give the correct path. For example:

If your schema.graphql is in src/schema, make sure that when you a passing it to typeDefs in your GraphQL Server, you give it the full path from your GraphQL Server to your schema location: src/schemas/schema.graphql

As mentioned above, this will not work. As graphql-import is not resolving paths from the source code (ie. the GraphQL server) but from the graphql-import node module.

I have included a reproduction repo where you can see this behaviour. Note I have included full paths for my schema.

@ken-nah
Copy link

ken-nah commented Aug 24, 2019

Maybe my repo could be an example for you: https://github.com/didaquis/react-graphql-apollo-example-server/tree/master/src/gql/schemas

Yeah this worked for me. Thank you

@Orteu
Copy link

Orteu commented Aug 25, 2019

You need to import the .graphql file from root even if your file is in the same folder:

Let's say you've got this structure:

root/
   schema/
      user.graphql
      index.js

If you are trying to import user.graphql from index.js you will need to do this:

const user = importSchema('schema/user.graphql')

@ardatan ardatan added the bug label Dec 31, 2019
@ardatan
Copy link
Owner

ardatan commented Dec 31, 2019

Hi @dan-cooke !
In 1.0.0 beta release, we introduced a lot of changes. And the way we load files is changed in this beta version.
So could you install graphql-import@beta to try new changes? Don't forget to modify your code regarding to the migration notes in README.
https://github.com/ardatan/graphql-import#updating-from-07x

@dan-cooke
Copy link
Author

We have since switched to merge-graphql-schemas as we don't even have to worry about importing.

I don't really have much time to test this out for you, but you are welcome to clone the reproduction repo and determine for yourself!

@ardatan
Copy link
Owner

ardatan commented Jan 14, 2020

@dan-cooke I'm happy you are using merge-graphql-schemas which is another library we also maintain and it uses GraphQL Toolkit behind the scenes like GraphQL Import.

@ardatan
Copy link
Owner

ardatan commented Mar 17, 2020

Available in 1.0.0!

@ardatan ardatan closed this as completed Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants