Global object identification
Give every object a globally unique ID and a Node interface with node(id) so clients can refetch anything by ID. Built and run on Yoga.
Global object identification
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Global object identification means every object has an `id` that is unique across the whole schema, not just within its table, plus a `node(id: ID!)` query that returns any object by that ID.
- The pattern uses a `Node` interface with one field, `id: ID!`; every refetchable type implements it.
- A common implementation encodes the type and the database id into one opaque string, such as base64 of `Post:10`, and decodes it in `node` to pick the table.
- `node` returns the interface, so the client uses inline fragments (`... on Post`) to select type-specific fields.
- Clients and caches benefit: a normalizing cache can key on `id` safely, and a client can refetch one object without knowing which root field produced it.
The problem: ids that only make sense in context
In the database, posts.id = 10 and authors.id = 10 are different rows. Inside a query, you always know which you mean because of where the field sits. A client that holds just the number 10, in a URL, a cache entry or a notification, cannot ask “what is this?” without also remembering its type and which root field to call.
Two features of GraphQL make this sharper. Clients with normalizing caches need a key that identifies an object across responses. And apps often want to refetch one object after a mutation or on navigation, without replaying the original query. Both want a single string that names any object in the graph.
The pattern
The Global Object Identification specification asks for two things:
interface Node {
id: ID!
}
type Query {
node(id: ID!): Node
}
type Post implements Node { id: ID! title: String! }
type Author implements Node { id: ID! name: String! }
- Every type that can be refetched implements
Node, and itsidis unique across the schema. Query.node(id:)takes any suchidand returns the object.
Think of a library catalogue number that includes the branch code: the number alone tells you which building to walk into.
Building the IDs
A common implementation puts the type in the ID. I encoded Type:databaseId as base64url:
const toGlobalId = (type, id) => Buffer.from(`${type}:${id}`).toString('base64url');
const fromGlobalId = (gid) => {
const [type, id] = Buffer.from(gid, 'base64url').toString().split(':');
return { type, id };
};
Post 10 becomes UG9zdDoxMA, which decodes to Post:10. Clients must treat it as opaque, as with cursors; the encoding is yours to change.
The id field on each type returns the global form, and the node resolver reverses it:
const resolvers = {
Node: { __resolveType: (obj) => obj.__typename },
Post: { id: (p) => toGlobalId('Post', p.id) },
Author: { id: (a) => toGlobalId('Author', a.id) },
Query: {
node: (_, { id }, { db }) => {
const { type, id: rawId } = fromGlobalId(id);
const table = { Post: 'posts', Author: 'authors' }[type];
if (!table) return null;
const row = db.prepare(`SELECT * FROM ${table} WHERE id = ?`).get(rawId);
return row ? { __typename: type, ...row } : null;
},
},
};
The table name comes from a fixed lookup map, never from the decoded string directly, so a forged ID cannot select an arbitrary table. The rawId is still bound as a parameter. __resolveType tells GraphQL which concrete type the returned object is; interfaces explains that hook.
Refetching any object
I ran this against Yoga 6.1.0 and SQLite with the global ID of post 10:
query ($id: ID!) {
node(id: $id) {
__typename
id
... on Post { title author { name } }
... on Author { name }
}
}
{ "data": { "node": { "__typename": "Post", "id": "UG9zdDoxMA",
"title": "Post 10", "author": { "name": "Ada" } } } }
The same query with an author’s global ID returned __typename: "Author" and the author’s name. An ID tagged with an unknown type, Nope:1, returned {"node": null}.
Because Node has only id, inline fragments pick the type-specific fields. The client sends one query shape and gets whatever object the ID names.
Authorization still applies
node is a door to every table you wire into it. It must apply the same permission checks as the typed query fields; otherwise a user who may not call Query.invoice could fetch the same invoice via node(id:). Put access rules in the service layer that both paths use, as suggested in authentication and authorization.
Costs and alternatives
IDs carry the type, so the database id alone is not enough to look up a row. The decoding step is one more thing to get right and test, and a refetch for a post and its author takes two ids. If your clients never refetch by ID and use no normalizing cache, you can skip the pattern. Apollo Client keys its cache on __typename plus id by default, which is already a unique pair; the global ID helps most when you want a single string, such as one passed through a URL or a refetch query.
The ID also gives connections their payoff: a connection’s node field returns objects whose id clients can safely use as cache keys, which is where the Apollo cache picks up.
What the video covers
Baylor Breaks It Down’s “GraphQL Tutorial - Global Object Identification” is a 17-minute video, and the title names exactly this topic. It is about nine years old, from a small channel, and I could not read its description or a transcript, so I cannot say which server library or what depth it uses. It is the weakest pick in this group, chosen because I found nothing newer that centres on the pattern. The Apollo entity-caching video and the specification linked below are better complements.
Where this fits
You can now name any object in the graph with one string. The last piece of schema design is the one that decides how long all these names stay valid: changing the schema once clients depend on it. Next: evolving a schema without versions.
Check yourself
Why is a plain database id like `10` not a global ID?
Post 10 and Author 10 can both exist, so `10` alone does not say which object it names. A global ID includes the type or is otherwise unique across the schema.
What does `node(id: ID!): Node` return when the ID names a type the server does not know?
Null in my implementation, since the type tag decoded from the ID matched no table. A missing object is null because the field is nullable.
Why does a query on `node` need inline fragments?
`node` returns the `Node` interface, which has only `id`. To read `title` or `name` the client writes `... on Post { title }` or `... on Author { name }`.
What does a normalizing client cache gain from global IDs?
It can store each object once under its `id` and update every view that shows it, with no collision between types that share a numeric key.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.