|
| 1 | +--- |
| 2 | +title: "Data mapping" |
| 3 | +date: 2018-09-09T12:52:46+10:00 |
| 4 | +draft: false |
| 5 | +tags: [documentation] |
| 6 | +weight: 106 |
| 7 | +description: Data mapping |
| 8 | +--- |
| 9 | +# Mapping data |
| 10 | + |
| 11 | +## How graphql maps object data to types |
| 12 | + |
| 13 | +At its heart graphql is all about declaring a type schema and mapping that over backing runtime data. |
| 14 | + |
| 15 | +As the designer of the type schema, it is your challenge to get these elements to meet in the middle. |
| 16 | + |
| 17 | +For example imagine we want to have a graphql type schema as follows: |
| 18 | + |
| 19 | + |
| 20 | +{{< highlight graphql "linenos=table" >}} |
| 21 | + type Query { |
| 22 | + products(match : String) : [Product] # a list of products |
| 23 | + } |
| 24 | + |
| 25 | + type Product { |
| 26 | + id : ID |
| 27 | + name : String |
| 28 | + description : String |
| 29 | + cost : Float |
| 30 | + tax : Float |
| 31 | + } |
| 32 | + |
| 33 | +{{< / highlight >}} |
| 34 | + |
| 35 | +We could then run queries over this simple schema via a something like the following: |
| 36 | + |
| 37 | +{{< highlight graphql "linenos=table" >}} |
| 38 | + query ProductQuery { |
| 39 | + products(match : "Paper*") |
| 40 | + { |
| 41 | + id, name, cost, tax |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | +{{< / highlight >}} |
| 46 | + |
| 47 | +We will have a ``DataFetcher`` on the ``Query.products`` field that is responsible for finding a list of products that match |
| 48 | +the argument passed in. |
| 49 | + |
| 50 | +Now imagine we have 3 downstream services. One that gets product information, one that gets product cost information and one that calculates |
| 51 | +product tax information. |
| 52 | + |
| 53 | +graphql-java works by running data fetchers over objects for all that information and mapping that back to the types specified in the schema. |
| 54 | + |
| 55 | +Our challenge is to take these 3 sources of information and present them as one unified type. |
| 56 | + |
| 57 | +We could specify data fetchers on the ``cost`` and ``tax`` fields that does those calculations but this is more to maintain and likely to lead to |
| 58 | +`N+1 performance problems`. |
| 59 | + |
| 60 | +We would be better to do all this work in the ``Query.products`` data fetcher and create a unified view of the data at that point. |
| 61 | + |
| 62 | +{{< highlight java "linenos=table" >}} |
| 63 | + DataFetcher productsDataFetcher = new DataFetcher() { |
| 64 | + @Override |
| 65 | + public Object get(DataFetchingEnvironment env) { |
| 66 | + String matchArg = env.getArgument("match"); |
| 67 | + |
| 68 | + List<ProductInfo> productInfo = getMatchingProducts(matchArg); |
| 69 | + |
| 70 | + List<ProductCostInfo> productCostInfo = getProductCosts(productInfo); |
| 71 | + |
| 72 | + List<ProductTaxInfo> productTaxInfo = getProductTax(productInfo); |
| 73 | + |
| 74 | + return mapDataTogether(productInfo, productCostInfo, productTaxInfo); |
| 75 | + } |
| 76 | + }; |
| 77 | +{{< / highlight >}} |
| 78 | + |
| 79 | +So looking at the code above we have 3 types of information that need to be combined in a way such that a graphql query above can get access to |
| 80 | +the fields ``id, name, cost, tax`` |
| 81 | + |
| 82 | +We have two ways to create this mapping. One is via using a not type safe ``List<Map>`` structure and one by creating a type safe ``List<ProductDTO>`` class that |
| 83 | +encapsulates this unified data. |
| 84 | + |
| 85 | +The ``Map`` technique could look like this. |
| 86 | + |
| 87 | +{{< highlight java "linenos=table" >}} |
| 88 | + private List<Map> mapDataTogetherViaMap(List<ProductInfo> productInfo, List<ProductCostInfo> productCostInfo, List<ProductTaxInfo> productTaxInfo) { |
| 89 | + List<Map> unifiedView = new ArrayList<>(); |
| 90 | + for (int i = 0; i < productInfo.size(); i++) { |
| 91 | + ProductInfo info = productInfo.get(i); |
| 92 | + ProductCostInfo cost = productCostInfo.get(i); |
| 93 | + ProductTaxInfo tax = productTaxInfo.get(i); |
| 94 | + |
| 95 | + Map<String, Object> objectMap = new HashMap<>(); |
| 96 | + objectMap.put("id", info.getId()); |
| 97 | + objectMap.put("name", info.getName()); |
| 98 | + objectMap.put("description", info.getDescription()); |
| 99 | + objectMap.put("cost", cost.getCost()); |
| 100 | + objectMap.put("tax", tax.getTax()); |
| 101 | + |
| 102 | + unifiedView.add(objectMap); |
| 103 | + } |
| 104 | + return unifiedView; |
| 105 | + } |
| 106 | + |
| 107 | +{{< / highlight >}} |
| 108 | + |
| 109 | +The more type safe ``DTO`` technique could look like this. |
| 110 | + |
| 111 | +{{< highlight java "linenos=table" >}} |
| 112 | + |
| 113 | + class ProductDTO { |
| 114 | + private final String id; |
| 115 | + private final String name; |
| 116 | + private final String description; |
| 117 | + private final Float cost; |
| 118 | + private final Float tax; |
| 119 | + |
| 120 | + public ProductDTO(String id, String name, String description, Float cost, Float tax) { |
| 121 | + this.id = id; |
| 122 | + this.name = name; |
| 123 | + this.description = description; |
| 124 | + this.cost = cost; |
| 125 | + this.tax = tax; |
| 126 | + } |
| 127 | + |
| 128 | + public String getId() { |
| 129 | + return id; |
| 130 | + } |
| 131 | + |
| 132 | + public String getName() { |
| 133 | + return name; |
| 134 | + } |
| 135 | + |
| 136 | + public String getDescription() { |
| 137 | + return description; |
| 138 | + } |
| 139 | + |
| 140 | + public Float getCost() { |
| 141 | + return cost; |
| 142 | + } |
| 143 | + |
| 144 | + public Float getTax() { |
| 145 | + return tax; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private List<ProductDTO> mapDataTogetherViaDTO(List<ProductInfo> productInfo, List<ProductCostInfo> productCostInfo, List<ProductTaxInfo> productTaxInfo) { |
| 150 | + List<ProductDTO> unifiedView = new ArrayList<>(); |
| 151 | + for (int i = 0; i < productInfo.size(); i++) { |
| 152 | + ProductInfo info = productInfo.get(i); |
| 153 | + ProductCostInfo cost = productCostInfo.get(i); |
| 154 | + ProductTaxInfo tax = productTaxInfo.get(i); |
| 155 | + |
| 156 | + ProductDTO productDTO = new ProductDTO( |
| 157 | + info.getId(), |
| 158 | + info.getName(), |
| 159 | + info.getDescription(), |
| 160 | + cost.getCost(), |
| 161 | + tax.getTax() |
| 162 | + ); |
| 163 | + unifiedView.add(productDTO); |
| 164 | + } |
| 165 | + return unifiedView; |
| 166 | + } |
| 167 | +{{< / highlight >}} |
| 168 | + |
| 169 | +The graphql engine will now use that list of objects and run the query sub fields ``id, name, cost, tax`` over it. |
| 170 | + |
| 171 | +The default data fetcher in graphql-java is ``graphql.schema.PropertyDataFetcher`` which has both map support and POJO support. |
| 172 | + |
| 173 | +For every object in the list it will look for an ``id`` field, find it by name in a map or via a `getId()` getter method and that will be sent back in the graphql |
| 174 | +response. It does that for every field in the query on that type. |
| 175 | + |
| 176 | +By creating a "unified view" at the higher level data fetcher, you have mapped between your runtime view of the data and the graphql schema view of the data. |
| 177 | + |
0 commit comments