Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.load() nullable field returns "Not found" error. #21

Open
SvenC56 opened this issue Jan 29, 2021 · 1 comment
Open

.load() nullable field returns "Not found" error. #21

SvenC56 opened this issue Jan 29, 2021 · 1 comment

Comments

@SvenC56
Copy link

SvenC56 commented Jan 29, 2021

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.

@lightning003
Copy link

lightning003 commented Jun 3, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants