Custom GraphQL resolvers
One can extend the GraphQL API generated by OpenReader with custom queries. To do that, one can define GraphQL query resolvers in the designated modulesrc/server-extension/resolvers. Note that all resolver classes (including any additional types) must be exported by src/server-extension/resolvers/index.ts.
A custom resolver should import TypeGraphQL types and use annotations provided by the library to define query arguments and return types. If your squid lacks a type-graphql dependency, add it with:
Examples
Simple entity counter
evm template:
- grab a test squid as described here;
- install
type-graphql; - save the example code to
src/server-extension/resolver.ts; - re-export
CountResolveratsrc/server-extension/resolvers/index.ts: - rebuild the squid with
npm run build; - (re)start the GraphQL server with
npx squid-graphql-server.
totalBurns selection will appear in the GraphiQL playground.
Custom SQL query
More examples
Some great examples of@subsquid/graphql-server-based custom resolvers can be spotted in the wild in the Rubick repo by KodaDot.
For more examples of resolvers, see TypeGraphQL examples repo.
Logging
To keep logging consistent across the entire GraphQL server, use@subsquid/logger:
LOG here is a logger object identical to ctx.log interface-wise.
Interaction with global settings
--max-response-sizeused for DoS protection is ignored in custom resolvers.- Caching works on custom queries in exactly the same way as it does on the schema-derived queries.
Troubleshooting
Reflect.getMetadata is not a function
Add import 'reflect-metadata' on top of your custom resolver module and install the package if necessary.