Gustavo
08/23/2022, 9:58 AMargs
seems to be an object, and you're trying to access the property where
and the property id
from where
which seems to be another object.
For this then you would need to declare the type at the parameter level so that TS knows what to expect when you want to use args
e.g.
type declaration in line:
.... = async ({ args: { where: { id: number } } }) => {
...
}
Or:
type MyWhere = {
id: number;
};
type MyArgs = {
where: MyWhere;
};
.... = async ({ args: MyArgs } }) => {
...
}
hope it makes sense 🙂
I'd highly suggest to take on a TS course, this is interactive one:
https://www.codecademy.com/learn/learn-typescriptJure Topolak
08/23/2022, 10:56 AM_export_ interface ArgsDictionary {
[argName: string]: any;
}
Gustavo
08/23/2022, 11:06 AMwhere
part not the args
, so basically TS is unsure whether there will be a where inside your dictionary, if you're 100% sure it'll be there try adding a !
which is a way to tell TS I know this property will be there.
{
id: args.where!.id
}
A better approach tho, would be to use a type safe dictionary if you have control over it, here's an example:
https://blog.logrocket.com/building-type-safe-dictionary-typescript/
Simply scroll down 'till you find const dictionary: { [key: string]: string } = {};
but I'd recommend reading the whole article 🙂Jure Topolak
08/24/2022, 11:26 AMThis is also not allowed according to eslint rules 😄 Now I realised that arguments of resolvers generated by prisma-typegraphql can be mapped directly:which is a way to tell TS I know this property will be there!
const posts = await context.prisma.post.findMany({
where: args.where as object,
});
And so the error is also gone.
Thank you for your input… helped a lot!