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 46 forks source link

defaultNullableToNull should use null for custom Scalars #155

Closed tjmgregory closed 1 month ago

tjmgregory commented 5 months ago

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,
  }
}
ardeois commented 5 months ago

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
tjmgregory commented 5 months ago

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 🤔

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