Commit 3deb7846 authored by LAVENIER's avatar LAVENIER
Browse files

[enh] Use GraphQLUtils

parent 3234e669
......@@ -26,6 +26,7 @@ import io.leangen.graphql.annotations.GraphQLArgument;
import io.leangen.graphql.annotations.GraphQLEnvironment;
import io.leangen.graphql.annotations.GraphQLMutation;
import io.leangen.graphql.annotations.GraphQLQuery;
import io.leangen.graphql.execution.ResolutionEnvironment;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.dao.technical.model.IEntity;
import net.sumaris.core.exception.SumarisTechnicalException;
......@@ -44,9 +45,11 @@ import net.sumaris.core.vo.technical.extraction.AggregationStrataVO;
import net.sumaris.core.vo.technical.extraction.ExtractionTableColumnFetchOptions;
import net.sumaris.core.vo.technical.extraction.ExtractionTableColumnVO;
import net.sumaris.server.config.ExtractionWebAutoConfiguration;
import net.sumaris.server.http.GraphQLUtils;
import net.sumaris.server.security.ExtractionSecurityService;
import net.sumaris.server.geojson.ExtractionGeoJsonConverter;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.graph.spi.GraphHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;
......@@ -79,17 +82,17 @@ public class AggregationGraphQLService {
@GraphQLQuery(name = "aggregationType", description = "Get one aggregation type")
@Transactional(readOnly = true)
public AggregationTypeVO getAggregationType(@GraphQLArgument(name = "id") int id,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
securityService.checkReadAccess(id);
return aggregationService.getTypeById(id, getFetchOptions(fields));
return aggregationService.getTypeById(id, getFetchOptions(GraphQLUtils.fields(env)));
}
@GraphQLQuery(name = "aggregationTypes", description = "Get all available aggregation types")
@Transactional(readOnly = true)
public List<AggregationTypeVO> getAllAggregationTypes(@GraphQLArgument(name = "filter") AggregationTypeFilterVO filter,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
filter = fillFilterDefaults(filter);
return aggregationService.findTypesByFilter(filter, getFetchOptions(fields));
return aggregationService.findTypesByFilter(filter, getFetchOptions(GraphQLUtils.fields(env)));
}
@GraphQLQuery(name = "aggregationRows", description = "Read an aggregation")
......@@ -112,7 +115,7 @@ public class AggregationGraphQLService {
@Transactional(readOnly = true)
public List<ExtractionTableColumnVO> getAggregationColumns(@GraphQLArgument(name = "type") AggregationTypeVO type,
@GraphQLArgument(name = "sheet") String sheetName,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
// Check type
type = aggregationService.getTypeByFormat(type);
......@@ -120,6 +123,8 @@ public class AggregationGraphQLService {
// Check access right
securityService.checkReadAccess(type);
Set<String> fields = GraphQLUtils.fields(env);
ExtractionTableColumnFetchOptions fetchOptions = ExtractionTableColumnFetchOptions.builder()
.withRankOrder(fields.contains(ExtractionTableColumnVO.Fields.RANK_ORDER))
.build();
......
......@@ -33,10 +33,10 @@
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.leangen.graphql</groupId>
<artifactId>spqr</artifactId>
<scope>provided</scope>
</dependency>
<!-- hibernate -->
......
/*
* #%L
* SUMARiS
* %%
* Copyright (C) 2019 SUMARiS Consortium
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package net.sumaris.server.http;
import graphql.schema.SelectedField;
import io.leangen.graphql.execution.ResolutionEnvironment;
import java.util.Set;
import java.util.stream.Collectors;
public class GraphQLUtils extends io.leangen.graphql.util.GraphQLUtils {
protected GraphQLUtils() {
// helper class
}
public static Set<String> fields(ResolutionEnvironment env) {
return env.dataFetchingEnvironment.getSelectionSet().getFields()
.stream()
.map(SelectedField::getQualifiedName)
.filter(qualifiedName -> !qualifiedName.endsWith("/__typename"))
.collect(Collectors.toSet());
}
public static Set<String> immediateFields(ResolutionEnvironment env) {
return env.dataFetchingEnvironment.getSelectionSet().getImmediateFields()
.stream().map(SelectedField::getQualifiedName)
.filter(qualifiedName -> !qualifiedName.endsWith("/__typename"))
.collect(Collectors.toSet());
}
}
\ No newline at end of file
......@@ -38,6 +38,7 @@ import net.sumaris.core.exception.SumarisBusinessException;
import net.sumaris.core.exception.SumarisTechnicalException;
import net.sumaris.server.exception.ErrorCodes;
import net.sumaris.server.exception.ErrorHelper;
import net.sumaris.server.http.GraphQLUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataRetrievalFailureException;
......@@ -49,27 +50,12 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class GraphQLHelper {
public class GraphQLHelper extends GraphQLUtils {
private GraphQLHelper() {
// helper class
}
public static Set<String> fields(ResolutionEnvironment env) {
return env.dataFetchingEnvironment.getSelectionSet().getFields()
.stream()
.map(SelectedField::getQualifiedName)
.filter(qualifiedName -> !qualifiedName.endsWith("/__typename"))
.collect(Collectors.toSet());
}
public static Set<String> immediateFields(ResolutionEnvironment env) {
return env.dataFetchingEnvironment.getSelectionSet().getImmediateFields()
.stream().map(SelectedField::getQualifiedName)
.filter(qualifiedName -> !qualifiedName.endsWith("/__typename"))
.collect(Collectors.toSet());
}
public static Map<String, Object> getVariables(Map<String, Object> request, ObjectMapper objectMapper) {
Object variablesObj = request.get("variables");
if (variablesObj == null) {
......
......@@ -26,7 +26,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.execution.SubscriptionExecutionStrategy;
import graphql.schema.GraphQLSchema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -24,6 +24,7 @@ package net.sumaris.server.http.graphql.administration;
import com.google.common.base.Preconditions;
import io.leangen.graphql.annotations.*;
import io.leangen.graphql.execution.ResolutionEnvironment;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.exception.SumarisTechnicalException;
......@@ -93,7 +94,7 @@ public class AdministrationGraphQLService {
@GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
@GraphQLArgument(name = "sortBy", defaultValue = PersonVO.Fields.PUBKEY) String sort,
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction,
@GraphQLEnvironment() Set<String> fields
@GraphQLEnvironment Set<String> fields
) {
List<PersonVO> result = personService.findByFilter(filter, offset, size, sort, SortDirection.fromString(direction));
......@@ -175,7 +176,7 @@ public class AdministrationGraphQLService {
@GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
@GraphQLArgument(name = "sortBy", defaultValue = ReferentialVO.Fields.NAME) String sort,
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment Set<String> fields) {
List<DepartmentVO> result = departmentService.findByFilter(filter, offset, size, sort, SortDirection.fromString(direction));
// Fill logo Url (if need)
......@@ -189,7 +190,7 @@ public class AdministrationGraphQLService {
@GraphQLQuery(name = "department", description = "Get a department")
@Transactional(readOnly = true)
public DepartmentVO getDepartmentById(@GraphQLArgument(name = "id") int id,
@GraphQLEnvironment() Set<String> fields
@GraphQLEnvironment Set<String> fields
) {
DepartmentVO result = departmentService.get(id);
......
......@@ -24,6 +24,7 @@ package net.sumaris.server.http.graphql.administration;
import com.google.common.base.Preconditions;
import io.leangen.graphql.annotations.*;
import io.leangen.graphql.execution.ResolutionEnvironment;
import lombok.extern.slf4j.Slf4j;
import lombok.NonNull;
import net.sumaris.core.dao.technical.Pageables;
......@@ -53,6 +54,7 @@ import net.sumaris.core.vo.referential.PmfmVO;
import net.sumaris.core.vo.referential.ReferentialVO;
import net.sumaris.core.vo.referential.TaxonGroupVO;
import net.sumaris.core.vo.referential.TaxonNameVO;
import net.sumaris.server.http.GraphQLUtils;
import net.sumaris.server.http.security.AuthService;
import net.sumaris.server.http.security.IsAdmin;
import net.sumaris.server.http.security.IsSupervisor;
......@@ -138,9 +140,9 @@ public class ProgramGraphQLService {
@GraphQLQuery(name = "strategy", description = "Get a strategy")
@Transactional(readOnly = true)
public StrategyVO getStrategy(@GraphQLNonNull @GraphQLArgument(name = "id") @NonNull Integer id,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
return strategyService.get(id, getStrategyFetchOptions(fields));
return strategyService.get(id, getStrategyFetchOptions(GraphQLUtils.fields(env)));
}
@GraphQLQuery(name = "strategies", description = "Search in strategies")
......@@ -151,11 +153,11 @@ public class ProgramGraphQLService {
@GraphQLArgument(name = "size", defaultValue = "1000") Integer size,
@GraphQLArgument(name = "sortBy", defaultValue = StrategyVO.Fields.LABEL) String sort,
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
return strategyService.findByFilter(filter,
Pageables.create(offset, size, sort, SortDirection.fromString(direction)),
getStrategyFetchOptions(fields));
getStrategyFetchOptions(GraphQLUtils.fields(env)));
}
@GraphQLQuery(name = "strategiesCount", description = "Get strategies count")
......@@ -192,11 +194,11 @@ public class ProgramGraphQLService {
@GraphQLQuery(name = "strategies", description = "Get program's strategies")
public List<StrategyVO> getStrategiesByProgram(@GraphQLContext ProgramVO program,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
if (program.getStrategies() != null) {
return program.getStrategies();
}
return strategyService.findByProgram(program.getId(), getStrategyFetchOptions(fields));
return strategyService.findByProgram(program.getId(), getStrategyFetchOptions(GraphQLUtils.fields(env)));
}
@GraphQLQuery(name = "pmfms", description = "Get strategy's pmfms")
......@@ -211,10 +213,11 @@ public class ProgramGraphQLService {
@GraphQLQuery(name = "denormalizedPmfms", description = "Get strategy's denormalized pmfms")
public List<DenormalizedPmfmStrategyVO> getDenormalizedPmfmByStrategy(@GraphQLContext StrategyVO strategy,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
if (strategy.getDenormalizedPmfms() != null) {
return strategy.getDenormalizedPmfms();
}
Set<String> fields = GraphQLUtils.fields(env);
return strategyService.findDenormalizedPmfmsByFilter(PmfmStrategyFilterVO.builder().strategyId(strategy.getId()).build(),
PmfmStrategyFetchOptions.builder()
.uniqueByPmfmId(true)
......@@ -303,7 +306,7 @@ public class ProgramGraphQLService {
public Publisher<ProgramVO> updateProgram(@GraphQLArgument(name = "id") final Integer id,
@GraphQLArgument(name = "label") final String label,
@GraphQLArgument(name = "interval", defaultValue = "30", description = "Minimum interval to find changes, in seconds.") final Integer minIntervalInSecond,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
......@@ -315,7 +318,7 @@ public class ProgramGraphQLService {
// Watch by label
Preconditions.checkNotNull(label, "Invalid 'label' argument");
ProgramFetchOptions fetchOptions = getProgramFetchOptions(fields);
ProgramFetchOptions fetchOptions = getProgramFetchOptions(GraphQLUtils.fields(env));
return changesPublisherService.getPublisher((lastUpdateDate) -> programService.findIfNewerByLabel(label, lastUpdateDate, fetchOptions).orElse(null), minIntervalInSecond, true);
}
......@@ -325,8 +328,9 @@ public class ProgramGraphQLService {
@IsUser
public Publisher<List<StrategyVO>> updateProgramStrategies(@GraphQLNonNull @GraphQLArgument(name = "programId") final int programId,
@GraphQLArgument(name = "interval", defaultValue = "30", description = "Minimum interval to find changes, in seconds.") final Integer minIntervalInSecond,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment ResolutionEnvironment env) {
Set<String> fields = GraphQLUtils.fields(env);
StrategyFetchOptions fetchOptions = getStrategyFetchOptions(fields);
Preconditions.checkArgument(programId >= 0, "Invalid programId");
......
......@@ -1331,28 +1331,25 @@ public class DataGraphQLService {
return result;
}
protected <T extends IWithVesselSnapshotEntity<?, VesselSnapshotVO>> VesselSnapshotVO fillVesselSnapshot(T bean, Set<String> fields) {
protected <T extends IWithVesselSnapshotEntity<?, VesselSnapshotVO>> void fillVesselSnapshot(T bean, Set<String> fields) {
// Add vessel if need
VesselSnapshotVO result = bean.getVesselSnapshot();
// No ID: cannot fetch
if (result == null || result.getId() == null) return result;
// Already fetched
if (result.getName() != null) return result;
if (result == null || result.getId() == null) return;
// Fetch (if need)
if (hasVesselFeaturesField(fields)) {
return vesselService.getSnapshotByIdAndDate(bean.getVesselSnapshot().getId(), Dates.resetTime(bean.getVesselDateTime()));
if (result.getName() == null && hasVesselFeaturesField(fields)) {
result = vesselService.getSnapshotByIdAndDate(bean.getVesselSnapshot().getId(), Dates.resetTime(bean.getVesselDateTime()));
bean.setVesselSnapshot(result);
}
return null;
}
protected <T extends IWithVesselSnapshotEntity<?, VesselSnapshotVO>> void fillVesselSnapshot(List<T> beans, Set<String> fields) {
// Add vessel if need
if (hasVesselFeaturesField(fields)) {
beans.forEach(bean -> {
if (bean.getVesselSnapshot() != null && bean.getVesselSnapshot().getId() != null) {
if (bean.getVesselSnapshot() != null && bean.getVesselSnapshot().getId() != null && bean.getVesselSnapshot().getName() == null) {
bean.setVesselSnapshot(vesselService.getSnapshotByIdAndDate(bean.getVesselSnapshot().getId(), bean.getVesselDateTime()));
}
});
......
......@@ -91,7 +91,7 @@ public class ConfigurationGraphQLService {
public ConfigurationVO getConfiguration(
@GraphQLArgument(name = "id") Integer id,
@GraphQLArgument(name = "label") String label,
@GraphQLEnvironment() Set<String> fields
@GraphQLEnvironment Set<String> fields
) {
SoftwareVO software = getSoftware(id, label);
return toConfiguration(software, fields);
......@@ -101,7 +101,7 @@ public class ConfigurationGraphQLService {
@IsAdmin
public ConfigurationVO saveConfiguration(
@GraphQLArgument(name = "config") ConfigurationVO configuration,
@GraphQLEnvironment() Set<String> fields) {
@GraphQLEnvironment Set<String> fields) {
SoftwareVO software = service.save(configuration);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment