Generic fields based on keyof class types #1465
-
@MichalLytek, not sure if this is something feasible, but does type-graphql allow the ability to generate let’s say enums or allowed string types based on properties of ObjectType class? TypeOrm has an interesting way in their FindOptions object that deals with checking if properties exist within a class. I’ll have to dig an example up later, but they can at runtime determine if a property is a valid key within an ENTITY type, so if you try to select, filter, or order by from a table with an invalid object key, they throw an error. the only way I can think of is making an enum object for every filtered query. I could also see this might clash with ObjectType properties who are those of field resolvers or essentially just relational fields to other tables in an ORM entity. E.g. Given an abstract filter, I want to be able to generate allowed values dynamically for ObjectTypes being queried. @ObjectType()
class Person {
@Field()
name: string:
}
function DynamicFilter<TItem>(TClassType: ClassType<TItem>) {
@InputType({ isAbstract: true })
abstract class Filter {
@Field() // Here I want to set a type that’s like a property of the TItem DTO
sortBy: keyof TItem;
}
} @E.g. the TypeORM way of getting said properties off an entity: const properties = Object.keys(
getConnection().getRepository(User).metadata.propertiesMap,
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The names of the properties of the object type fields exists in runtime but are hardly hardcoded in the metadata storage. Instead of making fancy solutions, just make an enum that matches properties of the object type and pass that as param to |
Beta Was this translation helpful? Give feedback.
The names of the properties of the object type fields exists in runtime but are hardly hardcoded in the metadata storage.
They are not exposed and not accessible for users.
Instead of making fancy solutions, just make an enum that matches properties of the object type and pass that as param to
DynamicFilter
- TS will do the rest of the checking of accessing those properties by name from enum.