-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enums): adds enum support (#15)
- Loading branch information
1 parent
8179135
commit 7ec77b7
Showing
19 changed files
with
280 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import 'reflect-metadata'; | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
import { getApp } from './app'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { IntrospectionSchema, IntrospectionEnumType } from 'graphql'; | ||
import { ObjectType, Query, Resolver } from 'type-graphql'; | ||
|
||
import { getSchemaInfo } from '../schema'; | ||
|
||
import { EnumField } from './EnumField'; | ||
|
||
describe('Enums', () => { | ||
let schemaIntrospection: IntrospectionSchema; | ||
|
||
beforeAll(async () => { | ||
enum StringEnum { | ||
Foo = 'FOO', | ||
Bar = 'BAR' | ||
} | ||
|
||
@ObjectType() | ||
class StringEnumInput { | ||
@EnumField('StringEnum', StringEnum, { nullable: true }) | ||
stringEnumField?: StringEnum; | ||
} | ||
|
||
@Resolver(of => StringEnumInput) | ||
class SampleResolver { | ||
@Query(returns => StringEnum) | ||
getStringEnumValue(): StringEnum { | ||
return StringEnum.Foo; | ||
} | ||
} | ||
|
||
const schemaInfo = await getSchemaInfo({ | ||
resolvers: [SampleResolver] | ||
}); | ||
schemaIntrospection = schemaInfo.schemaIntrospection; | ||
}); | ||
|
||
describe('EnumField', () => { | ||
it('Puts an enum in the GraphQL schema', async () => { | ||
const myEnum = schemaIntrospection.types.find((type: any) => { | ||
return type.kind === 'ENUM' && type.name === 'StringEnum'; | ||
}) as IntrospectionEnumType; | ||
|
||
expect(myEnum).toBeDefined(); | ||
expect(myEnum.enumValues.length).toEqual(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const caller = require('caller'); | ||
import { Field, registerEnumType } from 'type-graphql'; | ||
import { Column } from 'typeorm'; | ||
|
||
import { getMetadataStorage } from '../metadata'; | ||
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; | ||
|
||
interface EnumFieldOptions { | ||
nullable?: boolean; | ||
} | ||
|
||
export function EnumField(name: string, enumeration: object, options: EnumFieldOptions = {}): any { | ||
// Register enum with TypeGraphQL so that it lands in generated schema | ||
registerEnumType(enumeration, { name }); | ||
|
||
// In order to use the enums in the generated classes file, we need to | ||
// save their locations and import them in the generated file | ||
const entityFileName = caller(); | ||
|
||
const registerEnumWithWarthog = (target: any, propertyKey: string, descriptor: PropertyDescriptor): any => { | ||
getMetadataStorage().addEnum(target.constructor.name, propertyKey, name, enumeration, entityFileName); | ||
}; | ||
|
||
const factories = [ | ||
Field(type => enumeration, options), | ||
Column({ enum: enumeration, ...options }) as MethodDecoratorFactory, | ||
registerEnumWithWarthog | ||
]; | ||
|
||
return composeMethodDecorators(...factories); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './EmailField'; | ||
export * from './EnumField'; | ||
export * from './Model'; | ||
export * from './StringField'; | ||
export * from './ForeignKeyField'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getMetadataStorage } from './metadata-storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function getMetadataStorage(): MetadataStorage { | ||
if (!(global as any).WarthogMetadataStorage) { | ||
(global as any).WarthogMetadataStorage = new MetadataStorage(); | ||
} | ||
return (global as any).WarthogMetadataStorage; | ||
} | ||
|
||
export class MetadataStorage { | ||
enumMap: { [table: string]: { [column: string]: any } } = {}; | ||
|
||
addEnum(tableName: string, columnName: string, enumName: string, enumValues: any, filename: string) { | ||
this.enumMap[tableName] = this.enumMap[tableName] || {}; | ||
this.enumMap[tableName][columnName] = { | ||
name: enumName, | ||
enumeration: enumValues, | ||
filename | ||
}; | ||
} | ||
|
||
getEnum(tableName: string, columnName: string) { | ||
if (!this.enumMap[tableName]) { | ||
return undefined; | ||
} | ||
return this.enumMap[tableName][columnName] || undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.