Retrieve the object ID in GraphQL

graphql object type
graphql get all fields
graphql where
graphql map type
graphql subquery
graphql mutation best practices
graphql extend type
graphql union input type

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.

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,)

Global Object Identification, The id field can be extracted out of the response safely, and can be stored for re-​use via caching and refetching. Clients can use interface fragments to extract  GraphQL» Guides» Relay» Object Identification. Object Identification. Relay uses global object identificationto support some of its features: Caching: Unique IDs are used as primary keys in Relay’s client-side cache. Refetching: Relay uses unique IDs to refetch objects when it determines that its cache is stale.

First option, remove relay.Node as interface of your objectNode declaration.

Second option, use custom resolve_id fonction to return id original value.

Example

class objectNode(djangoObjectType):

    .... Meta ....

    id = graphene.Int(source="id")

    def resolve_id("commons args ...."):
        return self.id

Hope it helps

Queries and Mutations, In that case, you can make a sub-selection of fields for that object. GraphQL queries can traverse related objects and their fields, letting clients fetch lots of related  The GraphQL schema is formatted to allow fetching any object via the node field on the root query object. This returns objects which conform to a "Node" interface . The id field can be extracted out of the response safely, and can be stored for re-use via caching and refetching.

To expand on the top answer and for those using SQLAlchemy Object Types, this worked for me:

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

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

class ExampleType(SQLAlchemyObjectType):
    class Meta:
        model = Example
        interfaces = (CustomNode, )

If you have other ObjectTypes using relay.Node as the interface, you will need to use a unique name under your CustomNode. Otherwise you will get and assertion error.

Object Types, In GraphQL schema language, the way you define a new object type is the Instead of doing one API request to get basic information about an object, and then  You can access most objects in GitHub (users, issues, pull requests, etc.) using either the REST API v3 or the GraphQL API v4. With a recent update, you can find the global node ID of many objects from within the REST API and use these IDs in your GraphQL operations. Note: In REST, the global node ID field is named node_id.

Schemas and Types, Character is a GraphQL Object Type, meaning it's a type with some fields. type Starship { id: ID! name: String! length(unit: LengthUnit = METER): Float } Union types are very similar to interfaces, but they don't get to specify any common  A GraphQL object type has a name and fields, but at some point those fields have to resolve to some concrete data. That's where the scalar types come in: they represent the leaves of the query. In the following query, the name and appearsIn fields will resolve to scalar types: {. hero {.

Mutations and Input Types, A common example is that creating an object in a database and updating an object in a input MessageInput { content: String author: String } type Message { id: ID! Here, the mutations return a Message type, so that the client can get more  The GraphQL spec has five predefined scalars: as String, Int, Float, Boolean, and ID. Object types have fields that express the properties of that type and are composable. Examples of object types are the User or Post types we saw in the previous section. In every GraphQL schema, you can define your own scalar and object types.

Object Identification, Refetching: Relay uses unique IDs to refetch objects when it determines that its cache is stale. (It uses the Query.node field to refetch objects.) Defining UUIDs. You  When you are using the Transact-SQL functions DB_ID and OBJECT_ID to return a parameter value, always make sure that a valid ID is returned. If the database or object name cannot be found, such as when they do not exist or are spelled incorrectly, both functions will return NULL.

Comments
  • I got the following error: Node.id expects type "ID!" but xxx.id provides type "Int".