Slash GraphQL Lambda bug?

Was playing around with SlashGraphQL and Lambdas and noticed a bug. To reproduce, do the following:

  1. Add a schema with the following:
type Task {
  id: ID!
  title: String! @search(by: [fulltext])
  completed: Boolean! @search
  user: User!
}

type User {
  fullName: String @lambda
  username: String! @id @search(by: [hash])
  name: String @search(by: [exact])
  tasks: [Task] @hasInverse(field: user)
}
  1. Add a lambda script with the following:
const fullName = () => `hi`

self.addGraphQLResolvers({
  "User.fullName": fullName
})
  1. Then run the following in the API Explorer:
mutation AddTasks {
  addTask(input: [
    {title: "Create a database", completed: false, user: {username: "your-email@example.com"}},
    {title: "Write A Schema", completed: false, user: {username: "your-email@example.com"}},
    {title: "Put Data In", completed: false, user: {username: "your-email@example.com"}},
    {title: "Complete Tasks with UI", completed: false, user: {username: "your-email@example.com"}},
    {title: "Profit!", completed: false, user: {username: "your-email@example.com"}},

    {title: "Walking", completed: false, user: {username: "frodo@dgraph.io"}},
    {title: "More Walking", completed: false, user: {username: "frodo@dgraph.io"}},
    {title: "Discard Jewelery", completed: false, user: {username: "frodo@dgraph.io"}},

    {title: "Meet Dad", completed: false, user: {username: "skywalker@dgraph.io"}},
    {title: "Dismantle Empire", completed: false, user: {username: "skywalker@dgraph.io"}}
  ]) {
    numUids
    task {
      id
      title
      user {
        fullName
        username
      }
    }
  }
}

You’ll get output like the following:

{
  "data": {
    "addTask": {
      "numUids": 10,
      "task": [
        {
          "id": "0x5b85",
          "title": "Write A Schema",
          "user": {
            "fullName": "hi",
            "username": "your-email@example.com"
          }
        },
        {
          "id": "0x5b86",
          "title": "Walking",
          "user": {
            "fullName": "hi",
            "username": "frodo@dgraph.io"
          }
        },
        {
          "id": "0x5b87",
          "title": "More Walking",
          "user": {
            "fullName": null,
            "username": "frodo@dgraph.io"
          }
        },
        {
          "id": "0x5b88",
          "title": "Discard Jewelery",
          "user": {
            "fullName": null,
            "username": "frodo@dgraph.io"
          }
        },
        {
          "id": "0x5b89",
          "title": "Dismantle Empire",
          "user": {
            "fullName": "hi",
            "username": "skywalker@dgraph.io"
          }
        },
        {
          "id": "0x5b8a",
          "title": "Create a database",
          "user": {
            "fullName": null,
            "username": "your-email@example.com"
          }
        },
        {
          "id": "0x5b8b",
          "title": "Put Data In",
          "user": {
            "fullName": null,
            "username": "your-email@example.com"
          }
        },
        {
          "id": "0x5b8c",
          "title": "Complete Tasks with UI",
          "user": {
            "fullName": null,
            "username": "your-email@example.com"
          }
        },
        {
          "id": "0x5b8d",
          "title": "Profit!",
          "user": {
            "fullName": null,
            "username": "your-email@example.com"
          }
        },
        {
          "id": "0x5b8e",
          "title": "Meet Dad",
          "user": {
            "fullName": null,
            "username": "skywalker@dgraph.io"
          }
        }
      ]
    }
  },
  "extensions": {
    "touched_uids": 147,
    "tracing": {
      "version": 1,
      "startTime": "2021-01-10T13:47:16.207430123Z",
      "endTime": "2021-01-10T13:47:16.215849208Z",
      "duration": 8419104,
      "execution": {
        "resolvers": [
          {
            "path": [
              "addTask"
            ],
            "parentType": "Mutation",
            "fieldName": "addTask",
            "returnType": "AddTaskPayload",
            "startOffset": 153151,
            "duration": 8188076,
            "dgraph": [
              {
                "label": "mutation",
                "startOffset": 469205,
                "duration": 4047792
              },
              {
                "label": "query",
                "startOffset": 5405383,
                "duration": 1237134
              }
            ]
          }
        ]
      }
    }
  }
}

Note: Some of the fullNames are what they should be “hi”, but some are null. I’m assuming this is a bug right?

2 Likes

This appears to be a bug!

In fact thanks to this post, I also noticed that my own lambda fields in a project of mine had null in them.

Specifically, the null happens only when you make many mutations at once. I suspect this has to do with the roundtrip - an iterator goes through all the inputs, applying the lambda function in an asynchronous manner. Thus some fields are not filled in when the lambda function does not return in time.

Accepting this as a bug. @gja FYI

2 Likes

@akashjain971 noticed this observation

Observe that there are 3 unique emails for which the lambda was called and the result also has only 3 resolved fields(one for each email) and rest are null.

I tried this:

mutation AddTasks {
  addTask(input: [
    {title: "Create a database", completed: false, user: {username: "your-email1@example.com"}},
    {title: "Write A Schema", completed: false, user: {username: "your-email2@example.com"}},
    {title: "Put Data In", completed: false, user: {username: "your-email3@example.com"}},
    {title: "Complete Tasks with UI", completed: false, user: {username: "your-email4@example.com"}},
    {title: "Profit!", completed: false, user: {username: "your-email5@example.com"}},

    {title: "Walking", completed: false, user: {username: "frodo]@dgraph.io"}},
    {title: "More Walking", completed: false, user: {username: "frodo8@dgraph.io"}},
    {title: "Discard Jewelery", completed: false, user: {username: "frodo@0dgraph.io"}},

    {title: "Meet Dad", completed: false, user: {username: "skywalker@dgraph.io"}},
    {title: "Dismantle Empire", completed: false, user: {username: "skywalker1234@dgraph.io"}}
  ]) {
    numUids
    task {
      id
      title
      user {
        fullName
        username
      }
    }
  }
}

this yielded the correct result (“hi” in full name).

I further observe that the @id directive in username, if removed, will cause the null results above to not exist. It would appear that @id is making @lambda function funnily. This should not be the case IINM.

2 Likes

This has been fixed with perf(GraphQL): Generate GraphQL query response by optimized JSON encoding (GraphQL-730) by abhimanyusinghgaur · Pull Request #7371 · dgraph-io/dgraph · GitHub and will be part of the 21.03 release.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.