嵌套查询 GraphQl
Nested Query GraphQl
我是 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 应该 return 这两个食谱,但是如果用户通知 Sugar、Honey 和 Four,API 会 return只有选项 1.
有人可以帮我吗?
非常感谢。
我得到了一个解决方案,我想与你分享。
我在解析器上实现的过滤器:
module.exports = {
Recipe: {
ingredients(recipe, _, { dataSources }, info) {
return dataSources.IngredientService.getRecipeIngredients(recipe.id)
}
},
Query: {
recipe(obj, {name}, {dataSources}, info) {
if (name) {
return dataSources.IngredientService.getIngredientsByName(name)
} else {
return db('Recipe')
}
},
ingredient: async () => db('Ingredient'),
recipeByIngredient:async () => db('Recipe'),
}, Mutation: {
createRecipe: async (_, { data }) => await (await db('Recipe').insert(data).returning('*'))[0],
updateRecipe: async (_, { data, id }) => await (await db('Recipe').where({ id }).update(data).returning('*'))[0],
deleteRecipe: async (_, { filter }) => {
if (filter.id) {
return await db('Recipe').where({ id: filter.id }).delete()
}
if (filter.title) {
return await db('Recipe').where({ title: filter.title }).delete()
}
throw new Error('Should provide the ID or TITLE')
}
}
}
使用这个解析器模块,我在“食谱”查询解析器上创建了一个新过滤器,它接收成分的“名称”来制作过滤器并将其传递给服务以在数据库中实施过滤器。
感谢支持
我是 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 应该 return 这两个食谱,但是如果用户通知 Sugar、Honey 和 Four,API 会 return只有选项 1.
有人可以帮我吗?
非常感谢。
我得到了一个解决方案,我想与你分享。
我在解析器上实现的过滤器:
module.exports = {
Recipe: {
ingredients(recipe, _, { dataSources }, info) {
return dataSources.IngredientService.getRecipeIngredients(recipe.id)
}
},
Query: {
recipe(obj, {name}, {dataSources}, info) {
if (name) {
return dataSources.IngredientService.getIngredientsByName(name)
} else {
return db('Recipe')
}
},
ingredient: async () => db('Ingredient'),
recipeByIngredient:async () => db('Recipe'),
}, Mutation: {
createRecipe: async (_, { data }) => await (await db('Recipe').insert(data).returning('*'))[0],
updateRecipe: async (_, { data, id }) => await (await db('Recipe').where({ id }).update(data).returning('*'))[0],
deleteRecipe: async (_, { filter }) => {
if (filter.id) {
return await db('Recipe').where({ id: filter.id }).delete()
}
if (filter.title) {
return await db('Recipe').where({ title: filter.title }).delete()
}
throw new Error('Should provide the ID or TITLE')
}
}
}
使用这个解析器模块,我在“食谱”查询解析器上创建了一个新过滤器,它接收成分的“名称”来制作过滤器并将其传递给服务以在数据库中实施过滤器。
感谢支持