Overview
GraphQL queries and clients are organized insrc/graphql/, which is a separate yarn workspace with its own code generation setup.
Architecture
The GraphQL setup consists of:- Query definitions:
.graphqlfiles insrc/graphql/queries/ - Configuration:
src/graphql/config.jsdefines GraphQL endpoints - Generated code: TypeScript types and SDK functions in
src/graphql/__generated__/ - Clients: Exported client instances in
src/graphql/index.ts
Adding a Query to an Existing Client
If you’re adding a query to an existing GraphQL client (like ENS or Metadata), follow these steps:Add the query to the .graphql file
Add your query or mutation to the appropriate
.graphql file in src/graphql/queries/.Example: Adding a domain query to src/graphql/queries/ens.graphql:Run the code generator
Run the GraphQL codegen tool to generate TypeScript types and fetcher functions.From the root of the repository:This will:
- Download the remote schema
- Parse your query definitions
- Generate TypeScript types
- Create SDK functions
Adding a New GraphQL Client
If you need to query a GraphQL API that isn’t already configured, follow these steps:Create a .graphql file
Create a new
.graphql file in src/graphql/queries/ with your query definitions.Example: src/graphql/queries/example.graphql:Run code generation
Generate types and SDK for your new client:This creates
src/graphql/__generated__/example.ts with:- TypeScript types for your queries
- SDK functions for each query
- Request/response types
Configuration Options
Theconfig.js file supports various schema options:
Working with GraphQL Queries
Query Structure
GraphQL queries should follow these conventions:Query Variables
All query parameters are type-safe:Query Results
Results are fully typed:Using GraphQL in Stores
Integrate GraphQL queries with Rainbow’s state management:With createQueryStore
With React Query (Legacy)
Error Handling
Handle GraphQL errors gracefully:Testing GraphQL Queries
Mock GraphQL clients in tests:Code Generation Commands
Install GraphQL workspace dependencies
Generate TypeScript types and SDK
yarn graphql-prepare- Prepares environment variablescd src/graphql && yarn codegen- Runs code generation
Full setup
yarn graphql-codegen:installyarn ds:installyarn graphql-codegenyarn fetch:networks
Best Practices
Request Only What You Need
Only request the fields you’ll use to minimize response size and improve performance.
Use Fragments
Define reusable fragments for complex types to avoid duplication.
Type-Safe Parameters
Always use TypeScript types for query parameters and results.
Error Handling
Always handle errors gracefully and log failures for debugging.
Query Optimization
Good:Naming Conventions
- Query names: Use descriptive names starting with
get(e.g.,getToken,getDomain) - Variables: Use camelCase (e.g.,
$tokenAddress,$chainId) - Fragments: Use descriptive names ending with
Fields(e.g.,TokenPriceFields)
Troubleshooting
Code generation fails
Types not updating
Ensure you’re running code generation after changing queries:Client import errors
Ensure the client is exported insrc/graphql/index.ts and the path alias is correct:
Additional Resources
GraphQL Documentation
Official GraphQL documentation
The Graph
The Graph protocol documentation
Code Conventions
Rainbow coding standards
Testing Guidelines
Testing best practices