| 15 | import static graphql.schema.FieldCoordinates.coordinates; |
| 16 | |
| 17 | @SuppressWarnings({"Convert2Lambda", "unused", "ClassCanBeStatic"}) |
| 18 | public class MappingExamples { |
| 19 | |
| 20 | interface ProductInfo { |
| 21 | String getId(); |
| 22 | |
| 23 | String getName(); |
| 24 | |
| 25 | String getDescription(); |
| 26 | } |
| 27 | |
| 28 | interface ProductCostInfo { |
| 29 | float getCost(); |
| 30 | } |
| 31 | |
| 32 | interface ProductTaxInfo { |
| 33 | float getTax(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | void productsDataFetcher() { |
| 38 | |
| 39 | DataFetcher productsDataFetcher = new DataFetcher() { |
| 40 | @Override |
| 41 | public Object get(DataFetchingEnvironment env) { |
| 42 | String matchArg = env.getArgument("match"); |
| 43 | |
| 44 | List<ProductInfo> productInfo = getMatchingProducts(matchArg); |
| 45 | |
| 46 | List<ProductCostInfo> productCostInfo = getProductCosts(productInfo); |
| 47 | |
| 48 | List<ProductTaxInfo> productTaxInfo = getProductTax(productInfo); |
| 49 | |
| 50 | return mapDataTogether(productInfo, productCostInfo, productTaxInfo); |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | private Object mapDataTogether(List<ProductInfo> productInfo, List<ProductCostInfo> productCostInfo, List<ProductTaxInfo> productTaxInfo) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | private List<Map> mapDataTogetherViaMap(List<ProductInfo> productInfo, List<ProductCostInfo> productCostInfo, List<ProductTaxInfo> productTaxInfo) { |
| 60 | List<Map> unifiedView = new ArrayList<>(); |
| 61 | for (int i = 0; i < productInfo.size(); i++) { |
| 62 | ProductInfo info = productInfo.get(i); |
| 63 | ProductCostInfo cost = productCostInfo.get(i); |
| 64 | ProductTaxInfo tax = productTaxInfo.get(i); |
| 65 | |
| 66 | Map<String, Object> objectMap = new HashMap<>(); |
| 67 | objectMap.put("id", info.getId()); |
| 68 | objectMap.put("name", info.getName()); |
| 69 | objectMap.put("description", info.getDescription()); |
| 70 | objectMap.put("cost", cost.getCost()); |
| 71 | objectMap.put("tax", tax.getTax()); |
| 72 | |
| 73 | unifiedView.add(objectMap); |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…