首页 > 解决方案 > 基于嵌套对象过滤查询

问题描述

我正在寻找一种基于嵌套字段过滤查询的解决方案。

我收集了一些食品卡车,有很多预订。每个预订都有一个位置、一个食品卡车和一个时间表。

因此,我想获得所有有特定位置和时间表的食品卡车。

我试试这个:

  const query = qs.stringify({
    _where: {
      reservations: [
        { location: "60be40f06a15ab675c91f415" },
        { schedule: "60d0cd9a9f4886438c90b932" },
      ],
    },
  });

  //Get foodtrucks by user with Axios
  const { data:foodtrucksSSP = await getAxiosAPI(`/foodtrucks?${query}`);

但它什么也没返回。

和这个 :

  const qs = require("qs");
  const query = qs.stringify({
    _where: [
      { "reservations.location": "60be40f06a15ab675c91f415" },
      { "reservations.schedule": "60d0cd9a9f4886438c90b932" },
    ],
  });

  //Get foodtrucks by user with Axios
  const { data: foodtrucksSSP } = await getAxiosAPI(`/foodtrucks?${query}`);

但它会返回每辆预订了位置或时间表的食品卡车,而不仅仅是一个。

我尝试使用graphql,但我遇到了同样的问题......

query FoodtrucksReservationsLocation($location: ID!, $date: String!) {
    foodtrucks(
      where: {
        _where: [
          {
            reservations: {
              location: { id: $location }
              schedule: { scheduleDate: $date }
            }
          }
        ]
      }
    ) {
      title
    }
  }

难道我做错了什么 ?

我可以让我的查询按预期工作的唯一方法是首先查询预订:

query FoodtruckyByReservation($location: ID!, $date: String!) {
    reservations(
      where: { location: $location, schedule: { scheduleDate: $date } }
    ) {
      location {
        title
        id
      }
      schedule {
        scheduleDate
        id
      }
      foodtruck {
        ...foodtruckDetail
      }
    }
  }

可以吗 ?

标签: graphqlstrapinested-queries

解决方案


推荐阅读