|
| 1 | +package resolver |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + |
| 6 | + "graphql-go-compatibility/model" |
| 7 | +) |
| 8 | + |
| 9 | +func FindDeprecatedProductBySkuAndPackage(sku string, packageArg string) (*model.DeprecatedProduct, error) { |
| 10 | + for i := range deprecatedProducts { |
| 11 | + if deprecatedProducts[i].Sku == sku && deprecatedProducts[i].Package == packageArg { |
| 12 | + return deprecatedProducts[i], nil |
| 13 | + } |
| 14 | + } |
| 15 | + return nil, nil |
| 16 | +} |
| 17 | + |
| 18 | +func FindProductById(id string) (*model.Product, error) { |
| 19 | + for i := range products { |
| 20 | + if products[i].ID == id { |
| 21 | + return products[i], nil |
| 22 | + } |
| 23 | + } |
| 24 | + return nil, nil |
| 25 | +} |
| 26 | + |
| 27 | +func FindProductBySkuAndPackage(sku string, packageArg string) (*model.Product, error) { |
| 28 | + for i := range products { |
| 29 | + if *products[i].Sku == sku && *products[i].Package == packageArg { |
| 30 | + return products[i], nil |
| 31 | + } |
| 32 | + } |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +func FindProductBySkuAndVariationID(sku string, variationID string) (*model.Product, error) { |
| 37 | + for i := range products { |
| 38 | + if *products[i].Sku == sku && products[i].Variation.ID == variationID { |
| 39 | + return products[i], nil |
| 40 | + } |
| 41 | + } |
| 42 | + return nil, nil |
| 43 | +} |
| 44 | + |
| 45 | +func FindProductResearchByProductId(product *model.Product) ([]*model.ProductResearch, error) { |
| 46 | + switch product.ID { |
| 47 | + case "apollo-federation": |
| 48 | + return research[:1], nil |
| 49 | + case "apollo-studio": |
| 50 | + return research[1:], nil |
| 51 | + default: |
| 52 | + return nil, nil |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func CalculateAverageProductsCreatedPerYear(user *model.User) (*int, error) { |
| 57 | + if user.TotalProductsCreated == nil { |
| 58 | + return nil, nil |
| 59 | + } |
| 60 | + var avgProductsCreated = int(math.Round(float64(*user.TotalProductsCreated) / float64(user.YearsOfEmployment))) |
| 61 | + return &avgProductsCreated, nil |
| 62 | +} |
| 63 | + |
| 64 | +// entity resolvers |
| 65 | + |
| 66 | +// @key(fields: "sku package") |
| 67 | +func DeprecatedProductEntityResolver(params map[string]interface{}) (*model.DeprecatedProduct, error) { |
| 68 | + sku, skuOk := params["sku"].(string) |
| 69 | + pkg, pkgOk := params["package"].(string) |
| 70 | + if skuOk && pkgOk { |
| 71 | + return FindDeprecatedProductBySkuAndPackage(sku, pkg) |
| 72 | + } |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +// @key(fields: "id") |
| 77 | +// @key(fields: "sku package") |
| 78 | +// @key(fields: "sku variation { id }") |
| 79 | +func ProductEntityResolver(params map[string]interface{}) (*model.Product, error) { |
| 80 | + id, ok := params["id"].(string) |
| 81 | + if ok { |
| 82 | + return FindProductById(id) |
| 83 | + } else { |
| 84 | + sku, skuOk := params["sku"].(string) |
| 85 | + if skuOk { |
| 86 | + pkg, pkgOk := params["package"].(string) |
| 87 | + if pkgOk { |
| 88 | + return FindProductBySkuAndPackage(sku, pkg) |
| 89 | + } else { |
| 90 | + variation, varOk := params["variation"].(map[string]interface{}) |
| 91 | + if varOk { |
| 92 | + varId, varIdOk := variation["id"].(string) |
| 93 | + if varIdOk { |
| 94 | + return FindProductBySkuAndVariationID(sku, varId) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return nil, nil |
| 101 | +} |
| 102 | + |
| 103 | +// @key(fields: "study { caseNumber }") |
| 104 | +func ProductResearchEntityResolver(params map[string]interface{}) (*model.ProductResearch, error) { |
| 105 | + study, ok := params["study"].(map[string]interface{}) |
| 106 | + if ok { |
| 107 | + caseNumber, caseOk := study["caseNumber"].(string) |
| 108 | + if caseOk { |
| 109 | + for i := range research { |
| 110 | + if research[i].Study.CaseNumber == caseNumber { |
| 111 | + return research[i], nil |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return nil, nil |
| 117 | +} |
| 118 | + |
| 119 | +// @key(fields: "email") |
| 120 | +func UserEntityResolver(params map[string]interface{}) (*model.User, error) { |
| 121 | + email, ok := params["email"].(string) |
| 122 | + if ok { |
| 123 | + for i := range users { |
| 124 | + var user *model.User |
| 125 | + if users[i].Email == email { |
| 126 | + user = users[i] |
| 127 | + } |
| 128 | + |
| 129 | + totalProducts, totalSpecified := params["totalProductsCreated"].(float64) |
| 130 | + if totalSpecified { |
| 131 | + total := int(totalProducts) |
| 132 | + user.TotalProductsCreated = &total |
| 133 | + } |
| 134 | + |
| 135 | + yearsOfEmployment, yearsSpecified := params["yearsOfEmployment"].(float64) |
| 136 | + if yearsSpecified { |
| 137 | + user.YearsOfEmployment = int(yearsOfEmployment) |
| 138 | + } |
| 139 | + |
| 140 | + if user != nil { |
| 141 | + return user, nil |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return nil, nil |
| 146 | +} |
0 commit comments