Migrate from The Graph
This guide walks through the steps to migrate a subgraph to SQD. In what follows we will convert the Gravatar subgraph into a squid and run it locally. Impatient readers may clone the squid from the repo and run it by following the instructions in README:EvmBatchProcessor provided by the Squid SDK defines a single handler that indexes EVM logs and transaction data in batches. It differs from the programming model of subgraph mappings that defines a separate data handler for each EVM log topic to be indexed. Due to significantly less frequent database hits (once per batch compared to once per log) the batch-based handling model shows up to a 10x increase in the indexing speed.
At the same time, concepts of the schema file, code generation from the schema file and auto-generated GraphQL API should be familiar to subgraph developers. In most cases the schema file of a subgraph can be imported into a squid as is.
There are some known limitations:
- Many-to-Many entity relations should be modeled explicitly as two many-to-one relations
- Full control over the target database (Postgres), including custom migrations and ad-hoc queries in the handler
- Custom target databases and data formats (e.g. CSV)
- Arbitrary code execution in the data handler
- Extension of the GraphQL API with arbitrary SQL
- Secret environment variables, allowing to seamlessly use private third-party JSON-RPC endpoints and integrate with external APIs
- Deployment slots and tags
- API caching
Squid setup
1. Install Squid CLI
Instructions here.2. Fetch the template
Create a squid from the minimalisticevm template:
3. Copy the schema file and generate entities
The minimal template already contains a dummyschema.graphql file. We replace it with the subgraph schema as is:
file=schema.graphl
squid-typeorm-codegen tool of the Squid SDK, then build the squid:
yarn codegen in subgraph.
After that, start the local database and regenerate migrations based on the generated entities using the squid-typeorm-migration tool:
Gravatar will appear in db/migrations. Apply it with
4. Generate typings from ABI
Copy./abis/Gravity.json from the subgraph project and paste it to ./abi folder in the squid project.
To generate the typings, run:
graph add <address> [<subgraph-manifest default: "./subgraph.yaml">] command, to generate typings, run:
evm-typegen tool that fetches the contract ABI by the address and generates type-safe access classes in src/abi/Gravity.ts. The generated boilerplate code will be used to decode EVM logs and directly query the contract. It also contains topic definitions used in the next step.
5. Subscribe to EVM logs
While in The Graph data source is defined in the manifest filesubgraph.yaml, in SQD subscriptions to EVM data, including logs, are performed at the processor object definition customarily located at src/processor.ts. The processor is configured directly by the code, unlike subgraphs which require handlers and events to be defined in the manifest file.
file=src/processor.ts
0x2E645469f354BB4F5c8a05B3b30A929361cf77eC with topic0 within a specified list. The configuration also states that indexing should start from block 6175243, the height at which the contract was deployed.
Check out the EVM indexing section for the list of supported networks and configuration details.
The above snippet is eqivalent to the following subgraph.yaml:
file=subgraph.yaml
6. Transform and save the data
In Subgraph data is saved in themapping.ts file. The mapping function will receive an ethereum.Block as its only argument. In SQD, we set up the processor in processor.ts and save the data in the main.ts.
We migrate the subgraph handlers that transform the event data into Gravatar objects. Instead of saving or updating gravatars one by one, EvmBatchProcessor receives an ordered batch of event items it is subscribed to. In our case we have only two kinds of logs — emitted on gravatar creations and updates.
The entry point for transform code is src/main.ts. We start by appending an auxiliary data normalization function to the end of that file:
src/main.ts
0x... string into a byte array we use the decodeHex utility from Squid SDK.
src/main.ts
7. Run the processor and GraphQL API
To start the indexing, (re)build the project and run the processor:4350 by default) with a GraphQL schema auto-generated from the schema file, run in a new terminal window
What’s Next?
- Compare your API to that of subgraph using the compareGraphQL script
- Have a closer look at
EvmBatchProcessor - Learn how to deploy a squid to SQD Cloud for free
- Learn how to index and query the contract state
- Inspect a more complex Uniswap V3 squid which tracks Uniswap V3 trading data. It was migrated from the Uniswap V3 subgraph. It takes only a few hours to sync from scratch on a local machine.

