Skip to content

Commit

Permalink
feat(#549): accompanying prices in GQL
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashornych committed May 31, 2024
1 parent 7781489 commit 1a6070c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.evitadb.api.query.QueryConstraints.*;
import static io.evitadb.externalApi.api.ExternalApiNamingConventions.PROPERTY_NAME_NAMING_CONVENTION;
Expand Down Expand Up @@ -348,7 +347,7 @@ private String[] resolveAccompanyingPriceLists(@Nonnull SelectionSetAggregator s
.stream()
.flatMap(f -> SelectionSetAggregator.getImmediateFields(PriceForSaleDescriptor.ACCOMPANYING_PRICE.name(), f.getSelectionSet())
.stream()
.flatMap(apf -> Stream.of((String[]) apf.getArguments().get(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name()))))
.flatMap(apf -> ((List<String>) apf.getArguments().get(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name())).stream()))
.toArray(String[]::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

Expand Down Expand Up @@ -99,8 +100,10 @@ protected abstract P computePrices(@Nonnull EntityDecorator entity,
@Nonnull
protected String[] resolveDesiredPricesLists(@Nonnull DataFetchingEnvironment environment,
@Nonnull EntityQueryContext context) {
return Optional.ofNullable((String) environment.getArgument(PriceForSaleFieldHeaderDescriptor.PRICE_LIST.name()))
.map(priceList -> new String[]{priceList})
return Optional.ofNullable((List<String>) environment.getArgument(PriceForSaleFieldHeaderDescriptor.PRICE_LISTS.name()))
.map(it -> it.toArray(String[]::new))
.or(() -> Optional.ofNullable((String) environment.getArgument(PriceForSaleFieldHeaderDescriptor.PRICE_LIST.name()))
.map(priceList -> new String[]{ priceList }))
.or(() -> Optional.ofNullable(context.getDesiredPriceInPriceLists()))
.orElseThrow(() -> new GraphQLInvalidArgumentException("Missing price list argument. You can use `" + PriceForSaleFieldHeaderDescriptor.PRICE_LIST.name() + "` or `" + PriceForSaleFieldHeaderDescriptor.PRICE_LISTS.name() + "` parameter for specifying custom price list."));
}
Expand Down Expand Up @@ -133,7 +136,8 @@ protected AccompanyingPrice[] resolveDesiredAccompanyingPrices(@Nonnull DataFetc
.filter(f -> f.getArguments().size() == 1 && f.getArguments().containsKey(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name()))
.map(f -> new AccompanyingPrice(
f.getAlias() != null ? f.getAlias() : f.getName(),
(String[]) f.getArguments().get(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name())
((List<String>) f.getArguments().get(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name()))
.toArray(String[]::new)
))
.toArray(AccompanyingPrice[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

Expand Down Expand Up @@ -109,7 +110,8 @@ private Optional<PriceContract> getPrefetchedPrice(@Nonnull PrefetchedPriceForSa

@Nonnull
private String[] resolveDesiredPriceListsForCustomPrice(@Nonnull DataFetchingEnvironment environment) {
return Optional.ofNullable((String[]) environment.getArgument(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name()))
return Optional.ofNullable((List<String>) environment.getArgument(AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name()))
.map(it -> it.toArray(String[]::new))
.orElseThrow(() -> new GraphQLInvalidArgumentException("Missing price list argument. You can use `" + AccompanyingPriceFieldHeaderDescriptor.PRICE_LISTS.name() + "` parameter for specifying custom price list."));
}

Expand Down

0 comments on commit 1a6070c

Please sign in to comment.