Open KCH0000 opened 3 months ago
Could you not just create resolver etc for ContactDTO and add a @ResolveField
for value
that returns one of the two?
We do something similiar for the Creator
type, which is just a UserEntity
in the database:
import { Parent, ResolveField, Resolver } from '@nestjs/graphql'
import { UserType } from '@constants/user'
import { UserEntity } from '@database/main/user.entity'
import { Creator, CreatorType } from './creator.type'
@Resolver(() => Creator)
export class CreatorResolver {
@ResolveField()
public name(@Parent() creator: UserEntity): string {
if (this.isSpecialUser(creator)) {
return creator.fullName
}
return creator.firstName
}
@ResolveField()
public type(@Parent() creator: UserEntity): string {
if (this.isSpecialUser(creator)) {
switch (creator.type) {
case UserType.API:
return CreatorType.API
case UserType.System:
return CreatorType.System
case UserType.Integration:
return CreatorType.Integration
}
}
return CreatorType.User
}
private isSpecialUser(creator: UserEntity) {
return [UserType.API, UserType.Integration, UserType.System].includes(creator.type as UserType)
}
}
This is not exactly the pattern. When we use ChildEntity
, we create our own Entity and DTO for each Child.
Resolver
should convert ChildEntity
to ContactDTO
based on the type. ContactEntity
is abstract.
I wrote such a Resolver, but automatic filtering in the scheme does not work on it
User {
name: String!
contacts: [ContactDTO]
}
I realized that this is not an easy solution, so for now I keep all types of contacts in one column and additionally verify before saving
📚 Documentation
We need an example in the documentation of how to work with inherited entities.
There are two Entities in the same table. How do I make
FilterableUnpagedRelation
with a common DTO?I need to transform
PhoneContactEntity
EmailContactEntity
toContactDTO
where value is either a phone number or an email.I'm trying to do it through
assemblers
, but how use two assemblers for one DTO Or choose a way to store data in different tables?