the-events-calendar / ql-events

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

Querying events by endDateQuery{ before } returns empty. #19

Open justlevine opened 3 years ago

justlevine commented 3 years ago

Trying to query past events, but its returning an empty array. Query:

{
  events(where: {endDateQuery: {before: {year: 2020}}}) {
    nodes {
      title
      startDate
    }
  }
}

Response:

{
  "data": {
    "events": {
      "nodes": []
    }
  },
  "extensions": {
    "debug": []
  }
}

using startDateQuery works fine.

Here's where it gets a bit weird: if I grab the query_args and do a WP_Query, it also doesn't work:

$query = new \WP_Query(
    [
        'post_type' => [ 'tribe_events' ],
        'no_found_rows' => true,
        'posts_per_page' => '11',
        'endDateQuery' => [ 'before' => [ 'year' => '2020' ] ],
        'meta_query' => [
            [
                'key' => '_EventEndDate',
                'value' => '2020-12-02',
                'compare' => '<',
            'type' => 'DATE',
            ],
        ],
    ]
);

but if I change it to post_type => 'tribe_events instead of an array it does return the events.

Not sure if this is an issue with TEC (that post_type should accept an array), or with ql-events (that it should be passing the post_type as a string).

craigwilcox commented 3 years ago

Hi @justlevine,

@kidunot89 has provided an update we haven't merged yet. It appears to solve this problem. Please try with this branch: https://github.com/simplur/ql-events/tree/bugfix/wp-graphql-v0.4.0-and-woographql-v0.3.0-support.

Thanks, Craig

justlevine commented 3 years ago

No dice with that branch (which btw says it was merged ).

I'm thinking this is a bug with TEC, since it's not just that startDateQuery works in GQL, but using an array for post_type in WP_Query when the meta query key is _EventStartDate also works as expected.

justlevine commented 3 years ago

Seems to be a known quirk with TEC (https://wordpress.org/support/topic/bug-using-wp_query-to-get-past-events-doesnt-work-if-post_type-is-an-array/#post-13737078) and their recommendation for personal use is to use tribe's ORM.

craigwilcox commented 3 years ago

I was able to get data with the latest TEC plugins, the 1.0 version of WPGraphQL, and QL-Events. It does fail with older versions of each.

  events(where: {endDateQuery: {before: {year: 2020}}}) {
    nodes {
      title
      startDate
    }

And yes, @kidunot89 merged this in a couple hours ago.

justlevine commented 3 years ago

TEC v5.3.0 and redownloading QL-Events did fix it. Closing (and thanks!)

justlevine commented 3 years ago

I spoke too soon. Its returning events, sure, but they're not events that match the endDateQuery.

Annotation 2020-12-03 105258

Seemingly related is while the the endDateQuery => [ 'before' => ['year'=>'2020'] ] is still present, the corresponding meta_query now shows a blank array. Annotation 2020-12-03 105659

craigwilcox commented 3 years ago

Thanks for testing this, @justlevine. It does seem to be an issue with TEC, though, correct? As such, we will need them to fix that.

justlevine commented 3 years ago

Well two separate issues. That WP_Query won't work with an array of post_types and the meta query as _EventEndDate is a TEC issue that their solution seems to be using their ORM instead (feel free to chime in here: https://wordpress.org/support/topic/bug-using-wp_query-to-get-past-events-doesnt-work-if-post_type-is-an-array/#post-13737078).

That endDateQuery isn't being sanitized into meta_query I believe is an issue with the latest QL-Events.

justlevine commented 3 years ago

PS: the source for why the latest master of QL-Events is returning future events is because class-event-connection-resolver.php::get_query_args() is no longer merging $input_fields to $query_args.

When uncommented, its back to returning empty because of the TEC bug.

justlevine commented 3 years ago

I'm not comfortable enough with this codebase to submit a PR, but so far adding the following code to class-event-connection-resolver.php::get_query_args() works around the issue. (There's probably a more elegant approach):

if( ! empty( $query_args['endDateQuery'] ) && [ 'tribe_events' ] === $query_args[ 'post_type' ] ) {
    $query_args[ 'post_type' ] = 'tribe_events';
}

The full function would look like this now:


public static function get_query_args( $query_args, $source, $args, $context, $info ) {
    /**
     * Collect the input_fields and sanitize them to prepare them for sending to the WP_Query
     */
    $input_fields = [];
    if ( ! empty( $args['where'] ) ) {
        $input_fields = self::sanitize_input_fields( $args['where'] );
    }

    /**
     * Merge the input_fields with the default query_args
     */
    if ( ! empty( $input_fields ) ) {
        $query_args = array_merge( $query_args, $input_fields );

        /**
         * If using endDateQuery, make sure the post_type is a string.
         *
         * @see https://github.com/simplur/ql-events/issues/19
         */
        if( ! empty( $query_args['endDateQuery'] ) && [ 'tribe_events' ] === $query_args[ 'post_type' ] ) {
            $query_args[ 'post_type' ] = 'tribe_events';
        }
    }

    return apply_filters(
        'graphql_' . Main::POSTTYPE . '_connection_query_args',
        $query_args,
        $source,
        $args,
        $context,
        $info
    );
}

(Note, I also uncommented the array_merge, but without knowing why @kidunot89 commented it out, it's hard to test for side effects).