Getting back "String or Number expected, Array provided." When calling function in graphQL

Hello

I have a schema that looks like this:
type User {
userDishes: [UserDish]! @relation(name: “user_userDishes”)

}

type UserDish {
owner: User! @relation(name: “user_userDishes”)
dishId: String!
}

I want to be able to retrieve all userDishes with the same User relation.

I’ve set up this function:
Screenshot 2021-04-22 at 13.17.05

When testing the function in the shell I get back an object with a data array, which contains the documents I need.

So far so good, everything seems to work at this point.

My problem is when I try to query this function in graphQL, I’ve added the query in my schema and also added the function as a resolver, but I just get back this error message when querying it:
“String or Number expected, Array provided.”

I am pretty much stuck here, as I cannot find anything related to this in the docs…

Hi Rasmus,

You mentioned that you’ve added a function for graphql,
could you send it as well?


Here you go! :smiley:

Could you try to wrap “userId” in array brackets, like this (I’ve put only a couple of first lines from your function):

    Query(
      Lambda(
         ["userId"],
         Map(
           // the rest of the function

I just tried it and it gave me back this error when calling the function in graphQL

I still get the correct data back, when calling the function from the shell

Can you show us the GraphQL that you are running? Each GraphQL query describes the data that it wants to receive in the result.

Either you need to revise the GraphQL query to match the output of the resolver, or adjust the resolver to match the GraphQL query, or both.

This is it

I think you will need to share the UDF body for us to help diagnose what is happening.

As an aside, a more GraphQL canonical way of running this query might not be to create a top level custom resolver, but to query the user and expand the query through the relationship.

If you have a Schema:

type User {
  dishes: [UserDish] @relationship
}

type UserDish {
  user: User! @relationship
  title: String!
}

then you can query, without any customer resolvers, like this:

query {
  findUserById(id: "292128025028329991") {
    _id
    dishes: {
      data {
        _id
        title
      }
    }
  }
}

Oh I think I see it. You are return the fauna Page from the UDF. This will only work if you set pagination to true and then also handle the extra arguments in the UDF.

Wrap the whole UDF body in

Select(
   'data',
   /* everything here */
)

@parkhomenko 's comment about wrapping arguments is also very important for GraphQL resolvers.

Hopefully that will resolve things. Or, at least solve that one issue.

alternatives:

tell fauna you want to return a Page.

type Query {
  userDishesByUserId(userId: ID!): [UserDish]!
    @resolver(name: "GetUserDishesByUserId", paginated: "true")
}

But then you need to deal with the extra size and cursor arguments in the UDF. At that point, I strongly suggest going back to the already generated queries available, since the pagination is already built in.

query {
  findUserById(id: "292128025028329991") {
    _id
    dishes(_size: 3): {
      data {
        _id
        title
      }
      before
      after
    }
  }
}

Thank you so much. I figured it out the day before yesterday though. You are right that I was returning the entire page in my function, and I was asking for an array in my resolver :slight_smile:

So I changed my function to return an array instead like this:
Screenshot 2021-04-25 at 18.09.03
Which solved the problem.

Thank you so much for your time!

1 Like