Skip to content

OneOf causes invalid functions #178

@davidr64

Description

@davidr64

Using this plugin along with a types file generated by typescript typescript-operations on a schema that includes use of @OneOf directive causes an invalid object.

A oneof input generates an object like

/** One-Of attributes for quote content */
export type OneOfInput =
  { a: AInputType; b?: never; c?: never; d?: never; e?: never; }
  |  
  { a?: never; b: BInputType; c?: never; d?: never; e?: never; }
  | 
  { a?: never; b?: never; c: CInputType; d?: never; e?: never; }
  |  
  { a?: never; b?: never; c?: never; d: DInputType; e?: never; }
  |  
  { a?: never; b?: never; c?: never; d?: never; e: EInputType; };

The corresponding mock generation looks like

export const anOneOfInput = (overrides?: Partial<OneOfInput>, _relationshipsToOmit: Set<string> = new Set()): OneOfInput => {
    const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
    relationshipsToOmit.add('OneOfInput');
    return {
        a: overrides && overrides.hasOwnProperty('a') ? overrides.a! : relationshipsToOmit.has('AInputType') ? {} as AInputType : anAInputType({}, relationshipsToOmit),
        b: overrides && overrides.hasOwnProperty('b') ? overrides.b! : relationshipsToOmit.has('BInputType') ? {} as BInputType : aBInputType({}, relationshipsToOmit),
        c: overrides && overrides.hasOwnProperty('c') ? overrides.c! : relationshipsToOmit.has('CInputType') ? {} as CInputType : aCInputType({}, relationshipsToOmit),
        d: overrides && overrides.hasOwnProperty('d') ? overrides.d! : relationshipsToOmit.has('DInputType') ? {} as DInputType : aDInputType({}, relationshipsToOmit),
        e: overrides && overrides.hasOwnProperty('e') ? overrides.e! : relationshipsToOmit.has('EInputType') ? {} as EInputType : anEInputType({}, relationshipsToOmit),
    };
};

Typescript compile then fails because the object being returned could have multiple of the fields but the type must only have one field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions