Graph - multiple items | schema-org-go | Spatie       

 SPATIE  

schema-org-go
=============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Schema-org-go](https://spatie.be/docs/schema-org-go/v1)  Graph - multiple items

 Version   v1      

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

  Graph - multiple items    
- [ Introduction ](https://spatie.be/docs/schema-org-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/schema-org-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/schema-org-go/v1/usage)
- [ Advanced usage ](https://spatie.be/docs/schema-org-go/v1/advanced-usage)
- [ Graph - multiple items ](https://spatie.be/docs/schema-org-go/v1/graphs)
- [ Multi typed entities ](https://spatie.be/docs/schema-org-go/v1/multi-typed-entities)
- [ Generating types ](https://spatie.be/docs/schema-org-go/v1/generating-types)
- [ Testing ](https://spatie.be/docs/schema-org-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/schema-org-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/schema-org-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/schema-org-go/v1/support-us)

 Graph - multiple items
======================

###  On this page 

1. [ Graph node identifiers ](#content-graph-node-identifiers)
2. [ Visibility and context ](#content-visibility-and-context)

The graph is a collection of schemas that can link to each other and prevent helper schemas from being rendered as top-level nodes in the script tag. Generated accessors get an already created or new instance of the requested type.

```
package main

import (
    "fmt"
    schema "github.com/spatie/schema-org-go"
)

func main() {
    graph := schema.NewGraph()
    graph.Product().Name("My cool Product").Brand(graph.Organization())
    graph.Hide("Organization")
    graph.Organization().Name("My awesome Company")

    script, err := graph.ToScript()
    if err != nil {
        panic(err)
    }
    fmt.Println(script)
}
```

The organization remains accessible and appears inside the product even though it is hidden from `@graph`. Nested types remain live until rendering.

Graph node identifiers
--------------------------------------------------------------------------------------------------------------------------

Sometimes you need to keep track of multiple nodes of the same type, for example several people in your organization. Supply a local key to any generated graph accessor.

```
package main

import (
    "fmt"
    schema "github.com/spatie/schema-org-go"
)

func main() {
    graph := schema.NewGraph()
    graph.Person("freekmurze").GivenName("Freek").FamilyName("Van der Herten")
    graph.Person("sebastiandedeyne").GivenName("Sebastian").FamilyName("De Deyne")
    graph.Person("random").Name("Random Person")
    graph.Hide("Person", "random")

    data, err := graph.ToJSON()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(data))
}
```

If you do not provide a key, the reserved key `"default"` is used. These local keys do not become JSON-LD `@id` values. Use `Identifier` on each entity when you need an identifier in the output.

`Add(node, key)` returns `ErrAlreadyExists` when occupied. `Set(node, key)` inserts or replaces without moving an existing node. `Get(typeName, key)` returns the original `schema.Type` or `ErrNotFound`. Use its `Builder()` method for generic fluent property access. `GetOrCreate(typeName, key)` accepts generated vocabulary names and returns `ErrInvalidType` for unknown names. For custom types, use `Add` or `Set`.

`Has` includes hidden nodes. `Remove` removes membership and reports whether the node existed. `GetNodes` returns a copy of membership with live node pointers. Nodes render grouped by type in first-insertion order, and by key insertion order within each type, matching PHP.

Visibility and context
--------------------------------------------------------------------------------------------------------------------------

`Hide(typeName, key)` and `Show(typeName, key)` affect one key, defaulting to `"default"`. `HideAll(typeName)` and `ShowAll(typeName)` affect an entire type. Showing one key after hiding its type keeps the other existing nodes hidden. Nodes created afterward are shown, matching PHP's snapshot behavior. Showing a key before creating it also works.

Use `SetContext` to supply a custom string, context map or array. Nil restores the default `https://schema.org` context. `SetNonce` adds a CSP nonce to script output. `When(condition, func(*schema.Graph))` conditionally changes a graph.

 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)
