GraphQL Long scalar type

Running a mutation like the following:

mutation SomeMutation {
  insertOneFoo(data: {
    quantity: 12

Where quantity in my schema is defined as bsonType long will result in the value being silently converted to a string which will no longer be valid when attempting to query or sync from other clients. This is not a problem with the sync clients (realm-cocoa at least).

Edit: I guess this is just a limitation of sending 64bit ints over json, I’ll try and figure out a workaround.

Hey Theo - thanks for bringing this to our attention. Can you use the integer type for now?

As for the reason why we ask users to pass in a string for the long type, it is because different languages have different limitations around long and we want each client to be able to cast the returned string from GraphQL the way it best suits their use-case.

However, the fact that it is getting cast to a String in MongoDB is an issue that we are working on resolving, so that the data in your collections respects your JSON schema - I will follow up on when this is fixed.


Thank you for your response and help @Sumedha_Mehta1, the reasons make sense and I didn’t think of that when initially asking the question.

The thing is that my local sync client’s schema (realm-cocoa) is defined as Int32 so I’m unsure why it was mapped to a long on the server-side, this might be my fault though, maybe at some point, I manually set it to that. What would be the best way to convert this value to an integer? Add a new field?

The Realm Database stores all integers as int64, regardless of what the SDK model uses. The SDK handles the downcasting/upcasting internally, so for the most part this should be transparent to the developer. The exception, as you have seen, is when the schema gets synchronized - when developer mode is enabled, the server side schema that gets generated doesn’t use the swift models but instead uses the database schema (thus using the int64 type). It is safe to manually specify integer in the server schema - Realm sync will correctly downcast/upcast it.


Thanks for the explanation @nirinchev, appreciate it. So I should be able to just specify integer on the sync server schema config? I assume this will trigger a sync restart? Do I also need to run an update in the MongoDB shell to change the type of these to integer?

If you have non-integer values for that field in the database, you’ll need to convert them manually, yes. And yes, changing the type will require reinitializing sync.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.