Sub topic 8
Connecting entities with @graph
This chapter assumes you can write a JSON-LD object with @context and @type, nest one entity inside another, and give an entity an @id. Here you will put several top-level entities in one block and link them by reference.
The problem with deep nesting
Nesting works well for a single relationship, such as an Article with its author. A page usually describes more than that. An article has a publisher, the publisher owns a website, and the website contains the article. If you nest all of this, the same Organization details appear in several places. Copies drift apart when you edit one and forget the other.
What @graph does
@graph holds an array of entities that sit side by side at the top level. Each one is a separate node. You declare @context once, outside the array, and it applies to every node inside.
An entity inside @graph refers to another by writing an object that contains only @id. A parser treats that reference as a pointer to the node with the same identifier.
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "Organization",
"@id": "https://example.com/#organization",
"name": "Example Coffee Co.",
"url": "https://example.com/"
},
{
"@type": "WebSite",
"@id": "https://example.com/#website",
"url": "https://example.com/",
"publisher": { "@id": "https://example.com/#organization" }
},
{
"@type": "Article",
"@id": "https://example.com/cold-coffee#article",
"headline": "How to Brew Cold Coffee",
"isPartOf": { "@id": "https://example.com/#website" },
"publisher": { "@id": "https://example.com/#organization" }
}
]
}The Organization is defined once. The WebSite and the Article both point to it with { "@id": ... }. The Article also points to the WebSite through isPartOf.
Rules for reliable references
- Use a full URL as the
@id. A fragment such as#organizationgives you a stable name that does not collide with the page URL itself. - Keep the string identical everywhere.
https://example.com/#organizationandhttps://example.com#organizationare different identifiers. - Define the full details of an entity in one place only. Other nodes carry just the
@id. - Give each node its own
@type. A reference object has no@typebecause it only points.
Common errors
A reference to an @id that no node defines is not an error in the JSON, but it leaves a dangling link. Consumers cannot find the details you meant to share. Check that every referenced identifier appears as a defined node in the same block or on the same page.
Another mistake is placing @context inside each node of the array. It is valid, but it repeats work and invites mismatches. Declare it once at the top.
Finally, @graph does not make entities relate to each other. Two nodes in the same array are unrelated until a property such as publisher or isPartOf connects them.
Exercise
In a @graph, an Article has "publisher": { "@id": "https://example.com/#org" }, but the Organization node has "@id": "https://example.com#org". What goes wrong, and how do you fix it?
Show answer
The two identifiers differ by a slash, so they name different nodes. The Article's publisher reference points to nothing that is defined. Fix it by making the strings identical, for example both "https://example.com/#org".
Exercise
Why declare "@context" once next to "@graph" instead of inside every node?
Show answer
One declaration applies to all nodes in the array. It avoids repetition and removes the chance that nodes end up with different contexts.