Graphql Apollo Server Resolver:字段和嵌套对象的参数集
Graphql Apollo Server Resolver: set of arguments for field and nested object
我正在查看 graphql 文档中的这个示例:
{
human(id: "1000") {
name
height(unit: FOOT)
}
}
所以,我想以这种方式构建我的架构和解析器,这样我就可以查询类似的内容。
我有一份炸玉米饼清单:
const tacos = [
{
cathegory: 'meat',
type: 'Al Pastor',
like: false
},
{
cathegory: 'meat',
type: 'Barbacoa',
like: false
},
{
cathegory: 'meat',
type: 'Chorizo',
like: true
},
{
cathegory: 'fish',
type: 'Camaron',
like: true
},
{
cathegory: 'fish',
type: 'Salmon',
like: false
},
{
cathegory: 'fish',
type: 'Pulpo',
like: false
},
{
cathegory: 'veggi',
type: 'Cauliflower',
like: true
},
{
cathegory: 'veggi',
type: 'Avocado',
like: true
},
{
cathegory: 'veggi',
type: 'Tofu',
like: false
},
]
我的模式非常简单:
const typeDefs = gql`
type Taco {
cathegory: String
type: String
like: Boolean
}
type Query {
tacos(cathegory: String, like: Boolean): [Taco]
}
`;
我设法编写了我的解析器,因此我无法根据分类进行过滤:
const resolvers = {
Query: {
tacos: (parent, args, context, info) => {
return tacos.filter((taco) => taco.cathegory === args.cathegory);
},
},
};
所以我可以查询如下
query xyz {
tacos(cathegory:"veggi" ) {
like
}
}
但是我如何编写我的解析器,以便我可以只查询喜欢的素食炸玉米饼?我对这个话题超级陌生。
像这样:
const resolvers = {
Query: {
tacos: (parent, args, context, info) => {
return tacos.filter((taco) => {
if(Reflect.has(args, 'like')) {
return taco.cathegory === args.cathegory && taco.like === args.like;
}
return taco.cathegory === args.cathegory;
});
},
},
};
我正在查看 graphql 文档中的这个示例:
{
human(id: "1000") {
name
height(unit: FOOT)
}
}
所以,我想以这种方式构建我的架构和解析器,这样我就可以查询类似的内容。 我有一份炸玉米饼清单:
const tacos = [
{
cathegory: 'meat',
type: 'Al Pastor',
like: false
},
{
cathegory: 'meat',
type: 'Barbacoa',
like: false
},
{
cathegory: 'meat',
type: 'Chorizo',
like: true
},
{
cathegory: 'fish',
type: 'Camaron',
like: true
},
{
cathegory: 'fish',
type: 'Salmon',
like: false
},
{
cathegory: 'fish',
type: 'Pulpo',
like: false
},
{
cathegory: 'veggi',
type: 'Cauliflower',
like: true
},
{
cathegory: 'veggi',
type: 'Avocado',
like: true
},
{
cathegory: 'veggi',
type: 'Tofu',
like: false
},
]
我的模式非常简单:
const typeDefs = gql`
type Taco {
cathegory: String
type: String
like: Boolean
}
type Query {
tacos(cathegory: String, like: Boolean): [Taco]
}
`;
我设法编写了我的解析器,因此我无法根据分类进行过滤:
const resolvers = {
Query: {
tacos: (parent, args, context, info) => {
return tacos.filter((taco) => taco.cathegory === args.cathegory);
},
},
};
所以我可以查询如下
query xyz {
tacos(cathegory:"veggi" ) {
like
}
}
但是我如何编写我的解析器,以便我可以只查询喜欢的素食炸玉米饼?我对这个话题超级陌生。
像这样:
const resolvers = {
Query: {
tacos: (parent, args, context, info) => {
return tacos.filter((taco) => {
if(Reflect.has(args, 'like')) {
return taco.cathegory === args.cathegory && taco.like === args.like;
}
return taco.cathegory === args.cathegory;
});
},
},
};