Lambda expects an array with 1 elements. Array contains 4

I have a working function now…at least it works in the shell:

Query(
  Lambda(
    ["inputdata"],
    Let(
      { dataIds: Select("ids", Var("inputdata")) },
      Union(
        Map(
          Var("dataIds"),
          Lambda(
            ["id"],
            Select(
              ["data"],
              Paginate(
                Match(
                  Index("certificate_by_dealer"),
                  Ref(Collection("Dealers"), Var("id"))
                )
              )
            )
          )
        )
      )
    )
  )
)

This works in the Shell of the dashboard but returns an error in GraphQL:

{
  "errors": [
    {
      "message": "Lambda expects an array with 1 elements. Array contains 4.",
      "extensions": {
        "code": "invalid argument"
      }
    }
  ]
}

Here is the shell Call:

Call("user_dealers_all_certificates", {ids: ["302122229239382536", "301394099049595400"]})
[
  Ref(Collection("Certificate"), "302122488174739977"),
  Ref(Collection("Certificate"), "302132509580198413"),
  Ref(Collection("Certificate"), "302120872550859273")
]

and the GraphQL query:

query	{
  allUserDealersCertificate(data: {ids: "302122229239382536"}){
    data {
      _id
    }
  }
}

and relevant schema:

allUserDealersCertificate(
    data: allUserDealersCertificateInputs
  ): [Certificate]
    @resolver(name: "user_dealers_all_certificates", paginated: true)
input allUserDealersCertificateInputs {
  ids: [ID!]!
}

I have posted this issue in a few locations, so hopefully it can get resolved. Thanks.

I’d much prefer to provide a working solution. Would you mind sharing your GraphQL schema, and at least one document from each involved type? With those details, I can repro the problem.

Since you have paginated: true then fauna will pass the pagination variables automatically so you’re also getting variables for "_size", "after", "before" where _size is the page size and after and before are the cursor references that will allow you to page through the query results.

When this happens to me I add these to my UDF and it solves my issue. Even if you do not need these variables it should work. So your Lamda input would look like this inside your UDF:

["inputdata", "_size", "after", "before"]

I got into the habit of going into the Fauna dashboard and looking at the Graphql docs there instead of referring to my actual schema file. The reason being that when you import your schema file into Fauna they do perform some transformations on it automatically in order to make it fit into their ecosystem better. These are nice since during dev I can think about schema the same way I would if I was not using Fauna but it does make these types of issues surprising. By going into the graphql docs and schema in the Fauna dashboard you can see the actual files that are being used in your queries, which will mostly likely be slightly different then the one you created yourself.

1 Like

@yellowspaceboots has the right of it. And that’s good advice, too, to be familiar with generated schema. I’ve gotten mixed up myself before :upside_down_face:

Here is a function that works with pagination:

Query(
  Lambda(
    ["ref_ids", "size", "after", "before"],
    Let(
      {
        match: Union(
          Map(
            Var("ref_ids"),
            Lambda(
              "ref_id",
              Match(
                Index("certificate_by_dealer"),
                Ref(Collection("Dealers"), Var("ref_id"))
              )
            )
          )
        ),
        page: If(
          Equals(Var("before"), null),
          If(
            Equals(Var("after"), null),
            Paginate(Var("match"), { size: Var("size") }),
            Paginate(Var("match"), { after: Var("after"), size: Var("size") })
          ),
          Paginate(Var("match"), { before: Var("before"), size: Var("size") })
        )
      },
      Map(Var("page"), Lambda("ref", Get(Var("ref"))))
    )
  )
)

Note about Input Types

It might be handy at times to wrap your inputs into a separate input type. For example:

type Query {
  allUserDealersCertificate(data: allUserDealersCertificateInputs): 
    [Certificate] @resolver(name: "user_dealers_all_certificates", paginated: true)
}

input allUserDealersCertificateInputs {
  ids: [ID!]!
}

If you do, make sure to destructure the argument.

Query(
  Lambda(
    ["data", "size", "after", "before"],
    Let(
      {
        ref_ids: Select("ids", Var("data")), // <---
        match: Union(
          Map(
            Var("ref_ids"),
// ...

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