Skip to content

Commit

Permalink
Merge pull request #254 from terrestris/graphql-all-by-ids
Browse files Browse the repository at this point in the history
Graphql all by ids
  • Loading branch information
dnlkoch committed Jan 29, 2021
2 parents 95d9d5e + e76ef96 commit 8b5437e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class GraphQLProvider {

protected GraphQLSchema graphQLSchema;

@Autowired
private ApplicationContext appContext;

@Autowired
private List<BaseGraphQLDataFetcher> dataFetchers;

Expand Down Expand Up @@ -110,7 +107,7 @@ protected List<TypeRuntimeWiring.Builder> gatherTypes() {
private void addBaseTypes(List<TypeRuntimeWiring.Builder> typeBuilders, BaseGraphQLDataFetcher dataFetcher) {
String simpleClassName = dataFetcher.getGenericSimpleClassName();

String queryAllName = String.format("all%s", (English.plural(simpleClassName)));
String queryAllName = String.format("all%s", English.plural(simpleClassName));
typeBuilders.add(TypeRuntimeWiring.newTypeWiring("Query")
.dataFetcher(queryAllName, dataFetcher.findAll()));

Expand All @@ -123,6 +120,12 @@ private void addBaseTypes(List<TypeRuntimeWiring.Builder> typeBuilders, BaseGrap

log.debug("Added GraphQL query {}", queryByIdName);

String queryAllByIdsName = String.format("all%sByIds", English.plural(simpleClassName));
typeBuilders.add(TypeRuntimeWiring.newTypeWiring("Query")
.dataFetcher(queryAllByIdsName, dataFetcher.findAllByIds()));

log.debug("Added GraphQL query {}", queryAllByIdsName);

String createName = String.format("create%s", simpleClassName);
typeBuilders.add(TypeRuntimeWiring.newTypeWiring("Mutation")
.dataFetcher(createName, dataFetcher.create()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.GenericTypeResolver;
Expand Down Expand Up @@ -43,6 +45,15 @@ public DataFetcher findOne() {
};
}

public DataFetcher findAllByIds() {
return dataFetchingEnvironment -> {
List<Integer> entityIds = dataFetchingEnvironment.getArgument("ids");

return this.service.findAllById(entityIds.stream().map(Integer::longValue).collect(
Collectors.toList()));
};
}

public DataFetcher create() {
return dataFetchingEnvironment -> {
LinkedHashMap<String, Object> createEntity = dataFetchingEnvironment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public Optional<S> findOne(Long id) {
return repository.findById(id);
}

@PostFilter("hasRole('ROLE_ADMIN') or hasPermission(filterObject, 'READ')")
@Transactional(readOnly = true)
public List<S> findAllById(List<Long> id) {
return (List<S>) repository.findAllById(id);
}

@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#entity, 'READ')")
@Transactional(readOnly = true)
public Revisions<Integer, S> findRevisions(S entity) {
Expand Down
6 changes: 6 additions & 0 deletions shogun-lib/src/main/resources/graphql/shogun.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ scalar Geometry
type Query {
allApplications: [Application]
applicationById(id: Int): Application
allApplicationsByIds(ids: [Int]): [Application]

allFiles: [File]
fileById(id: Int): File
allFilesByIds(ids: [Int]): [File]

allGroups: [Group]
groupById(id: Int): Group
allGroupsByIds(ids: [Int]): [Group]

allImageFiles: [ImageFile]
imageFileById(id: Int): ImageFile
allImageFilesByIds(ids: [Int]): [ImageFile]

allLayers: [Layer]
layerById(id: Int): Layer
allLayersByIds(ids: [Int]): [Layer]

allUsers: [User]
userById(id: Int): User
allUsersByIds(ids: [Int]): [User]
}

type Mutation {
Expand Down

0 comments on commit 8b5437e

Please sign in to comment.