Skip to content

Commit

Permalink
Merge branch 'mattleff-deprecated-enum-values'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 10, 2020
2 parents cd2c727 + 316eece commit 9ae49f6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/schema-builder/factories/enum-definition.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export class EnumDefinitionFactory {
name: metadata.name,
description: metadata.description,
values: Object.keys(enumValues).reduce((prevValue, key) => {
const valueMap = metadata.valuesMap[key];
prevValue[key] = {
value: enumValues[key],
description: valueMap?.description,
deprecationReason: valueMap?.deprecationReason,
};
return prevValue;
}, {}),
Expand All @@ -28,7 +31,7 @@ export class EnumDefinitionFactory {
}

private getEnumValues(enumObject: Record<string, any>) {
const enumKeys = Object.keys(enumObject).filter(key =>
const enumKeys = Object.keys(enumObject).filter((key) =>
isNaN(parseInt(key, 10)),
);
return enumKeys.reduce((prev, nextKey) => {
Expand Down
14 changes: 12 additions & 2 deletions lib/schema-builder/metadata/enum.metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
export interface EnumMetadata {
ref: object;
export interface EnumMetadataValuesMapOptions {
deprecationReason?: string;
description?: string;
}

export type EnumMetadataValuesMap<T extends object> = Partial<
Record<keyof T, EnumMetadataValuesMapOptions>
>;

export interface EnumMetadata<T extends object = any> {
ref: T;
name: string;
description?: string;
valuesMap?: EnumMetadataValuesMap<T>;
}
10 changes: 8 additions & 2 deletions lib/type-factories/register-enum-type.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* To avoid numerous breaking changes, the public API is backward-compatible and may resemble "type-graphql".
*/

import { EnumMetadataValuesMap } from '../schema-builder/metadata';
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage';
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage';

/**
* Interface defining options that can be passed to `registerEnumType` function.
*/
export interface EnumOptions {
export interface EnumOptions<T extends object = any> {
/**
* Name of the enum.
*/
Expand All @@ -20,6 +21,10 @@ export interface EnumOptions {
* Description of the enum.
*/
description?: string;
/**
* A map of options for the values of the enum.
*/
valuesMap?: EnumMetadataValuesMap<T>;
}

/**
Expand All @@ -28,13 +33,14 @@ export interface EnumOptions {
*/
export function registerEnumType<T extends object = any>(
enumRef: T,
options: EnumOptions,
options: EnumOptions<T>,
) {
LazyMetadataStorage.store(() =>
TypeMetadataStorage.addEnumMetadata({
ref: enumRef,
name: options.name,
description: options.description,
valuesMap: options.valuesMap || {},
}),
);
}
9 changes: 9 additions & 0 deletions tests/code-first/enums/direction.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ export enum Direction {
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT',
Sideways = 'SIDEWAYS',
}

registerEnumType(Direction, {
name: 'Direction', // this one is mandatory
description: 'The basic directions', // this one is optional
valuesMap: {
Sideways: {
deprecationReason: 'Replaced with Left or Right',
},
Up: {
description: 'The primary direction',
},
},
});
8 changes: 7 additions & 1 deletion tests/e2e/code-first-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Code-first - schema factory', () => {
enumValues: [
{
deprecationReason: null,
description: null,
description: 'The primary direction',
isDeprecated: false,
name: 'Up',
},
Expand All @@ -141,6 +141,12 @@ describe('Code-first - schema factory', () => {
isDeprecated: false,
name: 'Right',
},
{
deprecationReason: 'Replaced with Left or Right',
description: null,
isDeprecated: true,
name: 'Sideways',
},
],
}),
);
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/printed-schema.snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ type Query {
"""The basic directions"""
enum Direction {
"""The primary direction"""
Up
Down
Left
Right
Sideways @deprecated(reason: "Replaced with Left or Right")
}
"""Search result description"""
Expand Down Expand Up @@ -118,6 +120,9 @@ enum Direction {
Down
Left
Right
Sideways @deprecated(reason: "Replaced with Left or Right")
"""The primary direction"""
Up
}
Expand Down

0 comments on commit 9ae49f6

Please sign in to comment.