Search code examples
graphqlrelaygraphql-java

What is the best way of Query node(id: ID!) implementation with RDBMS


GraphQL and Relay specification requires to have Query type with node(id: ID!) field described and implemented like this:

type Query {
  node(id: ID!): Node
}

Relay want to refetch any object by id. That means server should understand somehow which type of object it should look for. Which table in db to scan. It is clear that id cannot be just serial id or uuid, as in this case it have to scan all tables. That is bad approach.

I came to solution to encode object id along with its type, to be able to understand what table to query. Something like "24|User" encoded in base64 will give unique identifier as "MjR8VXNlcg==". I am pretty sure that this approach will work, but I wonder is there any better way to handle this situation? What are the alternatives?


Solution

  • https://relay.dev/docs/en/graphql-server-specification.html#object-identification gives the answer.

    A system without globally unique IDs can usually synthesize them by combining the type with the type-specific ID...

    So what I did in the question. However they used another format: "TypeName: id".