Graphs | schema-org-rust | Spatie       

 SPATIE  

schema-org-rust
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Schema-org-rust](https://spatie.be/docs/schema-org-rust/v1)  Graphs

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/schema-org-rust/v1) 

  Graphs    
- [ Introduction ](https://spatie.be/docs/schema-org-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/schema-org-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/schema-org-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/schema-org-rust/v1/usage)
- [ Graphs ](https://spatie.be/docs/schema-org-rust/v1/graphs)
- [ Multi-typed entities ](https://spatie.be/docs/schema-org-rust/v1/multi-typed-entities)
- [ Generating types ](https://spatie.be/docs/schema-org-rust/v1/generating-types)
- [ Testing ](https://spatie.be/docs/schema-org-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/schema-org-rust/v1/changelog)

 Graphs
======

###  On this page 

1. [ Several nodes of the same type ](#content-several-nodes-of-the-same-type)
2. [ Managing nodes ](#content-managing-nodes)
3. [ Custom contexts and CSP ](#content-custom-contexts-and-csp)

A `Graph` collects Schema.org nodes and renders them under one `@context` and `@graph`. Generated accessors get or create a default node. Each accessor returns a shared handle, so you can keep and link nodes without borrowing the graph.

```
use spatie_schema_org::{Graph, JsonLd};

let mut graph = Graph::new();
let company = graph.organization();
graph.product().name("My cool Product").brand(&company);
graph.hide("Organization", None);
graph.organization().name("My awesome Company");

assert_eq!(graph.to_value()["@graph"][0]["brand"]["name"], "My awesome Company");
assert_eq!(graph.to_value()["@graph"].as_array().unwrap().len(), 1);
```

Hidden nodes remain available for nesting or references but do not appear as top-level graph entries. `hide("Person", Some("key"))` hides one node. `hide("Person", None)` hides every node of that type, including future nodes. `show` reverses these operations. Showing a single node after hiding its type keeps the other existing nodes hidden.

Several nodes of the same type
--------------------------------------------------------------------------------------------------------------------------------------------------

Use `get_or_create` with a type name and local key. Generated accessors use the local key `"default"`. Local keys are collection keys, not JSON-LD identifiers; use `identifier` to assign `@id`.

```
use spatie_schema_org::{Graph, JsonLd};

let mut graph = Graph::new();
graph.get_or_create("Person", "freekmurze")?
    .given_name("Freek").family_name("Van der Herten").alternate_name("freekmurze");
graph.get_or_create("Person", "sebastiandedeyne")?
    .given_name("Sebastian").family_name("De Deyne");
graph.get_or_create("Person", "random")?.name("Random Person");
graph.hide("Person", Some("random"));
assert_eq!(graph.to_value()["@graph"].as_array().unwrap().len(), 2);
```

Nodes render grouped by type in first-insertion order, then by local key insertion order. Setting a node again preserves its position.

Managing nodes
--------------------------------------------------------------------------------------------------

`has` checks a type/key pair. `get` returns a shared node handle or `CollectionError::NotFound`. `add` rejects duplicates with `CollectionError::AlreadyExists`; `set` inserts or replaces. `remove` deletes a node. `get_nodes` returns shared handles with their local keys, including hidden nodes.

```
let mut graph = Graph::new();
graph.add(Schema::person().name("Freek"), "freek")?;
assert!(graph.has("Person", "freek"));
graph.get("Person", "freek")?.email("freek@spatie.be");
assert!(graph.remove("Person", "freek").is_some());
```

Unknown names passed to `get_or_create` return `CollectionError::InvalidType`. Custom types can be inserted with `set`. `when` accepts a conditional callback for graph changes; use ordinary Rust blocks for callbacks that need several node handles.

Custom contexts and CSP
-----------------------------------------------------------------------------------------------------------------------------

```
use spatie_schema_org::{Graph, JsonLd};

let mut graph = Graph::with_context(serde_json::json!([
    "https://schema.org", {"@base":"https://spatie.be/"}
]));
graph.set_nonce("your-csp-nonce");
assert_eq!(graph.get_context()[1]["@base"], "https://spatie.be/");
assert!(graph.to_script().contains("nonce=\"your-csp-nonce\""));
```

Graphs support `to_value`, `to_json`, `to_script`, `Display`, and Serde serialization, including empty graphs.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
