Hi ! I am making a cooking recipes management app and was giving a go to dgraph. My GraphQL schema is the following (simplified version):
type Ingredient {
id: String! @id
name: String!
}
type IngredientWithQuantity {
ingredient: Ingredient!
quantity: Float!
}
type Recipe {
id: ID!
name: String!
ingredientsWithQuantity: [IngredientWithQuantity!]!
}
I have the following use case (that I thought very good for testing out the database capabilities):
A user can provide N
constraints [(ingredientId1, quantity1), ..., (ingredientIdN, quantityN)]
such as [("carrot", 0.5), ("salad", 1.3)]
I want to query the DB such as it fetches the recipes maximizing the use of constrained ingredients with respect to quantities
The (very simplified) pseudo algorithm would be the following:
FOR EACH recipe
score = 0
FOR EACH (ingredient, quantity) IN recipe.ingredientsWithQuantity
FOR EACH (cstIngredient, cstQuantity) IN constraints // the N user constraints
IF cstIngredient.id == ingredient.id AND cstQuantity > quantity
score += 1
score = score / COUNT(recipe.ingredientsWithQuantity)
SORT recipes BY score DESC
RETURN recipes
I cannot make the score computation part in GraphQL so I have to use custom DQL resolvers.
At first, DQL seemed to be a perfect fit for this, but I am discovering in the docs that I cannot send an array of N constraints to this resolver. I have no idea how to handle this list of tuples [(ingredient, quantity)]
on DQL side.
So I am currently stuck. Is there really a way to accomplish that with DGraph ? Would be cool because I really liked the doc ! Any hint is very welcome.