Types for relation fields
Closed this issue · 3 comments
First off, thanks for the extension. Immensely useful already in its current state.
If you do spend time improving this, I think it would be marvelous to get options to specify the depth. I.e. a relational field, can have the collection item as a value.
I think it would be marvelous to get options to specify the depth. I.e. a relational field, can have the collection item as a value.
Sounds like a great idea. Should it be a option though? I would just always include the relations.
Just so that I understand correctly a small example of what I think you mean. If we have a collection "Post" and a collection "Translated Post" that provides translations for the posts, then this would be the types that you would expect:
export type Post = {
id: number;
// ...
};
export type TranslatedPost = {
id: number;
// ...
post_id: number;
post?: Post;
};
Is that right?
Just so that I understand correctly a small example of what I think you mean. If we have a collection "Post" and a collection "Translated Post" that provides translations for the posts, then this would be the types that you would expect:
export type Post = { id: number; // ... }; export type TranslatedPost = { id: number; // ... post_id: number; post?: Post; };Is that right?
Hey! Thanks for the reply!
Unfortunately, I think that wouldn't be really correct. Directus returns relational items in a nested array of objects.
Depending on how relational fields are queried, you get different information back from the API.
E.g. a file field that is queried just with its name gets only the id, but if you query it like this: file.*
, you get the full file object
Same goes for a relation, and I think that is really tricky to work with in the SDK right now while using typescript.
E.g. I have artistst that play at events
export type Artists = {
id: number;
// ...
events: ArtistsEvents[]
export type ArtistsEvents = {
id: number,
events_id: number | Event // Depending on query depth
artists_id: number | Artist // Depending on query depth
Unfortunately, I have no idea how to map this well so the SDK (or API responses) make sense depending on the depth of the query.
I can try to give more elaborate examples with some code snippets, but not today