Search code examples
node.jsnpmgraphqlgraphql-js

multi-repository graphql types with local npm link


Given 2 repositories user-repo and project-repo, I define UserType in the user-repo, and in package.json of project-repo I do this:

"dependencies": {
    "user-repo": "git+ssh://[email protected]/me/user-repo"
}

and everything works (UserType loads in project-repo). However if I link the repo locally like so:

"dependencies": {
    "user-repo": "file:../../user-repo"
}

UserType instanceof GraphQLObjectType returns false. And it's only the graphql types that are acting up. Everything else like models is getting loaded just fine.

I've tried npm linking (npm link user-repo), doing it both the require and import ways, and it didn't help. Any ideas?


Solution

  • This problem was also discussed in graphql-js issue tracker:

    https://github.com/graphql/graphql-js/issues/1091#issuecomment-349874772

    Answer by FB developer @leebyron:

    Unfortunately npm link is pretty broken, though I'm surprised using a file path as a dependency didn't work for you.

    This issue occurs when there are multiple instances of graphql-js in your node_modules directory. npm-link works by creating symlink to another directory, if that other directory also has a node_modules folder with graphql-js within it, then you'll have multiple copies.

    When I develop in this way, I usually set up a script which cp's my sub-project's built source directly into the main project's node_modules directory, avoiding any filesystem symlinking hyjinx.

    We ended up doing this: common code is no longer a npm package, we use symlink to map common code directory in each application directory and import/require same way as any application module.

    Hint for those using docker: you will not be able to put common code into the container this way, docker forbids symlinks. To go around this you can copy the common code in the app temporary directory, build container and remove temporary directory afterwards.