I have two questions.
- How can I use optional parameters.
- If i can use optional parameters, is the following use case a good practice?
For my example I use the following simplified schema.
# A recipe can be easy or hard for example.
type Difficulty {
name: String! @id
recipes: [Recipe!]!
}
type Recipe {
slug: String! @id
title: String! @search(by: [fulltext])
difficulty: Difficulty! @hasInverse(field: recipes)
}
I created a DQL query that allows me to find the recipe by the difficulty and a text search on the title:
query q($difficulty: string, $recipeTitle: string){
var(func: eq(Difficulty.name, $difficulty)){
Difficulty.recipes {
RecipeIds as uid
}
}
queryRecipeByTitleAndDifficulty(func :uid(RecipeIds)) @filter(anyoftext(Recipe.title,$recipeTitle)){
id: uid
title: Recipe.title
}
}
This works. However, if I don’t give the query a $difficulty or $recipeTitle, it will error. And if I leave these fields blank it will return an empty array. Is there a way I can change my query to allow me to search for recipes and filter the recipes whether I enter both a title and a difficulty or leave one empty.
One of my solutions is to create a query for all the cases. A query for only filtering by title, only filtering by difficulty and filtering by both difficulty and title. But what if I want to add a ‘cuisine’ field on recipe to filter by. In that case I would have to add more queries for each case and this will become really cumbersome.
(1) How must I change my query to allow me to get filtered results whether I provide all fields or leave one blank.
(2) And if this is possible should I do this or should I rather be using a search Database for this use case.
Thank you in advance