2

I have users table and it has Array relationship with CustomerOrders i need to filter users based on CustomerOrders completion_date. It kind of works but when no date filters selected I need to show all users even those who does not have CustomerOrder if I add condition which ends in empty object {} it still considered as looking at thatCustomerOrder and I still get all users that have CustomerOrder.

My question is how to completely remove CustomerOrders Filter when inputs are empty?

variables

variables: {
        order_byUsers: { creationDate: 'desc' },
        whereUsers: {
          deleted_at: { _is_null: true }
          // CustomerOrders:
          //   fromDate && toDate
          //     ? {
          //         completion_date: {
          //           _gte: fromDate,
          //           _lte: toDate
          //         }
          //       }
          //     : {}
        },
        whereCustomerOrders: {
          completion_date:
            fromDate && toDate
              ? {
                  _gte: fromDate,
                  _lte: toDate
                }
              : {}
        },
        offsetUsers: state.pageSize * state.page,
        limitUsers: state.pageSize
      }

query

query GetUsersAnalysisGqlQuery(
    $whereUsers: User_bool_exp
    $whereCustomerOrders: CustomerOrder_bool_exp
    $order_byUsers: [User_order_by!]
    $limitUsers: Int
    $offsetUsers: Int
  ) {
    User(
      where: $whereUsers
      order_by: $order_byUsers
      limit: $limitUsers
      offset: $offsetUsers
    ) {
      id
      nameSurname
      email
      phone
      description
      blocked
      CustomerOrders(where: $whereCustomerOrders) {
        completion_date
      }
      CustomerOrderData {
        order_count
        amount
      }
    }
    User_aggregate(where: $whereUsers) {
      aggregate {
        count
      }
    }
  }

I also tried adding where variable for CustomerOrders ,but I guess I'm doing something wrong.

1 Answer 1

1

My solution was this function

const getWhereUsersAnalysis = () => {
    let whereUsers: GQL_gen.Schema.User_Bool_Exp = {
      deleted_at: { _is_null: true }
    };

    if (fromDate && toDate) {
      whereUsers = {
        ...whereUsers,
        CustomerOrders: {
          completion_date: {
            _gte: fromDate,
            _lte: toDate
          }
        }
      };
    }

    return whereUsers;
  };

and i used it in variables like this

      variables: {
        order_byUsers: { creationDate: 'desc' },
        whereUsers: getWhereUsersAnalysis(),
        offsetUsers: state.pageSize * state.page,
        limitUsers: state.pageSize
      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.