My Products
Help
omelhus
PARTNER

Issue with _in when the array is empty

by omelhus

Hey,

 

There's a logical issue when using _in. When the array is empty i expect it to be false, but instead it returns all rows.

 

So just for the sake of a demo I'm setting the sellerOrBuyer to 999 here, so that we know that it won't return any, but this might be due to some other filter in the real world.

 

When order in this example returns 0 rows I would expect sellerDetails to do the same, but instead it returns all rows.

 

 

query IssuesWith_InSample($cid: Int!, $sellers: [Int!] = []) {
  useCompany(no: $cid) {
    order(filter: {sellerOrBuyer: {_gt: 999}}, first: 3) {
      items {
        sellerOrBuyer @export(as: "sellers")
      }
    }
    sellerDetails: associate(filter: {employeeNo: {_in: $sellers}}) {
      items {
        employeeNo
        name
      }
    }
  }
}

 

 

I understand that this will be a breaking change to fix, but I'd like to argue that we're not past the point of breaking changes with bnxt yet.

8 REPLIES 8

by Marius Bancila
omelhus
PARTNER

by omelhus

So I just realized that theres a very simple workaround for this. Setting the initial array with a value of [-1].

 

query IssuesWith_InSample($cid: Int!, $sellers: [Int!] = [-1]) {
  useCompany(no: $cid) {
    order(filter: {sellerOrBuyer: {_gt: 0}}, first: 3) {
      items {
        sellerOrBuyer @export(as: "sellers")
      }
    }
    sellerDetails: associate(filter: {employeeNo: {_in: $sellers}}) {
      items {
        employeeNo
        name
      }
    }
  }
}

by Marius Bancila

OK. We can make it without returning an error.

by Marius Bancila

I get your point: in the context of @export, it's not an error, because the array was evaluated. But if you pass an empty array directly, would you consider it an error? Would you expect a message that your argument was incorrect? Or would you expect an empty result, and nothing about the cause?

 

Yes, we can do it so that an error is not returned, but I see pros and cons for it.

omelhus
PARTNER

by omelhus

I see no reason to return an error if I send an empty array. An empty array is not an invalid argument to an input of array.

 

If I pass an empty error to for(const x of y) it just does 0 iterations, it doesn't throw an error.

by Marius Bancila (Updated ‎16-02-2024 14:05 by Marius Bancila VISMA )

We can't just return no data, we should also indicate the problem. So you get an error message back. It would look like this:

 

{
  "errors": [
    {
      "message": "The array argument for the _in operator is empty.",
      "path": [
        "useCompany",
        "associate"
      ],
      "extensions": {
        "details": "GraphQL.ExecutionError: The array argument for the _in operator is empty."
      }
    }
  ],
  "data": {
    "useCompany": {
      "order": {
        "totalCount": 0,
        "items": null
      },
      "sellerDetails": null
    }
  }
}

 

Seems good?

omelhus
PARTNER

by omelhus (Updated ‎16-02-2024 14:08 by omelhus PARTNER )

But this would raise and log an error, when it's not strictly an error. I don't see any reason to return an error in this situation.

 

Can't you just return an empty array?

 

But still, this would solve my specific issue as I'm ignoring errors in this context.

by Marius Bancila

You are correct. There should be nothing to return. Will look into it.