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

# Create a cross-chain subgraph

> Merge entities from multiple subgraphs across chains into one table (legacy Mirror subgraph-entity source).

You can use subgraphs as a pipeline source, combining the flexibility of subgraph indexing with the expressiveness of the database of your choice. **You can also push data from *multiple subgraphs* with the same schema into the same sink, merging subgraphs across chains.**

> [!NOTE]
>
> Subgraph-entity sources are a legacy [Mirror](https://docs.goldsky.com/mirror/introduction.md) feature; [Turbo pipelines](https://docs.goldsky.com/turbo-pipelines/introduction.md) don't support them yet. For the single-subgraph version of this flow, see [sync a subgraph to Postgres](https://docs.goldsky.com/mirror/guides/sync-subgraph-to-postgres.md).

## What you'll need

1. One or more subgraphs in your project: a deployed subgraph or a [no-code subgraph](https://docs.goldsky.com/subgraphs/guides/create-a-no-code-subgraph.md).

   > [!NOTE]
   >
   > If more than one subgraph is desired, they need to have the same graphql schema.
   > You can use [this tool](https://www.npmjs.com/package/graphql-schema-diff) to
   > compare schemas.

2. A working database to sink into. PostgreSQL is the most common choice; see [sync a subgraph to Postgres](https://docs.goldsky.com/mirror/guides/sync-subgraph-to-postgres.md).

## Walkthrough

**1. Prepare a database**

`goldsky secret list` will show you the database secrets available on your active project.

If you need to setup a secret, you can use `goldsky secret create -h`. [Here](https://docs.goldsky.com/platform/secrets.md) is an example.

**2. Select the subgraphs to combine**

Open the [Subgraphs Dashboard](https://app.goldsky.com/subgraphs) and find the deployment IDs of each subgraph you would like to use as a source.

Run the following query against the subgraph to get the deployment ID.

```graphql theme={"dark"}
query {
  _meta {
    deployment
  }
}
```

**3. Create the pipeline definition**

Open a text editor create your definition, using the `subgraphEntity` source. In this example we will use subgraphs on Optimism and on BSC:

* `qidao-optimism` (`QmPuXT3poo1T4rS6agZfT51ZZkiN3zQr6n5F2o1v9dRnnr`)
* `qidao-bsc` (`QmWgW69CaTwJYwcSdu36mkXgwWY11RjvX1oMGykrxT3wDS`)

They have the same schema, and we will be syncing the `account` and `event` entities from each.

> [!WARNING]
>
> Entities may be camelCased in the GraphQL API, but here they must be
> snake\_cased. For example, `dailySnapshot` will be `daily_snapshot` here.

```yaml qidao-crosschain.yaml theme={"dark"}
sources:
  - type: subgraphEntity
    # The deployment IDs you gathered above. If you put multiple,
    # they must have the same schema
    deployments:
      - id: QmPuXT3poo1T4rS6agZfT51ZZkiN3zQr6n5F2o1v9dRnnr
      - id: QmWgW69CaTwJYwcSdu36mkXgwWY11RjvX1oMGykrxT3wDS
    # A reference name, referred to later in the `sourceStreamName` of either a transformation or a sink.
    referenceName: account
    entity:
      # The name of the entities
      name: account
  - type: subgraphEntity
    deployments:
      - id: QmPuXT3poo1T4rS6agZfT51ZZkiN3zQr6n5F2o1v9dRnnr
      - id: QmWgW69CaTwJYwcSdu36mkXgwWY11RjvX1oMGykrxT3wDS
    referenceName: market_daily_snapshot
    entity:
      name: market_daily_snapshot
# We are just replicating data, so we don't need any SQL transforms.
transforms: []
sinks:
  # In this example, we're using a postgres secret called SUPER_SECRET_SECRET.
  # Feel free to change this out with any other type of sink.
  - type: postgres
    # The sourceStreamName matches the above `referenceNames`
    sourceStreamName: account
    table: qidao_accounts
    schema: public
    secretName: SUPER_SECRET_SECRET
  - type: postgres
    sourceStreamName: market_daily_snapshot
    table: qidao_market_daily_snapshot
    schema: public
    secretName: SUPER_SECRET_SECRET
```

**4. Create the pipeline**

```shell theme={"dark"}
goldsky pipeline create qidao-crosschain --definition-path qidao-crosschain.yaml --status ACTIVE
```

You should see a response from the server like:

```
◇  Successfully validated --definition-path file
✔ Created pipeline with name: qidao-crosschain
name: qidao-crosschain
version: 1
project_id: project_cl8ylkiw00krx0hvza0qw17vn
status: INACTIVE
resource_size: s
is_deleted: false
created_at: 1697696162607
updated_at: 1697696162607
definition:
  sources:
    - type: subgraphEntity
      entity:
        name: account
      referenceName: account
      deployments:
        - id: QmPuXT3poo1T4rS6agZfT51ZZkiN3zQr6n5F2o1v9dRnnr
        - id: QmWgW69CaTwJYwcSdu36mkXgwWY11RjvX1oMGykrxT3wDS
    - type: subgraphEntity
      entity:
        name: market_daily_snapshot
      referenceName: market_daily_snapshot
      deployments:
        - id: QmPuXT3poo1T4rS6agZfT51ZZkiN3zQr6n5F2o1v9dRnnr
        - id: QmWgW69CaTwJYwcSdu36mkXgwWY11RjvX1oMGykrxT3wDS
...
```

**5. Monitor the pipeline**

Monitor the pipeline with `goldsky pipeline monitor qidao-crosschain`. The status should change from `STARTING` to `RUNNING` in a minute or so, and data will start appearing in your postgresql database.

**6. Create API server**

Once you have multiple subgraphs being written into one destination database, you can set up a GraphQL API server with this database as a source; there are many options to do this:

* [Apollo Server](https://www.apollographql.com/docs/apollo-server/)
* [Express GraphQL](https://graphql.org/graphql-js/running-an-express-graphql-server/)
* [Hasura](https://hasura.io/) \[recommended for quick-start]

Can't find what you're looking for? Reach out to us at [support@goldsky.com](mailto:support@goldsky.com) for help.


## Related topics

- [Goldsky MCP Server](https://docs.goldsky.com/mcp-server.md)
- [Instant subgraph configuration](https://docs.goldsky.com/subgraphs/reference/instant-subgraph.md)
- [Create low-code subgraphs](https://docs.goldsky.com/subgraphs/guides/create-a-low-code-subgraph.md)
- [Cross-chain USDC settlement ledger](https://docs.goldsky.com/solutions/cross-chain-settlement.md)
- [Benefits](https://docs.goldsky.com/benefits.md)
