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

terminateCircularRelationships does not work for interfaces if useImplementingTypes is enabled #140

Closed Swanoo closed 1 year ago

Swanoo commented 1 year ago

If both options are enabled (terminateCircularRelationships and useImplementingTypes), the fields where useImplementingTypes is in effect are not taking into account terminateCircularRelationships.

As an example, for the following schema:

type Query {
  getUser(id: String!): User
}

type User {
  id: String!
  events: [Event!]
}

interface Event {
  startDate: String
  endDate: String
  timeZone: String
}

type MeetingEvent implements Event {
  endDate: String
  startDate: String
  timeZone: String
  event: Event!
}

type OtherEvent implements Event {
  endDate: String
  startDate: String
  timeZone: String
  somethingElse: String!
}

Upon enabling both options, the library generates the following mocks:

export const aMeetingEvent = (overrides?: Partial<MeetingEvent>, _relationshipsToOmit: Set<string> = new Set()): { __typename: 'MeetingEvent' } & MeetingEvent => {
    const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
    relationshipsToOmit.add('MeetingEvent');
    return {
        __typename: 'MeetingEvent',
        endDate: overrides && overrides.hasOwnProperty('endDate') ? overrides.endDate! : faker.lorem.word(),
        event: overrides && overrides.hasOwnProperty('event') ? overrides.event! : aMeetingEvent() || anOtherEvent(),
        startDate: overrides && overrides.hasOwnProperty('startDate') ? overrides.startDate! : faker.lorem.word(),
        timeZone: overrides && overrides.hasOwnProperty('timeZone') ? overrides.timeZone! : faker.lorem.word(),
    };
};

export const aUser = (overrides?: Partial<User>, _relationshipsToOmit: Set<string> = new Set()): { __typename: 'User' } & User => {
    const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
    relationshipsToOmit.add('User');
    return {
        __typename: 'User',
        events: overrides && overrides.hasOwnProperty('events') ? overrides.events! : [aMeetingEvent() || anOtherEvent()],
        id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker.lorem.word(),
    };
};

As you can see, the fields where the Event type is used is not taking into account relationshipsToOmit, which means that the mock will fail because of the recursion.

If that helps, I created a repository reproducing this here: https://github.com/Swanoo/graphql-mock-error

NathanGostelowGlobal commented 1 year ago

we have the same problem affecting us

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.