Skip to content

Commit c4df74d

Browse files
committed
Change ProducibleType to manually create a runtime list of members
This enum value needs to be lazily referenced, so the actual enum defers creation until after files have had a chance to run their side effects to add their members.
1 parent 244cae3 commit c4df74d

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

src/components/ethno-art/dto/ethno-art.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import {
99
} from '~/common';
1010
import { e } from '~/core/gel';
1111
import { RegisterResource } from '~/core/resources';
12-
import { Producible } from '../../product/dto/producible.dto';
12+
import {
13+
Producible,
14+
ProducibleTypeEntries,
15+
} from '../../product/dto/producible.dto';
1316

17+
ProducibleTypeEntries.add('EthnoArt');
1418
declare module '../../product/dto/producible.dto' {
1519
interface ProducibleTypeEntries {
1620
EthnoArt: true;

src/components/film/dto/film.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import {
99
} from '~/common';
1010
import { e } from '~/core/gel';
1111
import { RegisterResource } from '~/core/resources';
12-
import { Producible } from '../../product/dto/producible.dto';
12+
import {
13+
Producible,
14+
ProducibleTypeEntries,
15+
} from '../../product/dto/producible.dto';
1316

17+
ProducibleTypeEntries.add('Film');
1418
declare module '../../product/dto/producible.dto' {
1519
interface ProducibleTypeEntries {
1620
Film: true;

src/components/product/dto/producible.dto.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { Field, InterfaceType, ObjectType } from '@nestjs/graphql';
1+
import {
2+
Field,
3+
InterfaceType,
4+
ObjectType,
5+
registerEnumType,
6+
} from '@nestjs/graphql';
7+
import { MadeEnum } from '@seedcompany/nest';
28
import { stripIndent } from 'common-tags';
39
import { keys as keysOf } from 'ts-transformer-keys';
410
import {
511
EnumType,
12+
lazyRef,
613
makeEnum,
714
Resource,
815
SecuredProperty,
@@ -39,15 +46,21 @@ export abstract class Producible extends Resource {
3946
SetChangeType<'scriptureReferences', readonly ScriptureRangeInput[]>;
4047
}
4148

42-
export type ProducibleType = EnumType<typeof ProducibleType>;
43-
export const ProducibleType = makeEnum({
44-
name: 'ProducibleType',
45-
values: keysOf<ProducibleTypeEntries>(),
46-
});
47-
4849
// Augment this with each implementation of Producible via declaration merging
4950
// eslint-disable-next-line @typescript-eslint/no-empty-interface
5051
export interface ProducibleTypeEntries {}
52+
export const ProducibleTypeEntries = new Set<string>();
53+
54+
export type ProducibleType = EnumType<typeof ProducibleType>;
55+
export const ProducibleType = lazyRef(
56+
(): MadeEnum<keyof ProducibleTypeEntries> =>
57+
(realProducibleType ??= makeEnum({
58+
values: ProducibleTypeEntries as Iterable<keyof ProducibleTypeEntries>,
59+
})),
60+
);
61+
let realProducibleType: MadeEnum<keyof ProducibleTypeEntries> | undefined;
62+
// Register proxy eagerly to GQL schema
63+
registerEnumType(ProducibleType, { name: 'ProducibleType' });
5164

5265
export type ProducibleRef = UnsecuredDto<Producible> & {
5366
__typename: ProducibleType;

src/components/product/dto/product.dto.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import {
2727
SecuredScriptureRangesOverride,
2828
SecuredUnspecifiedScripturePortion,
2929
} from '../../scripture/dto';
30-
import { Producible, ProducibleRef, SecuredProducible } from './producible.dto';
30+
import {
31+
Producible,
32+
ProducibleRef,
33+
ProducibleTypeEntries,
34+
SecuredProducible,
35+
} from './producible.dto';
3136
import { SecuredProductMediums } from './product-medium.enum';
3237
import { SecuredMethodology } from './product-methodology.enum';
3338
import { SecuredProductPurposes } from './product-purpose.enum';
@@ -261,6 +266,9 @@ export const asProductType =
261266
return product as any;
262267
};
263268

269+
ProducibleTypeEntries.add('DirectScriptureProduct');
270+
ProducibleTypeEntries.add('DerivativeScriptureProduct');
271+
ProducibleTypeEntries.add('OtherProduct');
264272
declare module '../dto/producible.dto' {
265273
interface ProducibleTypeEntries {
266274
DirectScriptureProduct: true;

src/components/story/dto/story.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
} from '~/common';
1010
import { e } from '~/core/gel';
1111
import { RegisterResource } from '~/core/resources';
12-
import { Producible } from '../../product/dto';
12+
import { Producible, ProducibleTypeEntries } from '../../product/dto';
1313

14+
ProducibleTypeEntries.add('Story');
1415
declare module '../../product/dto/producible.dto' {
1516
interface ProducibleTypeEntries {
1617
Story: true;

0 commit comments

Comments
 (0)