• Resolved kirtiraj

    (@kirtiraj)


    In wpgraphql ide, using ‘after’ key in date_query returns queried data/value on locally but the same query used on server it returns null data.
    Also, other that date_query ‘after’ key, all the queries are working fine both locally and on live server.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jason Bahl

    (@jasonbahl)

    I was able to query your server with the following query:

    
    {
      posts(where: {dateQuery: {after: {year: 2021, month: 10}}}) {
        nodes {
          id
          title
          uri
          link
          date
          title
          status
        }
      }
    }
    

    And I got the following response:

    
    {
      "data": {
        "posts": {
          "nodes": [
            {
              "id": "cG9zdDo3NTAz",
              "title": "TEST POST",
              "uri": "/test-post/",
              "link": "https://pan02.wpengine.com/test-post/",
              "date": "2021-11-08T13:47:52",
              "status": "publish"
            }
          ]
        }
      }
    }
    

    I was able to change the month to 7, and get more posts as well.

    Can you paste the query you were trying? From what I can tell, things are working.

    Thread Starter kirtiraj

    (@kirtiraj)

    Also I’m using popular posts functionality code from your site(https://www.wpgraphql.com/recipes/popular-posts/) and when I modify(tries to add date_query’s ‘after’ key) the query into WP_Query args it doesn’t work.
    I want to show popular posts from last 30 days.
    As I said the query in this code works locally but doesn’t work on live servers.

    Plugin Author Jason Bahl

    (@jasonbahl)

    Ah, so that snippet sets the query args explicitly as:

    
    new WP_Query( [
      'posts_per_page' => $per_page,
      'meta_key' => 'wpb_post_views_count',
      'orderby' => 'meta_value_num',
      'order' => 'DESC',
      'fields' => 'ids', // Just ask for the IDs. WPGraphQL connection resolver will get the full objects for you
      'no_found_rows' => true,
    ] );
    

    The arguments from the query aren’t respected in this snippet.

    The snippet should probably be refactored to:

    
    add_action( 'graphql_register_types', function() {
    
    	register_graphql_connection([
    		'fromType' => 'RootQuery',
    		'toType' => 'Post',
    		'fromFieldName' => 'popularPosts',
    		'connectionTypeName' => 'RootQueryToPopularPostsConnection',
    		'resolve' => function( $root, $args, \WPGraphQL\AppContext $context, $info ) {
    
    			$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $root, $args, $context, $info );
    			
    			
    			// Note, these args will override anything the user passes in as { where: { ... } } args in the GraphQL Query
    			$resolver->set_query_arg( 'meta_key', 'wpb_post_views_count' );
    			$resolver->set_query_arg( 'orderby', 'meta_value_num' );
    			$resolver->set_query_arg( 'order', 'DESC' );
    			
    			return $resolver->get_connection();
    		}
    	]);
    
    } );

    I didn’t test this, but I believe it should work the same, but still respect the date query args.

    Please report back!

    • This reply was modified 3 years, 4 months ago by Jason Bahl.
    • This reply was modified 3 years, 4 months ago by Jason Bahl.
    • This reply was modified 3 years, 4 months ago by Jason Bahl.
    Plugin Author Jason Bahl

    (@jasonbahl)

    Ok, I tested this now with the following snippet:

    
    

    add_action( ‘graphql_register_types’, function() {

    register_graphql_connection([
    ‘fromType’ => ‘RootQuery’,
    ‘toType’ => ‘Post’,
    ‘fromFieldName’ => ‘popularPosts’,
    ‘connectionTypeName’ => ‘RootQueryToPopularPostsConnection’,
    ‘connectionArgs’ => \WPGraphQL\Connection\PostObjects::get_connection_args(),
    ‘resolve’ => function( $root, $args, \WPGraphQL\AppContext $context, $info ) {

    $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $root, $args, $context, $info );

    // Note, these args will override anything the user passes in as { where: { … } } args in the GraphQL Query
    $resolver->set_query_arg( ‘meta_key’, ‘wpb_post_views_count’ );
    $resolver->set_query_arg( ‘orderby’, ‘meta_value_num’ );
    $resolver->set_query_arg( ‘order’, ‘DESC’ );

    return $resolver->get_connection();
    }
    ]);

    register_graphql_field( ‘Post’, ‘viewCount’, [
    ‘type’ => ‘Int’,
    ‘resolve’ => function( $post ) {
    return get_post_meta( $post->databaseId, ‘wpb_post_views_count’, true );
    }
    ] );

    } );`
    `

    And we can see this working:

    View post on imgur.com

    Plugin Author Jason Bahl

    (@jasonbahl)

    I updated the snippet in the docs at https://www.wpgraphql.com/recipes/popular-posts/

    Thread Starter kirtiraj

    (@kirtiraj)

    OK thank you!!!

    Thread Starter kirtiraj

    (@kirtiraj)

    Also, how can we do the same thing for custom post type?
    I tried modifying this code for custom post type ‘policy’ as

    register_graphql_field( ‘policy’, ‘viewCount’, [
    ‘type’ => ‘Int’,
    ‘resolve’ => function( $post ) {
    return get_post_meta( $post->databaseId, ‘wpb_post_views_count’, true );
    }
    ] );
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wpgraphql ‘after’ key in date_query issue on server’ is closed to new replies.