0

when i print the id before returning, the code print the right value ( same with id in mongo ). but the client received a deferent id.

my query code :

def resolve_account(root, info, **kwargs):
    email = kwargs.get('email', None)
    password = kwargs.get('password', None)
    accounts = AccountModel.objects(email=email, password=password)
    if accounts.first() is None:
        return ResponseMessageField(is_success=False, message="Not found")

    print(accounts[0].id)
    return AccountResults(accounts=[AccountField(id=account.id,
                                                 name=account.name)
                                    for account in accounts])

console printed : `5e5f28a41e92b7cdb5cf30ea'

but my client received :

{
  "data": {
    "accountLogin": {
      "accounts": [
        {
          "name": "test1",
          "id": "QWNjb3VudEZpZWxkOjVlNWYyOGE0MWU5MmI3Y2RiNWNmMzBlYQ=="
        }
      ]
    }
  }
}

python 3.6.9 mongoengine 0.1.9 graphene 2.1.8 graphene_mongo 0.1.1 flask 1.1.1

Baltschun Ali
  • 376
  • 3
  • 12

1 Answers1

1

This is acutally an adavantage of graphene-django, if you're using auto-increment ID's.

Anyway it encodes them using base64 encoding, to get the real value, you can do this in vanilla JS:

>> atob('QWNjb3VudEZpZWxkOjVlNWYyOGE0MWU5MmI3Y2RiNWNmMzBlYQ==')
>> "AccountField:5e5f28a41e92b7cdb5cf30ea"

So if you want to mutate something, and you have the ID, which is not bas64 encoded, what you'll have to do is:

>> btoa("AccountField:5e5f28a41e92b7cdb5cf30ea")
>> "QWNjb3VudEZpZWxkOjVlNWYyOGE0MWU5MmI3Y2RiNWNmMzBlYQ==" 

In python, graphene provides an import, to_global_id and from_global_id to convert to and fro b/w base64 encoded values and the real IDs.

frozenOne
  • 558
  • 2
  • 8