> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Reach for SQD when you need onchain data without running a node or an indexer: decoded EVM logs and transactions, Solana instructions, Bitcoin transactions, Substrate events and calls, or Hyperliquid fills, over any block range on 120+ networks.
> To query directly, POST to https://portal.sqd.dev/datasets/{dataset}/stream. The full API is described at https://docs.sqd.dev/openapi.json, and responses to the stream endpoints are JSON Lines.
> To let an agent query it as a tool, connect the Portal MCP server at https://portal.sqd.dev/mcp.
> Every page on this site is available as Markdown by appending .md to its URL.

# Unions and typed JSON

> Model Squid SDK union types and typed JSON fields in schema.graphql.

Complex scalar types can be modelled using a typed JSON fields together with union types, making safe union types.

## Typed JSON

It is possible to define explicit types for JSON fields. The generated entity classes and the GraphQL API will respect the type definition of the field, enforcing the data integrity.

**Example**

```graphql theme={"system"}
type Entity @entity {
  a: A
}

type A {
  a: String
  b: B
  c: JSON
}

type B {
  a: A
  b: String
  e: Entity
}
```

## Union types

One can leverage union types supported both by [Typescript](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) and [GraphQL](https://graphql.org/learn/schema/#union-types).  The union operator for `schema.graphql` supports only non-entity types, including typed JSON types described above. JSON types, however, are allowed to reference an entity type.

**Example**

```graphql theme={"system"}
type User @entity {
  id: ID!
  login: String!
}
        
type Farmer {
  user: User!
  crop: Int
}

type Degen {
  user: User!
  bag: String
}
        
union Owner = Farmer | Degen
        
type NFT @entity {
  name: String!
  owner: Owner!
}
```


## Related topics

- [Unions and typed JSON](/en/sdk/squid-sdk/substrate/reference/schema-files/unions-typed-json.md)
- [OpenReader Overview](/en/sdk/squid-sdk/substrate/reference/openreader/overview.md)
- [JSON Queries](/en/sdk/squid-sdk/tron/reference/openreader/api/json-queries.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.