ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
132 stars 47 forks source link

Generated file is missing Types file import #152

Closed austinbiggs closed 6 months ago

austinbiggs commented 6 months ago

Before I dive in, thank you for creating this library. There have been a few times that I've really wanted a solution as elegant as this plugin (and I've nearly attempted it myself.) 😸

I'm experiencing an issue with the file generation when linking to an existing (generated) types file, the generated mock data file is missing an import statement. This results in broken types imports in the generated file.

A few thoughts I've had while debugging:

  1. Is this perhaps a misconfiguration on my end?
  2. Is this a bug?
  3. Is the plugin silently failing to find my file and as a result not importing the types file?

An example code generator config (using *.codegen.ts rather than .yml:

import { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  overwrite: true,
  schema: './schema/github/schema.graphql',
  documents: ['./operations/github/**/*.{gql,graphql}'],
  generates: {
    './clients/github/generated/apollo-helpers.ts': {
      plugins: [
        {
          add: {
            content: [
              '/* eslint-disable */',
              '// Auto-generated file: Do not alter.',
              '// @generated w/ gql:codegen:github',
            ],
          },
        },
        'typescript-apollo-client-helpers',
      ],
    },
    './clients/github/generated/types.ts': {
      plugins: [
        {
          add: {
            content: [
              '/* eslint-disable */',
              '// Auto-generated file: Do not alter.',
              '// @generated w/ gql:codegen:github',
            ],
          },
        },
        'typescript',
      ],
    },
    './clients/github/generated/introspection.ts': {
      plugins: [
        {
          add: {
            content: [
              '/* eslint-disable */',
              '// Auto-generated file: Do not alter.',
              '// @generated w/ gql:codegen:github',
            ],
          },
        },
        'fragment-matcher',
      ],
      config: {
        apolloClientVersion: 3,
      },
    },
    './clients/github/generated/factories.ts': {
      plugins: [
        {
          add: {
            content: [
              '/* eslint-disable */',
              '// Auto-generated file: Do not alter.',
              '// @generated w/ gql:codegen:github',
            ],
          },
        },
        {
          'typescript-mock-data': {
            typesFiles: './types.ts',
            listElementCount: 3,
          },
        },
      ],
    },

The output from running the code generator:

/* eslint-disable */
// Auto-generated file: Do not alter.
// @generated w/ gql:codegen:github

export const anAbortQueuedMigrationsInput = (
  overrides?: Partial<AbortQueuedMigrationsInput>
): AbortQueuedMigrationsInput => {
  return {
    clientMutationId:
      overrides && overrides.hasOwnProperty('clientMutationId')
        ? overrides.clientMutationId!
        : 'eum',
    ownerId:
      overrides && overrides.hasOwnProperty('ownerId')
        ? overrides.ownerId!
        : '7498729d-ca2d-40ba-be5a-99121f90750f',
  };
};

[... file contents truncated ...]
ardeois commented 6 months ago

@austinbiggs is the generated mock file missing ALL types import statements or only input types are missing?

austinbiggs commented 6 months ago

@austinbiggs is the generated mock file missing ALL types import statements or only input types are missing?

@ardeois There are no imports in the file at all. It appears that *Payload and *Input types are the missing imports.

austinbiggs commented 6 months ago

Resolved! 🎉

The issue was that I accidentally have an extra s in the TypesFile config key (typesFiles.)

Thinking out loud, a potential improvement that could benefit users would be logging unused/unrecognized config keys to the console. This would've helped me surface my user error sooner. 😸