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.85k stars 1.33k forks source link

Combining typed-document-node with import-types #5670

Open dsebastien opened 3 years ago

dsebastien commented 3 years ago

Hello,

I've tried combining this plugin with the import-types preset, as follows, but couldn't get it to work. The generated typed-document-nodes.ts file does get the import, but doesn't use it.

Here's the codegen.yml config file:

# graphql-codegen configuration

# Always replace files
overwrite: true

# Schema file to use as input
schema: ./apps/writer-plan/server/graphql/generated/schema.graphql

# All GraphQL queries (could also be extracted from TS files!)
documents: ./apps/writer-plan/client/graphql/queries/*.graphql

# Output to generate
generates:
  ./apps/writer-plan/client/graphql/generated/graphql.client.types.ts:
    plugins:
      - typescript
      - typescript-operations
    config:
      immutableTypes: true
  ./apps/writer-plan/client/graphql/generated/typed-document-nodes.ts:
    preset: import-types
    presetConfig:
      typesPath: ./graphql.client.types
    # https://graphql-code-generator.com/docs/plugins/typescript-document-nodes
    plugins:
      - typed-document-node
hooks:
  afterAllFileWrite:
    - prettier --write

And the generated TS file:

import * as Types from './graphql.client.types';

import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
import gql from 'graphql-tag';

export const PingDocument: DocumentNode<PingQuery, PingQueryVariables> = gql`
  query ping {
    ping {
      message
      timestamp
    }
  }
`;

As you can see, it does import the types, but doesn't use them below.

Am I using this incorrectly?

dotansimha commented 3 years ago

Thank you for reporting it @dsebastien . At the moment it's not possible. I think adding support shouldn't be too complicated. PRs are welcome!