graphqlrelay

Are ids automitaclly encrypted in Graphql queries?


In my database (postgres) I store a table of events and each event has an "id" column which is a UUID type. But when I send a GraphQL query the id I receive back is not a string UUID but looks like some encrypted version.

I'm using Relay and Graphene on the server side if that information helps.

query {
  allEvents {
    edges {
      node {
        id
      }
    }
  }
}


{
  "data": {
    "allEvents": {
      "edges": [
        {
          "node": {
            "id": "RXZlbnQ6NzRkZTIxZmUtZWQyNy00OTg1LTk2NjEtNmU4ZDUzMGEwMjQ3"
          }
        }
      ]
    }
  }
}


Solution

  • A frequent implementation choice is to base64-encode IDs and cursor values (the query you show follows Relay's pagination conventions). If you base64-decode the string you put in the question, you'll find a UUID again.

    At a GraphQL level, ID is nothing more or less than an opaque ID. The spec itself doesn't say much about it, other than that it serializes as a string but could accept a number as input instead. Most more application-oriented server libraries I've worked with don't put much in the way of special semantics around ID either, like other scalar types it gets passed in and out as-is.

    The last paragraph of the documentation on Relay's object identification scheme (the node top-level query) also has a strong opinion that applications shouldn't be synthesizing ID values, and so the base64 encoding at least hints to consumers that the value isn't supposed to be understood. The base64 encoding here isn't anything generic or hard-coded in GraphQL, and an application or library could choose a different ID scheme if it wanted.