0

I need to put current timestamp in mutation query when user cancels the order. It should be on server-side (cause it's not ok to rely on client's data).

Here is a simple mutation in my Laravel Lighthouse GraphQL schema:

type Mutation {
    cancelOrder(id: ID!, canceled_at: DateTime = "2021-02-19 12:00:00"): Order @update
}

How to change this hardcoded timestamp with some sort of now() function result? Like this:

cancelOrder(id: ID!, canceled_at: DateTime = current_timestamp): Order @update
Waldor
  • 31
  • 5
  • You can use `@event` directive to execute Code after `@update` was successful: https://lighthouse-php.com/5.1/api-reference/directives.html#event Alternatively you could probably write some kind of your own middleware-directive, it will be executed before `@update` resolver – lorado Feb 22 '21 at 15:07
  • @lorado Thank! I've written a custom mutator, but `@event` is also an option. I've just thought there should be some built in timestamp resolver. – Waldor Mar 05 '21 at 14:23

1 Answers1

0

I would create a custom mutation like php artisan lighthouse:mutation Order, then on my schema removed the @update and the put the @field(resolver: "App\\GraphQL\\Mutations\\Order@cancel"), then on class Order do like:

namespace App\GraphQL\Mutations;

class Order
{
   /**
   * @param  null  $_
   * @param  array<string, mixed>  $args
   */
   public function cancel($_, array $args)
   {
       $order = Order::findOrFail($args['id']);
       $order->canceled_at = now();
       $order->save();

       return $order;
   }
 }
francisco
  • 1,387
  • 2
  • 12
  • 23
  • I've done the same way! Thank you for your answer! I've just thought there was a more laconic solution. – Waldor Mar 05 '21 at 14:19