Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle json data types? #52

Closed
noah-integrityinspired opened this issue Jun 13, 2023 · 3 comments
Closed

How to handle json data types? #52

noah-integrityinspired opened this issue Jun 13, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@noah-integrityinspired
Copy link

Is your feature request related to a problem? Please describe.
My graphQL has a JSON* field that I would like to handle; but I cannot find a way to do this with ZeroQL; haven't had much luck with custom serializers yet.

*(Technically JSONB; we are using Hasura w/ Postgres)

For example I'd like to turn this data:
{ "name": "john", "address": { "street": 1234, "zip": 45567, } }
and put it into a shape like:

class Person 
{
  public string Name {get; set;}
  public jsonb Address {get; set;}
}

So far we've tried the code below, but we haven't had any success. Was wondering if you had any examples you could share or could help us figure out what we are missing.

Out of the box the code generation creates this scalar in the generated code.

public sealed record jsonb : ZeroQLScalar
{
    public jsonb()
    {
    }

    public jsonb(string value)
    {
        Value = value;
    }

    public static implicit operator jsonb(string value) => new jsonb(value);
    public static implicit operator string (jsonb scalar) => scalar.Value;
}

But we are unable to use it as it chokes on the opening curly brace ('StartObject') during deserialization.

So we have tried to create a custom converter, but we are unable to get any of the Console.WriteLine statements to occur; suggesting it's not registering correctly with ZeroQL?

public class JsonBConverter : JsonConverter<jsonb>
{
    public override jsonb? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        Console.WriteLine("Beginning read...");
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, jsonb value, JsonSerializerOptions options)
    {
        Console.WriteLine("Beginning write...");
        throw new NotImplementedException();
    }
}

public static class Module
{
    [ModuleInitializer]
    public static void Initialize()
    {
        ZeroQLJsonOptions.Configure(o => o.Converters.Add(new JsonBConverter()));
    }
}
@noah-integrityinspired noah-integrityinspired added the enhancement New feature or request label Jun 13, 2023
@byme8
Copy link
Owner

byme8 commented Jun 13, 2023

Have you seen https://github.com/byme8/ZeroQL/wiki/User-scalars?

If you want to create your own serialization for a custom scalar, you need to do the next steps:

  • create "container" for it
// something like that
namespace MyCustom;

public class jsonb : JsonElement
{
}
  • define serializer. You have it
  • tell zeroql generator to replace "stub scalar" with your custom one --scalars jsonb=MyCustom.jsonb

Looks like you skipped the first and the last step.

@noah-integrityinspired
Copy link
Author

noah-integrityinspired commented Jun 14, 2023

Thank you for your patience! We tired doing the first and last step you mentioned, but it did not work in our earlier attempts

But we've figured it out.
To future readers: if you are using the a zeroql.json file, try generating without the the json and use the --scalars arg; it may expose the issue we were dealing with.

What we experienced:

The project we were working in had only installed ZeroQL v4.1. However the project somehow had access to the ZeroQL cli, but only v4.0. And because we are using zeroql.json file, it did not give us any errors when we went to generate.

When we tried generating without the json file, using the --scalars arg, it gave us an error:

Unrecognized option(s):
--scalars

And told us it it was on v4.0.

After installing the ZeroQL.CLI v4.1 tool, the generate command now works correctly and uses the custom class in the generated code.

@byme8
Copy link
Owner

byme8 commented Jun 14, 2023

I suppose I need to add a validation so if some additional properties in zeroql.json are detected but not recognized the generation should fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants