import { Scalar, CustomScalar } from '@nestjs/graphql';
import { Kind, ValueNode } from 'graphql';

@Scalar('JSON', () => Object)
export class JSONScalar implements CustomScalar<object, object> {
  description = 'JSON custom scalar type';

  parseValue(value: unknown): object {
    return value as object; // value from the client
  }

  serialize(value: unknown): object {
    return value as object; // value sent to the client
  }

  parseLiteral(ast: ValueNode): object {
    if (ast.kind === Kind.OBJECT) {
      return ast as any;
    }
    return null;
  }
}
