sangria-graphql/sangria

Partial Response Along with Errors

abhigp27 opened this issue · 3 comments

Hi Team,

My usecase accepts multiple inputIds, Currently It will throw error only if all of them fails, But I want to return the results of all the successful Inputs and error for non-success ones. Not able to find any doc on this. Can you please suggest that if it is possible of not?

yanns commented

I don't really understand your use-case.
Can you provide examples?

So my fetcher accepts multiple inputIds, for which I make db queries, Some input Ids might be invalid, so the business logic will return 404 for some IDs and data for others. I want to display to the user that there were some 404 for some IDs and data for others.

But currently either I can send data or throw and exception, not the both. How can I do both?
The format I am supposed to have will be like:

{
    "error": [
        {
            "code": "404",
            "message": "Invalid {ID1} supplied to function"
        },
        {
            "code": "404",
            "message": "Invalid {ID2} supplied to function"
        }
    ],
    "payload": [
        {
            "id": "1",
            "name": "John",
            "age": "25"
        },
        {
            "id": "2",
            "name": "Jane",
            "age": "24"
        }
    ]
}
yanns commented

You can either use Option and return Some or None. A 404 will be encoded as null in the response.
Or you can use union types as result, something like:

sealed trait Result[A]
case class NotFound(id:String) extend Result[Nothing]
case class Found(result: A) extend Result[A]

In graphQL, the consumer would consume your API like this:

query {
  fetchUsers {
    ... on NotFound {
      id
    }
    ... on Found {
      result {
        id, name, age
      }
    }
  }
}