dotansimha / graphql-code-generator-community

MIT License
119 stars 156 forks source link

"typescript-graphql-request" returns import error #465

Open florisdegraaff opened 1 year ago

florisdegraaff commented 1 year ago

Which packages are impacted by your issue?

No response

Describe the bug

When using the typescript-graphql-request plugin I get an import error. Running the code does work, but building the code gives an error. The error will be on the second line, being: import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types';

Your Example Website or App

https://github.com/florisdegraaff/portfolio

Steps to Reproduce the Bug or Issue

  1. Install latest versions of:

    • @graphql-codegen/cli
    • @graphql-codegen/fragment-matcher
    • @graphql-codegen/typescript
    • @graphql-codegen/typescript-graphql-request
    • @graphql-codegen/typescript-operations
  2. Setup codegen.ts with:

    generates: {
    'src/generated.ts': {
      plugins: [
        {
          add: {
            content: '/* eslint-disable */'
          }
        },
        'typescript',
        'typescript-operations',
        'typescript-graphql-request',
        'fragment-matcher'
      ],
    }
    }
  3. Add .graphql files that follow the pattern in de documents setting of your codegen.ts

  4. Run graphql-codegen --config codegen.ts

Expected behavior

The import of the GraphQLClientRequestHeaders in the generated file should not return an error

Screenshots or Videos

No response

Platform

Codegen Config File

schema: [{
    'https://gapi.storyblok.com/v1/api': {
      headers: {
        Token: process.env.TOKEN,
      }
    },
  }],
  documents: 'src/storyblok/graphql',
  generates: {
    'src/storyblok/generated.ts': {
      plugins: [
        'typescript',
        'typescript-operations',
        'typescript-graphql-request',
        'fragment-matcher'
      ],
    }
  }

Additional context

No response

e965 commented 1 year ago

Same problem.

eth3lbert commented 1 year ago

Related to https://github.com/jasonkuhrt/graphql-request/issues/566

QuestionAndAnswer commented 1 year ago

Same problem when importing with esm modules. Seems like because GraphQLClientRequestHeaders isn't exposed in graphql-request.

QuestionAndAnswer commented 1 year ago

Fixed with this:

hooks: {
        afterOneFileWrite: [
          `sed -i '' -e '1s;^.*;import type { GraphQLClient, RequestOptions } from \"graphql-request\"\\;;' myFile.ts`,
          `sed -i '' -e '2s;^.*;export type GraphQLClientRequestHeaders = RequestOptions[\"requestHeaders\"]\\;;' myFile.ts`,
        ],
},

Extracted the needed type indirectly from RequestOptions