Mongodb Realm Sync GraphQL compatibility

I have a class defined in C# project with the following properties:

    public string Type { get; set; }
    public bool IsFatal { get; set; } = false;
    public string Message { get; set; }
    public DateTimeOffset? EffectiveFrom { get; set; }
        public string CreatedBy { get; set; } 
        public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.Now;
        public string UpdatedBy { get; set; 
        public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.Now;

The objects are successfully synced to mongodb, e.g:


note that “IsFatal” is missing if it is false
and EffectiveFrom is null

They can be queried by GraphiQL in Realm UI. But when I access it through Apollo Angular client, I get the following error message:

  1. message: “reason=“could not validate document: \n\t(root): IsFatal is required\n\tEffectiveFrom: Invalid type. Expected: undefined, given: null”; code=“SchemaValidationFailedRead”; untrusted=“read not permitted”; details=map[]”

can this be fixed or is there a way to bypass? Thanks

1 Like

Hey @Sing_Leung - do you mind pasting your JSON schema here?

IsFatal is required

From what I can tell, the workaround here would be to explicitly set isFatal to False because types cannot be nullable according to JSON schema at the moment, or ensure the field is not required in your schema.

EffectiveFrom: Invalid type. Expected: undefined, given: null”

What is the type defined for EffectiveFrom in your JSON Schema? If it is not explicitly “null” this will not work. You will have to make sure this field is not required in this json schema and then not explicitly pass in a “null” value.

This is because GraphQL only currently supports undefined for non-required fields instead of both undefined and null. null is a separate type in MongoDB.

1 Like

Hi @Sumedha_Mehta1
I think the problem is the way it syncs and creates schema for primitive types in dotnet.

if I define the following properties in a RealmObject

public bool A { get; set; }
public int B { get; set; }
public double C { get; set; }

they will be “required” in the generated schema

however, if the values of those fields are zero or false (I’ve tried to explicitly set them in C#), they will be missing in the synced document

so when I tried to use GraphQL, the error occurs.
I think I can workaround the nullable datetime, but I need to store zero or false for numbers and bool.

Hey @Sing_Leung, this is an issue we’re actively working to resolve and hope to have a proper fix soon-ish. In the meantime, you can work around it by explicitly setting the default values after adding the object to the Realm. Something like:

var myObject = ...;


myObject.IsFatal = false;
myObject.IntProperty = 0;
// etc

I realize this is very cumbersome, but should unblock you until the proper solution is released.

1 Like

Thanks @nirinchev
Great to hear that there will be a fix since this is a blocker for me to migrate from Realm Cloud

A post was split to a new topic: Value stores as null if it is not present, but it’s not a required field