graphqlgraphql-java

Validation error of type SubSelectionRequired: Sub selection required for type null of field


I am working on a graphql issue where I am getting following error for the request

{
  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'",

Below is my schema

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
}

I do have a CustomerResolver with function carsInterested in it.It looks as follows

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

When I query for customer without 'carsInterested', it works properly. Any idea why I am getting this error?

Thanks


Solution

  • When requesting a field that resolves to an object type (or a list of an object type), you have to specify the fields on that object type as well. The list of fields for a particular field (or the root) is called a selection set or a sub selection and is wrapped by a pair of curly brackets.

    You're requesting the carsInterested, which returns a list of Cars, so you need to specify which Car fields you want returned as well:

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