【发布时间】:2021-01-29 11:42:43
【问题描述】:
我是使用 graphql 的新手,我想知道如何过滤我的查询以获取在我的输入数组中包含一些成分对象的食谱。
这是 schema.gql 文件
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
}
type Query {
recipe: [Recipe]
ingredient:[Ingredients]
}
此配方架构有 1 个相应的服务
const db = require('../db')
class RecipeService{
//PENDENTE FINALIZAR ESSA SERVICE
async getRecipeByIngredient(ingredient)
}
以及相应的查询解析器
Recipe: {
async ingredients(recipe, _, { dataSources }) {
return await dataSources.IngredientService.getRecipeIngredients(recipe.id)
},
},
Query: {
recipe: async () => db('Recipe'),
ingredient: async () => db('Ingredient'),
}
这里的主要想法只是有一个过滤器,可以看到哪些食谱有一些成分,用户将通过 APP 告知。
我得到了包含数据库中所有食谱的“食谱”查询,但我需要一个查询来获取这些食谱并使用字段成分进行过滤,例如:
- 配方 - 糖蛋糕,配料:糖、蜂蜜、四...
- 食谱 - 天鹅绒蛋糕的配料:糖、香草……
如果用户通知 Sugar,API 应该返回这 2 个食谱,但如果用户通知 Sugar、Honey 和 Four,API 将只返回选项 1。
谁能帮帮我?
非常感谢。
【问题讨论】:
-
过滤是一个解析器角色... f.e.对于
query recipes( where: { contains: ['Sugar', 'Vanilla'] } ) {...请参阅文档/tuts 如何使用简单和复杂的参数/变量...稍后您可以引入 AND/OR 选项
标签: javascript reactjs graphql apollo-server