Skip to content

Errored field should return null, is instead causing the entire query to return null #1107

Answered by tyranron
twiclo asked this question in Q&A
Discussion options

You must be logged in to vote

@twiclo your User.team field cannot be null, because you've declared it as non-nullable type in the schema.

To make it nullable, you should declare the computable field to return the nullable type:

#[graphql_object(context = Context)]
impl User {
  async fn team(&self, ctx: &Context) -> FieldResult<Option<Team>> {
    // and return `Ok(None)` whenever you want to return `null`
  }
}

Or if you want to turn any error into null (which I don't recommend), just don't use the Result, only Option:

#[graphql_object(context = Context)]
impl User {
  async fn team(&self, ctx: &Context) -> Option<Team> {
    Ok(Team::get_users_team(validate_perm(&ctx.user.perms).ok()?, ctx, self.user_id).await.ok()?)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@twiclo
Comment options

@tyranron
Comment options

Answer selected by twiclo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants