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
134 stars 47 forks source link

Types are not imported in the generated mocks #15

Closed mdazy closed 4 years ago

mdazy commented 4 years ago

Hello,

the generated code appears to use the generated types without ever importing them first. As a result, as soon as I try to use one of the mock functions, I'm getting errors for each generated mock:

    schema/lib/mocks.tsx:214:55 - error TS2304: Cannot find name 'Status'.
    214 export const aStatus = (overrides?: Partial<Status>): Status => {
                                                    ~~~~~~

My codegen config is like this:

schema: ./schema/lib/schema.json
documents: ./schema/*.graphql
generates:
  ./schema/lib/clientTypes.tsx:
    plugins:
      - add: /* tslint:disable */
      - typescript
      - typescript-operations
      - typescript-react-apollo
    config:
      withHooks: true
      withHOC: false
      withComponent: false
  ./schema/lib/mocks.tsx:
    plugins:
      - graphql-codegen-typescript-mock-data:
        typesFile: ./clientTypes.tsx

I'm pretty sure I must be missing something very basic but I just can't figure out what.

Thanks,

ardeois commented 4 years ago

Hi @mdazy

I think you are missing an indentation when you declare typesFile It should be:

    plugins:
      - typescript-mock-data:
            typesFile: ./clientTypes.tsx
mdazy commented 4 years ago

You're right. I just hit tab and thought it was indented correctly but I didn't realize the parameter was in fact aligned with the hyphen level. It does work with the correct indentation. Thanks!

TomasBarry commented 1 year ago

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

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