Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
db-subgraph
Advanced tools
This package provides a flexible and reusable solution for generating GraphQL clients using @graphql-codegen/cli, integrated with @tanstack/react-query. It simplifies the process of setting up and managing GraphQL code generation for multiple subgraphs.
This package uses pnpm as the preferred package manager. First, install the package along with other required dependencies:
pnpm add db-subgraph graphql graphql-request
Then, add the development dependencies:
pnpm add -D @graphql-codegen/cli @graphql-typed-document-node/core
This separation ensures that the runtime dependencies are installed in your main dependencies, while the code generation tools are kept in your development dependencies.
Create a codegen file (e.g., src/subgraphs/codegen.ts
) in your project:
import { generateCodegenConfig } from "db-subgraph";
import { loadEnv } from "vite";
const env = loadEnv("subgraphs", process.cwd(), "");
export default generateCodegenConfig({
subgraphs: [
{
apiKey: env.PUBLIC_SUBGRAPHS_API_KEY,
chainsResourceIds: env.PUBLIC_SUBGRAPHS_CHAINS_RESOURCE_IDS,
environment: "production",
productionUrl: env.PUBLIC_SUBGRAPHS_PRODUCTION_URL,
},
],
});
You can use environment variables, hard-coded values, or a combination of both. Adjust the configuration according to your project's needs.
This file uses the generateCodegenConfig
function from our package to create the configuration for @graphql-codegen/cli.
Add a script to your package.json
to run the code generation:
{
"scripts": {
"codegen": "graphql-codegen --config ./src/subgraphs/codegen.ts"
}
}
Now you can generate your GraphQL clients by running:
pnpm run codegen
You can see a how it's been used in the dAppBooster Landing Page.
And for further information refer to the subgraph documentation.
The PackageConfig
interface accepts the following options for each subgraph:
apiKey
: Your subgraph API key.chainsResourceIds
: A string of comma-separated <chainId>:<subgraphId>:<resourceId>
.
137:uniswap:BvYimJ6vCLkk63oWZy7WB5cVDTVVMugUAF35RAUZpQXE,8453:aave:GQFbb95cE6d8mV989mL5figjaGaKCQB3xqYrr1bRyXqF
developmentUrl
: The URL for the development environment.productionUrl
: The URL for the production environment.environment
: Either 'development' or 'production'.queriesDirectory
: The directory where your GraphQL queries are located.
./src/subgraphs/queries
destinationDirectory
: The directory where your typed queries will be generated.
./src/subgraphs/gql
You can configure multiple subgraphs by adding more objects to the subgraphs
array in your configuration:
export const graphqlConfig: PackageConfig = {
subgraphs: [
{
// First subgraph configuration
},
{
// Second subgraph configuration
},
// Add more as needed
],
};
The package will generate GraphQL clients in the src/subgraphs/gql/
directory, organized by subgraph ID.
If you need to customize the generation process further, you can modify the codegen.ts
file. The package is designed to be flexible, allowing you to define configurations in the way that best suits your project structure and requirements.
FAQs
Subgraph batteries for dAppBooster
The npm package db-subgraph receives a total of 0 weekly downloads. As such, db-subgraph popularity was classified as not popular.
We found that db-subgraph demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.