User defined function not working as resolver

Given the following user defined function:

CreateFunction({
  name: "getProductByName3",
  body: Query(
        Lambda('name',
            Filter(
                Map(
                    Paginate(Match(Index("Products"))),
                    Lambda('product', Get(Var('product')))
                ),
                Lambda('product', Equals(Var('name'), Select(['data', 'name'], Var('product'))))
            )
        )
  ),
})

When I call this function using the shell like:

Call(Function("getProductByName3"), "kiwi")

It works, and the result is a set of products that match “kiwi” on name.

If however I try to use this function as a resolver like:

type Product {
  name: String!
}

type Query {
  getProductsByName3(name: String!): [Product!] @resolver(name: "getProductByName3", paginated: true)  
}

And use the following query in the GraphQL console:

{
  getProductsByName3(name: "kiwi") {
    data {
      name
    }
  }
}

The result is an empty data array, while calling the function in the shell still works. Any ideas on what’s going wrong? And I’m creating this resolver for practice, not gonna use this in production.

Thank you for reading,

Roy.

Hi @Roy and welcome!

How do your documents look like?

Luigi

1 Like

Hello Luigi,

This is one of the documents, the Products collection consists of multiple documents like these, the product name is not unique.

{
  "ref": Ref(Collection("Product"), "284715861993849349"),
  "ts": 1607785055060000,
  "data": {
    "name": "kiwi"
  }
}

Oke I suspect it has something to do with scoping? Using the following user defined functions works as expected. (I’ve replaced the variable to equal with a hardcoded kiwi string)

CreateFunction({
  name: "getProductByName",
  body: Query(
        Lambda('name',
            Filter(
                Map(
                    Paginate(Match(Index("Products"))),
                    Lambda('product', Get(Var('product')))
                ),
                Lambda('product', Equals('kiwi', Select(['data', 'name'], Var('product'))))
            )
        )
  ),
})

getProductsByName(name: String!): [Product!] @resolver(name: "getProductByName", paginated: true)

This would send the parameters to UDF in an array like this [name, size, after, before].
In your UDF getProductByName3

In Equals, you are comparing an array with String. Change it to below to read the first element in the provided array and it should work.

Lambda('product', Equals(Select([0],Var('name')), Select(['data', 'name'], Var('product'))))

Please let us know if you still see an issue.

1 Like