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

Won't respect typesPrefix: "I"

Open berkaytheunicorn opened this issue 1 year ago • 5 comments

Hi, our settings file goes like this

overwrite: true
schema: "http://localhost:3000/graphql"
generates:
  constants/mocks/index.ts:
    plugins:
      - typescript-mock-data:
          typesFile: "../../types"
          enumValues: upper-case#upperCase
          typeNames: change-case-all#pascalCase
          typesPrefix: "I"
          prefix: "Mock"
          addTypename: true
          generateLibrary: faker
          terminateCircularRelationships: true
          scalars:
            Date:
              generator: date.past
              arguments: 10
            ID:
              generator: database.mongodbObjectId
          fieldGeneration:
            _all:
              email: internet.email
              bio: lorem.paragraph
              firstName: name.firstName
              lastName: name.lastName
              fullName: name.fullName
              title: company.name
            File:
              key: undefined
            FileURLs:
              original: image.abstract
            FileStream:
              url: undefined

and our types generator goes like this

overwrite: true
schema: "http://localhost:3000/graphql"
generates:
  types/index.ts:
    plugins:
      - typescript
      - typescript-operations
    config:
      declarationKind:
        type: "interface"
        input: "interface"
      typesPrefix: "I"
      enumPrefix: false
      enumsAsConst: true
      maybeValue: T
      namingConvention:
        enumValues: keep
  types/apollo.ts:
    plugins:
      - fragment-matcher
    config:
      apolloClientVersion: 3

This was working until today but today, generator generates like below;

emails:
      overrides && overrides.hasOwnProperty("emails")
        ? overrides.emails!
        : [
            relationshipsToOmit.has("Email")
              ? ({} as Email)
              : MockEmail({}, relationshipsToOmit),
          ],

but it should be

emails:
      overrides && overrides.hasOwnProperty("emails")
        ? overrides.emails!
        : [
            relationshipsToOmit.has("IEmail")
              ? ({} as IEmail)
              : MockEmail({}, relationshipsToOmit),
          ],

berkaytheunicorn avatar Jun 20 '23 13:06 berkaytheunicorn

btw this is correctly generated;

export const MockEmail = (
  overrides?: Partial<IEmail>,
  _relationshipsToOmit: Set<string> = new Set(),
): { __typename: "Email" } & IEmail => {
  const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
  relationshipsToOmit.add("Email");
  return {
    __typename: "Email",
    address:
      overrides && overrides.hasOwnProperty("address") ? overrides.address! : "quaerat",
    primary:
      overrides && overrides.hasOwnProperty("primary") ? overrides.primary! : true,
    verified:
      overrides && overrides.hasOwnProperty("verified") ? overrides.verified! : true,
  };
};

berkaytheunicorn avatar Jun 20 '23 13:06 berkaytheunicorn

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 20 '23 21:07 stale[bot]

If anyone interested, I'm using this workaround until this is fixed graphql-codegen --config ./codegen-mocks.yml && perl -pi -w -e 's/{} as /{} as I/g;' ./constants/mocks/index.ts && prettier --write ./constants/mocks

berkaytheunicorn avatar Jul 28 '23 18:07 berkaytheunicorn

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 Aug 29 '23 19:08 stale[bot]

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 Oct 11 '23 22:10 stale[bot]