Fields marked with @hasInverse do not populate in @lambda parent object

What version of Dgraph are you using?

v21.03.0-92-g0c9f60156

Have you tried reproducing the issue with the latest release?

yes

What is the hardware spec (RAM, OS)?

Dgraph Cloud

Steps to reproduce the issue

Add a user and add GameScores related to that user

Schema

type User  {
  id: ID!
  username: String! @id @search(by: [fulltext])
  scores: [GameScore]! @hasInverse(field: "user")
  high_score: GameScore @lambda
}

type GameScore {
  user: User!
  record_date: DateTime!
  score: Int!
}

Lambda:

const generateHighScore = (parent)  => {
  console.log(parent);
}

self.addGraphQLResolvers({
  "User.high_score": generateHighScore
})

Expected behaviour and actual result.

Expected log of parent :

{ id: "0xExample", userName: "exampleName", scores: [ ] }

Actual log of parent :

{ id: "0xExample", userName: "exampleName" }

The goal was to make a simple Lambda that produced the high score from the score nodes connected to a given user. Every user is initialized with an empty array as the “scores” field. This field does not show up in the parent object when calling the lambda.

Hey @kdilla301,

IIRC, only ‘scalars’ are sent to lambda handlers. So this limits what can be done, but I think I have a solution for you.

Note that there are two types of lambdas: query resolvers and mutation handlers. You’re sort of mixing the two here. Based on what you posted, I believe the best solution is for the high_score edge to be a query resolver, not a mutation lambda.

Here’s a query resolver that should do the trick…

lambda JS:

async function calculateHighScore({parent, graphql}) {
    const results = await graphql(`
    query {
        getUser(id: "${parent.id}") {
          id
          scores(order: {desc: score}, first: 1) {
            score
            record_date
          }
        }
    }`)
    return {
        user: {id: parent.id},
        score: results.data.getUser.scores[0].score,
        record_date: results.data.getUser.scores[0].record_date
    }
}

self.addGraphQLResolvers({
    "User.high_score": calculateHighScore
})

Thank you. This is what I was looking for