Resolvers
QuerySearchResolver
classes are designed to enrich QueryFilter
POJOs by incorporating data from external components before processing in the QuerySearch
class.
This is essential due to constraints where each component might be persisted in a different database, preventing direct cross-component queries as they are blocked by the persistence provider.
Example
Imagine you have the email address of a user in the account::User
element, but you need their username in a query for
an element in a different component. You would need to take the following steps in the QuerySearchResolver
class:
- Retrieve email: Retrieve the user's email address from the
QueryFilter
object. - Resolve user: Use the finder for the
User
element to find the user based on the email address. - Enrich filter: If the user was found, add their username to the
QueryFilter
object.
public CrudsResult<MyElementQueryFilter> resolve(
ParameterContext<MyElementQueryFilter> queryFilterParameter) {
final Context context = queryFilterParameter.getContext();
final MyElementQueryFilter queryFilter = queryFilterParameter.getValue();
// @anchor:resolve:start
// @anchor:resolve:end
// anchor:custom-resolve:start
/* 1. Retrieve email */
String email = queryFilter.getEmail();
if (email != null) {
/* 2. Resolve user */
UserFindByEmailEqDetails finder = new UserFindByEmailEqDetails();
finder.setEmail(email);
SearchDetails<UserFindByEmailEqDetails> searchDetails =
SearchDetails.fetchNDetails(finder, 1);
UserAgent userAgent = UserAgent.getUserAgent(context);
SearchResult<UserDetails> userSearchResult = userAgent.find(searchDetails);
if (userSearchResult.isError()) {
return CrudsResult.error(userSearchResult.getDiagnostics());
}
/* 3. Enrich filter */
userSearchResult.getFirstResult().ifPresent(
searchResult -> queryFilter.setUsername(searchResult.getName()));
}
// anchor:custom-resolve:end
return CrudsResult.success(queryFilter);
}