SubSelectionRequired 类型的验证错误:字段类型为 null 需要子选择

Bro*_*der 19 graphql graphql-java

我正在处理一个 graphql 问题,在该问题中我收到请求的以下错误

{
  customer(id: "5ed6092b-6924-4d31-92d0-b77d4d777b47") {
    id
    firstName
    lastName
    carsInterested
  }
}

 "message": "Validation error of type SubSelectionRequired: Sub selection required for type null of field carsInterested @ 'customer/carsInterested'",
Run Code Online (Sandbox Code Playgroud)

下面是我的架构

type Customer {
  id: ID!
  firstName: String!
  lastName: String!
  # list of cars that the customer is interested in
  carsInterested: [Car!]

}

type Query {
  # return 'Customer'
  customer(id: ID!): Customer
}
Run Code Online (Sandbox Code Playgroud)

我确实有一个 CustomerResolver,里面有函数 carsInterested。它看起来如下

@Component
public class CustomerResolver implements GraphQLResolver<Customer> {

    private final CarRepository carRepo;

    public CustomerResolver(CarRepository carRepo) {this.carRepo = carRepo;}

    public List<Car> carsInterested(Customer customer) {
        return carRepo.getCarsInterested(customer.getId());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我查询没有“carsInterested”的客户时,它可以正常工作。知道为什么我会收到此错误吗?

谢谢

Dan*_*den 35

当请求解析为对象类型(或对象类型列表)的字段时,您还必须指定该对象类型的字段。特定字段(或根)的字段列表称为选择集或子选择,并由一对大括号括起来。

您正在请求carsInterested,它返回 的列表Cars,因此您还需要指定Car要返回的字段:

{
  customer(id: "5ed6092b-6924-4d31-92d0-b77d4d777b47") {
    id
    firstName
    lastName
    carsInterested {
      # one or more Car fields here
    }
  }
}
Run Code Online (Sandbox Code Playgroud)