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 Mock File Doesn't Import Types? #74

Closed CowDotDev closed 2 years ago

CowDotDev commented 2 years ago

My apologies if I am just missing something dumb - but I have implemented this typescript-mock-data plugin and am seeing the functions generated successfully. However, the generated-mocks.ts files does not import any of the types from the generated-types.ts file so TypeScript compilation fails on this file.

What do I need to do in order to have the generated-mock.ts file reference the types graphql-codegen generates?

Thank you in advanced!

codegen.yml

overwrite: true
schema: 'schema.graphql'
documents: 'src/graphql/operations/**/*.ts'
generates:
  src/graphql/generated.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      withHooks: true
  src/mocks/generated-mocks.ts:
    plugins:
      - typescript-mock-data:
        typesFile: 'src/graphql/generated.ts'
        enumValues: upper-case#upperCase
        typenames: keep
        scalars:
          ConnectionCursor:
            generator: word
          DateTime:
            generator: date
            arguments: 'YYYY-MM-DDTHH:mm:ssZ'
          JSON:
            # TODO: This shouldn't be currency, but typescript-mock-data plug-in does not support custom casual generators, yet.
            # TODO: Possibly look into extending typescript-mock-data to accept custom casual generators
            # https://github.com/boo1ean/casual#define-custom-generators
            generator: currency
          JSONObject:
            # TODO: This shouldn't be currency, but typescript-mock-data plug-in does not support custom casual generators, yet.
            # TODO: Possibly look into extending typescript-mock-data to accept custom casual generators
            # https://github.com/boo1ean/casual#define-custom-generators
            generator: currency
  ./graphql.schema.json:
    plugins:
      - 'introspection'
ardeois commented 2 years ago

@CowDotDev typesFile config should do this for you

Try to set it to: typesFile: './generated.ts' instead. This config will be used to add imports types from ./generated.ts

Hope that helps!

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

TomasBarry commented 1 year ago

For anyone coming here having the same issue with codgen.ts, you need to put typesFile within config:

'path/to/mocks.ts': {
  plugins: [
    'typescript-mock-data',
  ],
  config: {
    typesFile: './index.ts'
  }
}

https://github.com/ardeois/graphql-codegen-typescript-mock-data/issues/15#issuecomment-1351649503

CowDotDev commented 1 year ago

@ardeois Thank you for your quick reply - I am sorry I missed it until now. Thank you and @TomasBarry I will take a look at this again once we come back around to it, definitely would still be a huge help!