Create API’s for custom-post types & custom queries using REST or Graphql

Answering questions myself. I hope this will help some folks.

1.How to create REST API for the custom post type

If you’re creating a new custom post type then make sure this option is set to true in your arguments array:

show_in_rest' => true,

If your custom post is from a theme or plugin and you want to enable rest-api for it, then add this code into your child-theme functions.php

/**
 * Add REST API support to an already registered post type.
 */
add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
 
function my_post_type_args( $args, $post_type ) {
 
    if ( 'course' === $post_type ) {
        $args['show_in_rest'] = true;
 
        // Optionally customize the rest_base or rest_controller_class
        $args['rest_base']             = 'course';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
 
    return $args;
}

2.How to create an end-point for a custom query.

Let’s say you want to run some wp-query and needs to send a custom parameter & access that query from rest-API

        add_action('rest_api_init', function () {
          register_rest_route( 'namespace/v2', 'events/(?P<id>\d+)',array(
                        'methods'  => 'GET',
                        'callback' => 'get_events_from_id'
              ));
        });
        
        //Now create a function to return your custom query results
        
        function get_events_from_id($request){
           $id = $request['id']; 
// Custom WP query query
    $args_query = array(
        'post_type' => array('events'),
        'order' => 'DESC',
    );
    
    $query = new WP_Query( $args_query );
    
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            
            $data = //do your stuff like
        }
    } else {
    
    }
    return $data;
    wp_reset_postdata();
        }