Custom wp-query in wordpress rest api

You can create a route for Rest API using the below code. Simply put this code in your function.php file

/* Route For api */
add_action( 'rest_api_init', function () {
    register_rest_route('wp/v2', 'test', array(
        'methods' => array('GET', 'POST'),
        'callback' => 'test',
    ));
} );

And then simply create a callback function test and put your stuff in to that function

function test(){
  // put your stuff here
}