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

fieldGeneration missing quotes in generated mocks #113

Closed toyflish closed 1 year ago

toyflish commented 1 year ago

My email default values configured as static strings get translated to mock file without quotes.

versions:

"@graphql-codegen/cli": "3.0.0",
"@graphql-codegen/client-preset": "2.0.0",
"graphql-codegen-typescript-mock-data": "3.2.2",

codegen.yaml

overwrite: true
schema: ../../backend/internal/api/graph/
documents: 'src/flexify.queries.ts'
generates:
  src/gql_flexify/:
    preset: client
    presetConfig:
      fragmentMasking: false
  src/gql_flexify/mocks.ts:
    plugins:
      - typescript-mock-data:
          typesFile: '../../src/gql_flexify/graphql.ts'
          addTypename: true
          fieldGeneration:
            _all:
              email: 'customer@test.net'

mock.ts

export const aGoogleNotificationSettings = (overrides?: Partial<GoogleNotificationSettings>): { __typename: 'GoogleNotificationSettings' } & GoogleNotificationSettings => {
    return {
        __typename: 'GoogleNotificationSettings',
        email: overrides && overrides.hasOwnProperty('email') ? overrides.email! : customer@test.net,
        feedCreatedErrorAlert: overrides && overrides.hasOwnProperty('feedCreatedErrorAlert') ? overrides.feedCreatedErrorAlert! : false,
        planLimitExceededAlert: overrides && overrides.hasOwnProperty('planLimitExceededAlert') ? overrides.planLimitExceededAlert! : false,
        weeklyStatus: overrides && overrides.hasOwnProperty('weeklyStatus') ? overrides.weeklyStatus! : true,
    };
};

Line 4 has customer@test.net in the correct place but missing the quotation marks.

ardeois commented 1 year ago

I think this is because yaml parsing takes the single quotes out In order to have double or single quotes, did you try to escape them like this:

email: \'customer@test.net\'

EDIT: according to this doc this should work:

email: "'customer@test.net'"

or 

email: "\"customer@test.net\""

or

email: ' ''customer@test.net'' '

Or use the > character https://stackoverflow.com/a/37432679

toyflish commented 1 year ago

Thanks for your help, it was caused by my yaml config, wrapping in double quotes works fine.