dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

importDocumentNodeExternallyFrom generates incorrect import path #2913

Open willtrking opened 5 years ago

willtrking commented 5 years ago

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

willtrking commented 5 years ago

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 \'../|" {} \;
ha-akelius commented 3 years ago

this is an old bug, why it is not solved until now?

Urigo commented 3 years ago

Hi everyone and thank you for the reports

Sorry but I'm not adding a lot here but just labeling it according to our new Contribution Guide and issue flow.

It seems like we are on stage 0.

Now in order to advance to stage 1 we need a reproduction in a repo or codesandbox. Thanks for the great amount of details you provided on the issue, creating the reproduction shouldn't be too hard.

So the biggest benefit would be to advance to stage 2. In order to get there we'll need a failing test, would be great if someone could help progress the issues through the stages.

Thank you and sorry that this comment is not a complete solution (yet).