the-events-calendar / ql-events

The Events Calendar binding to WPGraphQL
15 stars 7 forks source link

Event query where condition not working #32

Open toriphes opened 3 years ago

toriphes commented 3 years ago

Hi, thank you for this great plugin.

I am using this plugin for a personal project and have found that event queries containing where condition are not working as expected.

For example this query should return no results because I have not created events starting after the year 4000 and instead returns all events from now.

query getAppointments() {
  events(where: {startDateQuery: {after: {year: 4000}}}) {
    nodes {
      title
      startDate
      endDate
    }
  }
}

I have seen that the problem is caused by these commented lines: https://github.com/the-events-calendar/ql-events/blob/master/includes/data/connection/class-event-connection-resolver.php#L46-L48

Is this a mistake?

As a workaround I wrote this code in my theme:

add_filter( 'graphql_' . Tribe__Events__Main::POSTTYPE . '_connection_query_args', function (
        $query_args,
        $source,
        $args
    ) {
    $where = $args['where'];

    if ( ! isset( $query_args['meta_query'] ) ) {
        $query_args['meta_query'] = array(); // WPCS: slow query ok.
    }

    if ( ! empty( $where['startDateQuery'] ) ) {
        $query_args['meta_query'][] = WPGraphQL\QL_Events\Data\Connection\Event_Connection_Resolver::date_query_input_to_meta_query( $where['startDateQuery'],
            '_EventStartDate' );
    }

    if ( ! empty( $where['endDateQuery'] ) ) {
        $query_args['meta_query'][] = WPGraphQL\QL_Events\Data\Connection\Event_Connection_Resolver::date_query_input_to_meta_query( $where['endDateQuery'],
            '_EventEndDate' );
    }

    if ( ! empty( $where['venuesIn'] ) ) {
        $query_args['meta_query'][] = array(
            'key'     => '_EventVenueID',
            'value'   => $where['venuesIn'],
            'compare' => 'IN',
        );
    }

    if ( ! empty( $where['venuesNotIn'] ) ) {

        $query_args['meta_query'][] = array(
            'key'     => '_EventVenueID',
            'value'   => $where['venuesNotIn'],
            'compare' => 'NOT IN',
        );
    }

    return $query_args;
}, 11, 3 );