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

Won't respect typesPrefix: "I" #135

Open berkaytheunicorn opened 1 year ago

berkaytheunicorn commented 1 year ago

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 commented 1 year ago

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,
  };
};
stale[bot] commented 1 year 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.

berkaytheunicorn commented 1 year ago

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

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