|
| 1 | +# Querying And Interfaces |
| 2 | + |
| 3 | +## Contents |
| 4 | + |
| 5 | +1. Quick health checks |
| 6 | +2. SQL over HTTP |
| 7 | +3. GraphQL |
| 8 | +4. gRPC |
| 9 | +5. MCP endpoint |
| 10 | +6. Static and metadata endpoints |
| 11 | +7. Metrics |
| 12 | + |
| 13 | +## Quick health checks |
| 14 | + |
| 15 | +```bash |
| 16 | +curl -s http://127.0.0.1:8080/ |
| 17 | +``` |
| 18 | + |
| 19 | +Expected shape: |
| 20 | +- JSON with `service: "torii"` and `version`. |
| 21 | + |
| 22 | +Check GraphQL route: |
| 23 | + |
| 24 | +```bash |
| 25 | +curl -i http://127.0.0.1:8080/graphql |
| 26 | +``` |
| 27 | + |
| 28 | +Check gRPC reflection: |
| 29 | + |
| 30 | +```bash |
| 31 | +grpcurl -plaintext 127.0.0.1:50051 list |
| 32 | +grpcurl -plaintext 127.0.0.1:50051 describe world.World |
| 33 | +``` |
| 34 | + |
| 35 | +## SQL over HTTP |
| 36 | + |
| 37 | +HTTP SQL is available only if `--http.sql true` (default true). |
| 38 | + |
| 39 | +Playground: |
| 40 | + |
| 41 | +```bash |
| 42 | +open http://127.0.0.1:8080/sql |
| 43 | +``` |
| 44 | + |
| 45 | +GET query: |
| 46 | + |
| 47 | +```bash |
| 48 | +curl -sG http://127.0.0.1:8080/sql \ |
| 49 | + --data-urlencode 'query=SELECT id, world_address, created_at FROM entities LIMIT 5;' |
| 50 | +``` |
| 51 | + |
| 52 | +POST query body: |
| 53 | + |
| 54 | +```bash |
| 55 | +curl -s http://127.0.0.1:8080/sql \ |
| 56 | + -X POST \ |
| 57 | + --data 'SELECT id, transaction_hash, executed_at FROM events ORDER BY executed_at DESC LIMIT 10;' |
| 58 | +``` |
| 59 | + |
| 60 | +## GraphQL |
| 61 | + |
| 62 | +Endpoint: `http://127.0.0.1:8080/graphql` |
| 63 | + |
| 64 | +Common root query fields: |
| 65 | +- `entity`, `entities` |
| 66 | +- `eventMessage`, `eventMessages` |
| 67 | +- `event`, `events` |
| 68 | +- `model`, `models` |
| 69 | +- `transaction`, `transactions` |
| 70 | +- `controller`, `controllers` |
| 71 | +- `tokenBalances`, `tokenTransfers`, `tokens` |
| 72 | +- `metadata`, `metadatas` |
| 73 | +- Dynamic model resolvers generated from indexed model schema |
| 74 | + |
| 75 | +Mutation: |
| 76 | +- `publishMessage(worldAddress, signature, message)` |
| 77 | + |
| 78 | +Subscriptions: |
| 79 | +- `entityUpdated` |
| 80 | +- `eventMessageUpdated` |
| 81 | +- `eventEmitted` |
| 82 | +- `modelRegistered` |
| 83 | +- `tokenBalanceUpdated` |
| 84 | +- `tokenUpdated` |
| 85 | +- `transaction` |
| 86 | + |
| 87 | +Example query: |
| 88 | + |
| 89 | +```bash |
| 90 | +curl -s http://127.0.0.1:8080/graphql \ |
| 91 | + -H 'content-type: application/json' \ |
| 92 | + -d '{ |
| 93 | + "query":"query { entities(first: 3) { totalCount edges { node { id keys } } } }" |
| 94 | + }' |
| 95 | +``` |
| 96 | + |
| 97 | +## gRPC |
| 98 | + |
| 99 | +Endpoint: `127.0.0.1:50051` |
| 100 | + |
| 101 | +List and inspect: |
| 102 | + |
| 103 | +```bash |
| 104 | +grpcurl -plaintext 127.0.0.1:50051 list world.World |
| 105 | +grpcurl -plaintext 127.0.0.1:50051 describe world.World |
| 106 | +``` |
| 107 | + |
| 108 | +Example calls: |
| 109 | + |
| 110 | +```bash |
| 111 | +grpcurl -plaintext -d '{}' 127.0.0.1:50051 world.World/Worlds |
| 112 | + |
| 113 | +grpcurl -plaintext -d '{ |
| 114 | + "query": "SELECT id, world_address FROM entities LIMIT 5;" |
| 115 | +}' 127.0.0.1:50051 world.World/ExecuteSql |
| 116 | + |
| 117 | +grpcurl -plaintext -d '{ |
| 118 | + "query": { "query": "dragon", "limit": 10 } |
| 119 | +}' 127.0.0.1:50051 world.World/Search |
| 120 | +``` |
| 121 | + |
| 122 | +Notes: |
| 123 | +- `ExecuteSql` is blocked when `--http.sql false`. |
| 124 | +- gRPC supports compressed messages and gRPC-Web in server config. |
| 125 | +- Use `torii-grpc-client` or `torii-client` crates for typed Rust integration. |
| 126 | + |
| 127 | +## MCP endpoint |
| 128 | + |
| 129 | +Routes: |
| 130 | +- WebSocket/SSE entry: `/mcp` |
| 131 | +- SSE message endpoint: `/mcp/message?sessionId=...` |
| 132 | + |
| 133 | +Built-in tools: |
| 134 | +- `query` tool: run SQL and return JSON text payload |
| 135 | +- `schema` tool: return table/column schema (`sqlite_master` + `pragma_table_info`) |
| 136 | + |
| 137 | +Use MCP for agent-driven DB inspection when raw SQL schema discovery is needed. |
| 138 | + |
| 139 | +## Static and metadata endpoints |
| 140 | + |
| 141 | +Static token/contract image serving: |
| 142 | +- `/static/{contract_address}/image` |
| 143 | +- `/static/{contract_address}/{token_id}/image` |
| 144 | + |
| 145 | +Optional image sizing query params: |
| 146 | +- `?h=...&w=...` (aliases: `height`, `width`) |
| 147 | + |
| 148 | +Metadata reindex endpoint: |
| 149 | +- `/metadata/reindex/{contract_address}/{token_id}` |
| 150 | + |
| 151 | +Use metadata reindex to force refresh token metadata from chain and persist updates. |
| 152 | + |
| 153 | +## Metrics |
| 154 | + |
| 155 | +Enable metrics: |
| 156 | + |
| 157 | +```bash |
| 158 | +torii --metrics --metrics.addr 0.0.0.0 --metrics.port 9200 |
| 159 | +``` |
| 160 | + |
| 161 | +Query: |
| 162 | + |
| 163 | +```bash |
| 164 | +curl -s http://127.0.0.1:9200/metrics | head |
| 165 | +``` |
| 166 | + |
| 167 | +For Grafana/Prometheus setup, use: |
| 168 | +- `docker-compose -f docker-compose.grafana.yml up -d` |
| 169 | +- `docs/grafana-setup.md` |
| 170 | + |
0 commit comments