graphql-import-loader icon indicating copy to clipboard operation
graphql-import-loader copied to clipboard

Error with relative paths inside sdl

Open peterpetre opened this issue 7 years ago • 6 comments

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?

peterpetre avatar Apr 18 '18 12:04 peterpetre

+1

JinHoSo avatar May 16 '18 03:05 JinHoSo

Maybe it's related to the graphql-import version that's being used?

"dependencies": {
    "graphql-import": "^0.4.5"
},

dsanch3z avatar Oct 05 '18 16:10 dsanch3z

+1

RoryKelly avatar Oct 10 '18 18:10 RoryKelly

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.

ThisIsMissEm avatar Oct 18 '18 14:10 ThisIsMissEm

(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).

ThisIsMissEm avatar Oct 18 '18 14:10 ThisIsMissEm

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, '\\`')}\``)
}

jgeschwendt avatar Apr 30 '19 17:04 jgeschwendt