79528497

Date: 2025-03-23 05:30:40
Score: 1
Natty:
Report link

I'm not sure if you are facing the same problem as I did, but I had this issue when I was trying to use Ulid type for IDs.

After an hour of debugging I found out that HotChocolate automatically creates lots of Id serializers, but they do not provide Ulid ID serializer.

But you can register yours:

#1 Create class

using System.Diagnostics.CodeAnalysis;

namespace SocialMediaMonitor.GraphQL.Types;

internal sealed class UlidNodeIdValueSerializer: INodeIdValueSerializer
{
    public bool IsSupported(Type type) => type == typeof(Ulid) || type == typeof(Ulid?);

    public NodeIdFormatterResult Format(Span<byte> buffer, object value, out int written)
    {
        if (value is Ulid u)
        {
            return System.Text.Encoding.UTF8.TryGetBytes(u.ToString(), buffer, out written)
                ? NodeIdFormatterResult.Success
                : NodeIdFormatterResult.BufferTooSmall;
        }

        written = 0;
        return NodeIdFormatterResult.InvalidValue;
    }

    public bool TryParse(ReadOnlySpan<byte> buffer, [NotNullWhen(true)] out object? value)
    {
        var conversion = Ulid.TryParse(buffer, out var result);
        value = result;
        return conversion;
    }
}

#2 Register it

builder.Services.AddSingleton<INodeIdValueSerializer>(sp => new UlidNodeIdValueSerializer());

Hope it will help!

Reasons:
  • Whitelisted phrase (-1): Hope it will help
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): facing the same problem
  • Low reputation (1):
Posted by: Kirill Makhonin