Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the object ID in GraphQL

I'd like to know whether it is possible to get the "original id" of an object as the result of the query. Whenever I make a request to the server, it returns the node "global identifier", something like U29saWNpdGFjYW9UeXBlOjEzNTkxOA== .

The query is similar to this one:

{
  allPatients(active: true) {
    edges {
      cursor
      node {
        id
        state
        name
      }
    }
  }

and the return is:

{
  "data": {
      "edges": [
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjA=",
          "node": {
            "id": "U29saWNpdGFjYW9UeXBlOjEzNTkxOA==",
            "state": "ARI",
            "name": "Brad"
          }
        }
      ]
  }
}

How can I get the "original" id of the object at the database level (e.g. '112') instead of that node unique identifier?

ps.: I am using graphene-python and Relay on the server side.

like image 641
almanegra Avatar asked Sep 29 '17 13:09

almanegra


People also ask

What is ID in GraphQL?

ID : The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

How do you return an object in GraphQL?

In other words, if you return an empty object ( {} ), an empty array ( [] ) or some other value, GraphQL will treat this as you returning an object and not a null value!

How do I get data from GraphQL?

You can fetch data very simply with the help of the package graphql-request . GraphQL Request is a library that doesn't require you to set up a client or a Provider component. It is essentially a function that just accepts an endpoint and a query.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).


1 Answers

Overriding default to_global_id method in Node object worked out for me:

class CustomNode(graphene.Node):
    class Meta:
        name = 'Node'

    @staticmethod
    def to_global_id(type, id):
        return id


 class ExampleType(DjangoObjectType):
     class Meta:
         model = Example
         interfaces = (CustomNode,)
like image 101
alexxmagpie Avatar answered Sep 22 '22 07:09

alexxmagpie