Display values passed into a lambda

Hi,

I’m trying to learn how faunadb works, and I’m looking for a way to debug what is going on in my UDFs, privilege functions, etc.

I’m trying to debug my “create” privilege function. I’m using graphql, and my schema looks like this:

type User {
  email: String! @unique
  company: Company!
}

type Company {
  name: String! @unique
  users: [User!]! @relation
}

When a new user registers, I want them to create the user, and then create a new company, with the new user associated, but I want to make sure users can only create companies with themselves associated, and then I plan to build out the other rules so only company users can edit / delete / read their own company as well. The user gets created just fine, but when I try and create the new company, I run into permission issues.

My graphql mutation looks like this:

variables = {
  data: {
    name: "foo",
    users: {
      connect: [userId], //this is the newly created user id
    },
  },
}

mutation createCompany($data: CompanyInput!) {
  createCompany(data: $data) {
    _id
    name
  }
}

and the role privileges looks like this:

{
  resource: Collection("Company"),
  actions: {
    read: true,
    write: true,
    create: Query(
      Lambda(
        "values",
        ContainsValue(Identity(), Select(["data", "users"], Var("values")))
      )
    ),
    delete: true,
    history_read: false,
    history_write: false,
    unrestricted_read: false
  }
},

I’ve tried Equals(Identity(), Select([“data”, “users”], Var(“values”)))
and I’ve tried ContainsValue(Identity(), Select([“data”, “users”, “connect”], Var(“values”)))

nothing seems to work, and I can’t tell what’s going on. Currently, I’m just messing around with trial and error, so my real question is, can I output the value of “data” that gets passed to the lambda somehow so i can see what’s getting passed into the lambda when the mutation is executed? That way I could at least dump it to the shell and play around there.

Ok I’ve done a little more testing and was able to write a function that returns true when written as a UDF:

it looks like this:

Query(
  Lambda(
    "values",
    ContainsValue(
      Select("id", CurrentIdentity()),
      Select(["data", "users", "connect"], Var("values"))
    )
  )
)

When i run
Call(“test”, {“data”:{“name”:“MY_COMPANY_NAME”,“users”:{“connect”:[“MY_USER_REF”]}}}) I get back true.

strange thing is, when I set it up as a privilege, it still isn’t working, leading me to believe that the data gets changed before the privilege gets invoked.

Lol just figured it was happening because the role didn’t have write access to users, enabling that fixed it. That being said, It still took about a day and a half of trial and error to figure out, so if anyway has some tips for debugging these that would be awesome, thanks!

Wanted to share this logging function post I just created in case it’s of use!

Thanks! I just voted for that feature.

It’d be great if they had a proper log however, or better yet some type of ide where you could write a UDF or Privilege function that attaches to the database and allows you to step through execution to debug. I can see it being a lot to ask, however they’re trying to sell faunadb as your entire backend so tooling like this will definately be necessary.

1 Like