import mongoose from 'mongoose' ; import { composeWithMongooseDiscriminators } from 'graphql-compose-mongoose' ; import { ObjectTypeComposer } from 'graphql-compose' ; import { schemaComposer , TContext } from './schemaComposer' ; const DKey = 'type' ; const enumCharacterType = { PERSON : 'Person' , DROID : 'Droid' , } ; const CharacterSchema = new mongoose . Schema ( { type : { type : String , require : true , enum : Object . keys ( enumCharacterType ) , description : 'Character type Droid or Person' , } , name : String , height : Number , mass : Number , films : [ String ] , } , { collection : 'md_characters' , } ) ; const DroidSchema = new mongoose . Schema ( { makeDate : String , primaryFunction : [ String ] , } ) ; const PersonSchema = new mongoose . Schema ( { gender : String , hairColor : String , starships : [ String ] , } ) ; CharacterSchema . set ( 'discriminatorKey' , DKey ) ; export const CharacterModel = mongoose . model ( 'Character' , CharacterSchema ) ; export const DroidModel = CharacterModel . discriminator ( enumCharacterType . DROID , DroidSchema ) ; export const PersonModel = CharacterModel . discriminator ( enumCharacterType . PERSON , PersonSchema ) ; export const CharacterDTC = composeWithMongooseDiscriminators < any , TContext > ( CharacterModel , { schemaComposer , } ) ; type DroidT = any ; export const DroidTC = CharacterDTC . discriminator < DroidT > ( DroidModel ) ; type PersonT = any ; export const PersonTC = CharacterDTC . discriminator < PersonT > ( PersonModel ) ;