【问题标题】:Validation error of type SubSelectionRequired: Sub selection required for type null of fieldSubSelectionRequired 类型的验证错误:字段的类型为空需要子选择
【发布时间】:2020-04-20 15:44:03
【问题描述】:

我正在处理一个 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'",

下面是我的架构

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
}

我确实有一个带有功能 cars 的 CustomerResolver 对它感兴趣。它看起来如下

@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());
    }
}

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

谢谢

【问题讨论】:

    标签: graphql graphql-java


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-20
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2016-12-11
      • 2018-08-04
      • 2013-02-07
      相关资源
      最近更新 更多