graphql-codegen-typescript-mock-data icon indicating copy to clipboard operation
graphql-codegen-typescript-mock-data copied to clipboard

defaultNullableToNull should use null for custom Scalars

Open tjmgregory opened this issue 10 months ago • 2 comments

The newly implemented defaultNullableToNull option should take precedence over custom Scalar definitions as you cannot define those optionally and are global.

As implemented in https://github.com/ardeois/graphql-codegen-typescript-mock-data/pull/130 it makes sense for specific field configuration to take precedence over defaultNullableToNull.

Precedence of options should be fieldGeneration > defaultNullableToNull > scalars.

If folks agree I'm happy to give this a crack in the coming days.

Example GQL Schema

type Hat {
  id: ID!
  description?: String
}

Example config

  src/__generated__/test-fixtures.gql.ts:
    plugins:
      - add:
          content: "import { faker } from '@faker-js/faker';"
      - typescript-mock-data:
          typesFile: ./base.gql.ts
          defaultNullableToNull: true
          scalars:
            ID: faker.string.uuid()
            String: faker.word.words()
            Boolean: faker.datatype.boolean()

Current output

export const aHat = (overrides?: Partial<Hat>): Hat => {
  return {
    id: overrides && overrides.hasOwnProperty('id') ? overrides.apiKeyName! : faker.string.uuid(),
    description: overrides && overrides.hasOwnProperty('description') ? overrides.apiKeyName! : faker.word.words(),
  }
}

Expected output

export const aHat = (overrides?: Partial<Hat>): Hat => {
  return {
    id: overrides && overrides.hasOwnProperty('id') ? overrides.apiKeyName! : faker.string.uuid(),
    description: overrides && overrides.hasOwnProperty('description') ? overrides.apiKeyName! : null,
  }
}

tjmgregory avatar Apr 11 '24 14:04 tjmgregory

Thanks for the suggestion @tjmgregory I agree defaultNullableToNull should take precedence over scalars Feel free to open a PR

A little side note, you don't need to add your own faker import and generators, it's builtin by the plugin, you just need to change the default generator:

  src/__generated__/test-fixtures.gql.ts:
    plugins:
      - typescript-mock-data:
          typesFile: ./base.gql.ts
          defaultNullableToNull: true
          generateLibrary: faker
          scalars:
            ID: string.uuid
            String: word.words
            Boolean: datatype.boolean

ardeois avatar Apr 11 '24 14:04 ardeois

Feel free to open a PR

Great stuff, thanks - will set myself a reminder.

A little side note, you don't need to add your own faker import and generators, it's builtin by the plugin, you just need to change the default generator:

Yeh I had seen this was suggested in the spec but was having trouble as faker wasn't being imported and they weren't being translated, i.e. string.uuid -> faker.string.uuid() wasn't happening, just string.uuid was placed in the file 🤔

tjmgregory avatar Apr 11 '24 15:04 tjmgregory

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] avatar Jul 12 '24 12:07 stale[bot]