graphql-import-loader
graphql-import-loader copied to clipboard
Error with relative paths inside sdl
Error, no such file or directory:
# import * from './myFolder/myFile.graphql'
Ok, with absolute paths:
# import * from 'src/.../myFolder/myFile.graphql'
I didnt meet any problem like this with graphql-import.
I just saw that on the documentation you have absolute paths, but what blocks to have the same behavior with the loader?
+1
Maybe it's related to the graphql-import version that's being used?
"dependencies": {
"graphql-import": "^0.4.5"
},
+1
Nope, it's actually a bug in https://github.com/prisma/graphql-import-loader/blob/master/src/index.ts#L8
It should be this.resource rather than source, as source is the file contents, which means that graphql-import doesn't have context of the location of the schema; If you pass importSchema(schema, [schemas]) a schema argument of type string which ends with ".graphql" then it does the file loading instead, which means all your imports get resolved to that file; without it, it's just from the root of your project.
(addendum: I just decided to switch to webpack-graphql-loader, which works roughly similar, but not as advanced on imports (it's more or less just file concatenation).
This is a possible fix, it also adds all the files to webpack's watch mode:
import { importSchema, parseSDL } from 'graphql-import'
import { dirname, resolve } from 'path'
export default function (source) {
const callback = this.async()
this.cacheable()
parseSDL(source).forEach(rawModule => {
this.addDependency(resolve(dirname(this.resourcePath), rawModule.from))
})
callback(null, `module.exports = \`${importSchema(this.resourcePath).replace(/`/g, '\\`')}\``)
}