Javascript Graphql筛选器查询结果
我是新使用graphql的,我想改进API的一些特性,其中一个是获得更好的过滤器。 此API应根据用户将在相应应用程序中告知的成分返回一些配方,这是我正在使用的解析器:Javascript Graphql筛选器查询结果,javascript,graphql,apollo,Javascript,Graphql,Apollo,我是新使用graphql的,我想改进API的一些特性,其中一个是获得更好的过滤器。 此API应根据用户将在相应应用程序中告知的成分返回一些配方,这是我正在使用的解析器: module.exports = { Recipe: { async ingredients(recipe, _, { dataSources }) { return await dataSources.IngredientService.getRecipeIngredients(recipe.id)
module.exports = {
Recipe: {
async ingredients(recipe, _, { dataSources }) {
return await dataSources.IngredientService.getRecipeIngredients(recipe.id)
},
},
Query: {
recipe: async () => db('Recipe'),
ingredient: async () => db('Ingredient'),
recipeByIngredient:async () => db('Recipe'),
}}
服务
class ingredientService {
async getRecipeIngredients(recipe_id) {
const filteredRecipe = db('Ingredient')
.where({ recipe_id })
.join('Recipe', 'Recipe.id', '=', recipe_id)
.select('Recipe.*', 'Ingredient.*')
.whereIn('Ingredient.name', ["sal", "açucar"])
return await filteredRecipe
}
查询模式
type Query {
recipe(name:[String]): [Recipe]
ingredient:[Ingredients]
recipeByIngredient(ingredients:String):[Ingredients]
}
type Recipe {
id: Int
title: String!
author: String
link: String
category: String
subcategory:String
ingredients:[Ingredients]
}
type Ingredients{
id:Int
name:String
quantity:Float
measure:String
observation:String
}
过滤器正在工作,但我想改进两件事:
当我在graphql游乐场中看到返回号时,当配方中不同表中的配料没有值时,配料值为空,我甚至不想返回配方。
我无法使过滤器工作。例如,我创建了查询类型recipename:[String]:[Recipe],但我不知道如何从那里过滤它。这意味着,我想过滤我的查询,过滤结果的预期
查询:
recipename:[sal,açucar,farinha]{
身份证件
标题
著者
链接
类别
子类别
配料{
名称
量
测量
观察
}
}
但正如您所看到的,解析程序是硬代码,如何将筛选器发送到解析程序
有人能帮我吗?
非常感谢。一般来说,为了处理过滤,我设置了一个条件类型,名为“基于上下文”。在这里,您可能希望传递一个RecipeCondition类型,它定义了一些字段来有效地过滤或限定返回的配方,例如,基于数据存储中是否有成分。这假设您将来可能需要添加其他条件,否则,可能只是在sql中硬编码条件
type RecipeCondition {
ingredientsRequired: Bool
// Add other condition fields here as needed
...
}
type Query {
recipe(name:[String], condition: RecipeCondition): [Recipe]
...
}
我将在顶层处理过滤器,在那里您最初使用db服务获取配方,而不是在subresolver中处理配料。您可以简单地使用此条件(可在recipe resolver arg上访问),并将其传递给最初获取recipes数组的db service func。如果条件IngRequired为true,则适当使用sql进行筛选将需要连接到配料表,其中条件-如果传递配方名称数组,则可能需要迭代完成。这样,假设条件是所需的,没有配料的配方甚至不会命中配料子解析器。一般来说,为了处理筛选,我设置了创建一个条件类型,名为“基于上下文”。在这里,您可能希望传递一个RecipeCondition类型,它定义了一些字段来有效地过滤或限定返回的配方,例如,基于数据存储中是否有成分。这假设您将来可能需要添加其他条件,否则,可能只是在sql中硬编码条件
type RecipeCondition {
ingredientsRequired: Bool
// Add other condition fields here as needed
...
}
type Query {
recipe(name:[String], condition: RecipeCondition): [Recipe]
...
}
我将在顶层处理过滤器,在那里您最初使用db服务获取配方,而不是在subresolver中处理配料。您可以简单地使用此条件(可在recipe resolver arg上访问),并将其传递给最初获取recipes数组的db service func。如果条件IngRequired为true,则适当使用sql进行筛选将需要连接到配料表,其中条件-如果传递配方名称数组,则可能需要迭代完成。这样,假设条件是理想的,不含任何成分的配方甚至不会击中成分分解器。感谢所有试图提供帮助的人,所有这些评论对于指导最终答案非常重要。 我有一个可行的解决方案,如果可行的话,我想与大家分享并得到你们的反馈 首先,我改进了查询解析器
Query: {
recipe(obj, {name}, {dataSources}, info) {
if (name) {
return dataSources.IngredientService.getIngredientsByName(name)
} else {
return db('Recipe')
}
}
第二,我更改了服务以接收筛选器
async getIngredientsByName(name) {
const filteredRecipe = db('Ingredient')
//.where({ recipe_id })
.join('Recipe', 'Recipe.id', '=', 'Ingredient.recipe_id')
.select('Recipe.*', 'Ingredient.name', 'Ingredient.quantity', 'Ingredient.measure','Ingredient.observation')
.whereIn('Ingredient.name', name)
return await filteredRecipe
现在一切都正常了,过滤器的性能也达到了预期
再次,非常感谢大家。感谢所有试图提供帮助的人,所有这些评论对于指导最终答案非常重要。 我有一个可行的解决方案,如果可行的话,我想与大家分享并得到你们的反馈 首先,我改进了查询解析器
Query: {
recipe(obj, {name}, {dataSources}, info) {
if (name) {
return dataSources.IngredientService.getIngredientsByName(name)
} else {
return db('Recipe')
}
}
第二,我更改了服务以接收筛选器
async getIngredientsByName(name) {
const filteredRecipe = db('Ingredient')
//.where({ recipe_id })
.join('Recipe', 'Recipe.id', '=', 'Ingredient.recipe_id')
.select('Recipe.*', 'Ingredient.name', 'Ingredient.quantity', 'Ingredient.measure','Ingredient.observation')
.whereIn('Ingredient.name', name)
return await filteredRecipe
现在一切都正常了,过滤器的性能也达到了预期
再次感谢大家。忘记你们的服务吧。。。看看解析器。。。没有任何过滤。。。您不知道如何将参数传递给解析器第二个参数。。。[再次!!]搜索教程!忘记你的服务吧。。。看看解析器。。。没有任何过滤。。。您不知道如何将参数传递给解析器第二个参数。。。[再次!!]搜索教程!