As per Spring for GraphQL documentation,
"By default the Querydsl and the Query by Example integrations turn the GraphQL selection set into property path hints that the underlying Spring Data module uses to limit the selection."
There are no examples on how Spring Data module uses property path hints to limit selection.
Given following GraphQL schema exists,
type Query {
employees: [Employee]
}
type Employee {
id: ID!
firstName: String!
lastName: String!
}
Given following Repository & Entity exists,
@Getter
@Setter
@Entity
@Table(name = "employee")
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
}
@GraphQlRepository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Integer>,
QuerydslPredicateExecutor<EmployeeEntity> {
}
Given following DataFetcher code exists,
@Controller
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
@Autowired
EmployeeMapper employeeMapper;
@QueryMapping
List<Employee> employees(DataFetchingEnvironment environment) throws Exception {
Iterable<EmployeeEntity> employeeEntities = QuerydslDataFetcher.builder(employeeRepository).many().get(environment);
// Iterate employeeEntities and convert each to Employee
return StreamSupport.stream(employeeEntities.spliterator(), false)
.map(employeeMapper::toEmployee)
.toList();
}
record Employee(Integer id, String firstName, String lastName) {
}
}
When executing following query,
query {
employees {
firstName
}
}
Expecting to see select clause to have only first_name, but actually all the fields are being fetched from database.
From the logs
select
e1_0.id,
e1_0.first_name,
e1_0.last_name
from
employee e1_0
When using Spring for GraphQL, how can I generate SQL with select clause having just the column names that correspond to the fields or selection set requested so we can avoid fetching values of all the columns in an entity?