1

Is there a way to inject now datetime into a mutation?

Something like this:

customerUpsert(input: CustomerInput! @spread): Customer
@upsert(model: "App\\Models\\Customer")
@inject(context: ???, name: "customer_since")
Alireza A2F
  • 519
  • 4
  • 26

1 Answers1

0

I've made a Scalar in laravel lighthouse that serves my need:

<?php

namespace App\GraphQL\Scalars;

use GraphQL\Error\Error;
use GraphQL\Type\Definition\ScalarType;

class CustomValue extends ScalarType
{

    public function serialize($value): string
    {
        return $value;
    }

    public function parseValue($value): string
    {
        return $value;
    }


    public function parseLiteral($valueNode, $variables = [])
    {
        $map = [
            "now" => now(),
        ];

        if (!array_key_exists($valueNode->value, $map)) {
            throw new Error(
                "Query error: Invalid custom value",
                $valueNode
            );
        }

        return $map[$valueNode->value];
    }
}

You can add your other values to be set by default if needed. Here is an example of it's usage:

input CustomerInput {
  id: ID
  name: String!
  date: CustomValue = "now"
}
Alireza A2F
  • 519
  • 4
  • 26