How to make a Graphql type where Price is dependent on Quantity

Viewed 67

Let's say I run an online store that sells coffee beans at volume. It's entirely reasonable to offer discounts based on quantity, so the price field is actually a function:

type Bean {
  id: ID! @id
  title: String!
  description: String!
  price(quantity: float, unit: WeightUnit = KG): Float!
}

enum WeightUnit {
  KG
  LB
}

Beyond this point I'm not sure how I would attach a specific function to a unique instance of Bean. I've also toyed with the idea of prices field where I can list an array of SizePriceTuple, but Prisma forces me to have an id field:

type Bean {
  ...
  prices: [SizePriceTuple]
}

type SizePriceTuple {
  id: ID! @id
  size: Int!
  unit: WeightUnit!
  price: Int!
}

I don't think tuples of (size, unit, price) is really something that needs an ID attached to it, and I'm not sure what a mutation to create a new Bean with prices would look like.

Can someone either a) provide an example where there the price field is actually a function that looks something up or b) show a mutation and a query of a Bean with prices being an array of SizePriceTuple or c) how to make a type without Prisma (v1.34) yelling at me to make an id field?

0 Answers
Related