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

Support for scalars having different input and output types #139

Closed sberlan-odeko closed 4 months ago

sberlan-odeko commented 11 months ago

Ran into a typing issue where scalars with a string serialization but resolved to a complex type does not appear to be supported in typescript-mock-data plugin. Specifically, the scalars field for this plugin does not support an { input, output } so that the correct generator could be applied (output for types, input for inputs). The scalars are handled with type resolvers in Apollo in the app.

Any plans on supporting this scalar configuration? If not, are you open to PRs?

Configuration: codegen.yml

schema: src/tmp-error/schema.graphql
generates:
  src/tmp-error/graphql.ts:
    plugins:
      - add: { content: "import type Decimal from 'decimal.js';" }
      - typescript
    config:
      scalars:
        Decimal:
          input: string
          output: Decimal
  src/tmp-error/mocks.ts:
    plugins:
      - add:
          { content: "/* eslint-disable no-prototype-builtins */\nimport * as generators from './mocks-generators';" }
      - typescript-mock-data
    config:
      prefix: make
      scalars:
        Decimal: generators.decimal()
      typesFile: ./graphql.ts

Schema: src/tmp-error/schema.graphql:

scalar Decimal
input TestInput {
  field: Decimal!
}
type Test {
  field: Decimal!
}

Generated src/tmp-error/mocks.ts with errors:

/* eslint-disable no-prototype-builtins */
import * as generators from './mocks-generators';
import { Test, TestInput } from './graphql';

// Type of generators.decimal: () => Decimal

// type Test = {
//   __typename?: 'Test';
//   field: Scalars['Decimal']['output'];
// }
export const makeTest = (overrides?: Partial<Test>): Test => {
    return {
        field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : generators.decimal(),
    };
};

// type TestInput = {
//   field: Scalars['Decimal']['input'];
// }
export const makeTestInput = (overrides?: Partial<TestInput>): TestInput => {
    return {
        // Error: Type 'string | Decimal' is not assignable to type 'string'.
        // Type 'Decimal' is not assignable to type 'string'. ts(2322)
        field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : generators.decimal(),
    };
};
stale[bot] commented 10 months 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.

ardeois commented 10 months ago

Thanks for reporting this @sberlan-odeko , I didn't know input and output existed. We're definitely open for PRs if you're willing to give it a try!

stale[bot] commented 9 months 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.

stale[bot] commented 5 months 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.