最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java GraphQL error handling with batching - Stack Overflow

programmeradmin1浏览0评论

I am using quarkus with SmallRye GraphQL

Given this resolver:

public Person partner(@Source Person person) throws GraphQLException {
  Person partner = service.getPartner(person);
  if (partner == null) {
      throw new GraphQLException("No partner for person " + person.getName());
  }
  return partner;
}

I get responses like this:

{
  "errors": [
    {
      "message": "No partner for person John Doe",
      ...
    },
    {
      "message": "No partner for person James Doe",
      ...
    }
  ],
  "data": {
    "allPersons": [
      {
        "name": "Test Doe",
        "partner": {
          "name": "Jane Doe"
        }
      },
      {
        "name": "John Doe",
        "partner": null
      },
      {
        "name": "James Doe",
        "partner": null
      }
    ]
  }
}

How can I replicate this behavior using a batched resolver like this:

public List<Person> partner(@Source List<Person> persons) throws GraphQLException {
  ...
}

It seems to be a trivial problem, but I can't find any resources on the matter. Another post suggests that it is not possible to return both errors and data from a resolver (How to return both error and data in a graphql resolver?). Is there any workaround / alternative approach for this problem?

I am using quarkus with SmallRye GraphQL

Given this resolver:

public Person partner(@Source Person person) throws GraphQLException {
  Person partner = service.getPartner(person);
  if (partner == null) {
      throw new GraphQLException("No partner for person " + person.getName());
  }
  return partner;
}

I get responses like this:

{
  "errors": [
    {
      "message": "No partner for person John Doe",
      ...
    },
    {
      "message": "No partner for person James Doe",
      ...
    }
  ],
  "data": {
    "allPersons": [
      {
        "name": "Test Doe",
        "partner": {
          "name": "Jane Doe"
        }
      },
      {
        "name": "John Doe",
        "partner": null
      },
      {
        "name": "James Doe",
        "partner": null
      }
    ]
  }
}

How can I replicate this behavior using a batched resolver like this:

public List<Person> partner(@Source List<Person> persons) throws GraphQLException {
  ...
}

It seems to be a trivial problem, but I can't find any resources on the matter. Another post suggests that it is not possible to return both errors and data from a resolver (How to return both error and data in a graphql resolver?). Is there any workaround / alternative approach for this problem?

Share Improve this question asked Mar 13 at 21:33 janbikojanbiko 915 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Unfortunately no, I don't think that is possible with batching. It would be achievable if there was some API to append custom errors to the response, but I guess it would be quite clunky to do... You may report that as a feature request in the project repository if you want (I am the maintainer).

发布评论

评论列表(0)

  1. 暂无评论