Graphql-code-generator: importDocumentNodeExternallyFrom generates incorrect import path

Created on 7 Nov 2019  路  1Comment  路  Source: dotansimha/graphql-code-generator

Describe the bug
When using importDocumentNodeExternallyFrom: 'near-operation-file' in combination with documentMode: 'external' import paths are not correctly generated if the near-operation-file specifies a custom folder

To Reproduce

Use a config like the following

overwrite: true
schema: 'schema.graphql'
documents: './graphql/**/*.gql.ts'
generates:
  /:
    preset: near-operation-file
    presetConfig:
      extension: .generated.tsx
      baseTypesPath: types.ts
      folder: generated
      cwd: '/'
    plugins:
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      documentMode: 'external'
      importDocumentNodeExternallyFrom: 'near-operation-file'
      withHooks: true
      withHOC: false
      reactApolloVersion: 3

It looks like it is hard-coded to import from the same folder here:
https://github.com/dotansimha/graphql-code-generator/blob/cc8ca46b120bd30620cee9d2897c93fa4db51c1a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts#L306

bug plugins

Most helpful comment

For anyone else running into this, we are working around it with find and sed as follows`

Note this only works if your Operations files are one folder above your generated folder

QUERIES_GENERATED_FOLDER=./graphql/queries/generated
MUTATIONS_GENERATED_FOLDER=./graphql/mutations/generated

find $QUERIES_GENERATED_FOLDER -type f -name "*.generated.tsx" -exec sed -i '' "s|import \* as Operations from './|import * as Operations from \'../|" {} \;
find MUTATIONS_GENERATED_FOLDER -type f -name "*.generated.tsx" -exec sed -i '' "s|import \* as Operations from './|import * as Operations from \'../|" {} \;

>All comments

For anyone else running into this, we are working around it with find and sed as follows`

Note this only works if your Operations files are one folder above your generated folder

QUERIES_GENERATED_FOLDER=./graphql/queries/generated
MUTATIONS_GENERATED_FOLDER=./graphql/mutations/generated

find $QUERIES_GENERATED_FOLDER -type f -name "*.generated.tsx" -exec sed -i '' "s|import \* as Operations from './|import * as Operations from \'../|" {} \;
find MUTATIONS_GENERATED_FOLDER -type f -name "*.generated.tsx" -exec sed -i '' "s|import \* as Operations from './|import * as Operations from \'../|" {} \;
Was this page helpful?
0 / 5 - 0 ratings