.load() nullable field returns "Not found" error.
Hello,
I am trying to query another api from my module which is related by the user module via the email key for each user. Each user can have possibly one api entry belonging to him but it can also be null.
User Module:
user.entity
...
@Field(() => ApiUser, { nullable: true })
api?: ApiUser;
...
user.resolver
@ResolveField(() => ApiUser, { nullable: true })
async api(
@Parent() user: User,
@Loader(ApiUserLoader)
apiUserLoader: DataLoader<ApiUser["email"], ApiUser>,
): Promise<ApiUser> {
const email = user.email;
return apiUserLoader.load(email);
}
API Module:
api-user.loader.ts
protected getOptions = () => ({
propertyKey: "email",
query: async (keys: Array<ApiUser["email"]>) => {
const users = await this.apiUserService.findByEmails(keys);
return users;
},
});
The API ignores user it does not find. E.g. returns [] for a single entry if it can't find it.
If I query for a user which the external API does not know I get this error by the data loader:
{
"message": "ApiUser does not exist ([email protected])",
}
Since the field api in the resolver is nullable I would expect that I get null returned.
Thank you.
I guess Loader does not know anything about field being optional or not. Is there any way to control this behavior for when to generate error and when not to?
Other than try catch and ignore the manually, @SvenC56 ... did you find any workaround for this?