private DataFetcher projectEntityDecorationsDataFetcher(Class<? extends ProjectEntity> projectEntityClass) { return environment -> { Object o = environment.getSource(); if (projectEntityClass.isInstance(o)) { // Filters Optional<String> typeFilter = GraphqlUtils.getStringArgument(environment, "type"); // Gets the raw list return decorationService.getDecorations((ProjectEntity) o).stream() // Filter by type .filter(property -> typeFilter .map(typeFilterName -> StringUtils.equals( typeFilterName, property.getDecorationType() )) .orElse(true) ) // OK .collect(Collectors.toList()); } else { return null; } }; } }