Javascript 嵌套查询图
我是一个新使用graphql的人,我想知道如何过滤我的查询以获得在我的输入数组中包含一些配料对象的配方 这是schema.gql文件Javascript 嵌套查询图,javascript,reactjs,graphql,apollo-server,Javascript,Reactjs,Graphql,Apollo Server,我是一个新使用graphql的人,我想知道如何过滤我的查询以获得在我的输入数组中包含一些配料对象的配方 这是schema.gql文件 type Recipe { id: Int title: String! author: String link: String category: String subcategory:String ingredients:[Ingredients] } type Ingredients{ id:Int name:S
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'),
}
这里的主要想法是只需要一个过滤器,就可以看到什么配方有一些成分,用户将通过应用程序通知
我得到了包含数据库中所有配方的“配方”查询,但我需要一个查询来获得这些配方,然后使用字段成分进行过滤,例如:
非常感谢。我得到了一个解决方案,我想与大家分享 我在解析器上实现的过滤器:
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')
}
}
}
使用这个解析器模块,我在“recipe”查询解析器上创建了一个新的过滤器,它接收成分的“name”来生成过滤器,并将其传递给服务以在数据库中实现过滤器
感谢您的支持。筛选是一个解析程序角色。。。f、 e.对于
查询配方(其中:{contains:['Sugar','Vanilla']}){
…请参阅文档/tuts如何使用简单和复杂的参数/变量…稍后您可以引入和/或选项