Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Add LLM and MCP support (#2788)

## [2.22.1] - 2025-05-21
### Changed
Expand Down
27 changes: 27 additions & 0 deletions packages/query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@ then run the following command
```sh
NODE_OPTIONS="-r dotenv/config" yarn start:dev -- --name <subquery_name> --playground
```

## LLM Configuration

Suggest to use reasoning models to archive better quality of results.

### Choose one of the following providers:

### Option 1: OpenAI

LLM_PROVIDER=openai
LLM_MODEL=o1 # Optional: defaults to '4o-mini'
OPENAI_API_KEY=your_openai_api_key

### Option 2: Anthropic Claude

LLM_PROVIDER=anthropic
LLM_MODEL=claude-3-7-sonnet-latest # Optional: defaults to 'claude-3-7-sonnet-latest'
ANTHROPIC_API_KEY=your_anthropic_api_key

### Option 3: Custom OpenAI-compatible endpoint

LLM_PROVIDER=openai
LLM_BASE_URL=http://your-llm-endpoint/v1 # Required for custom provider
LLM_MODEL=your-model-name # Required: model name for custom endpoint
OPENAI_API_KEY=your_api_key # Optional: API key for custom endpoint

### Common LLM Settings
8 changes: 8 additions & 0 deletions packages/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
"@graphile/pg-aggregates": "^0.1.1",
"@graphile/pg-pubsub": "^4.13.0",
"@langchain/anthropic": "^0.3.20",
"@langchain/core": "^0.3.55",
"@langchain/langgraph": "^0.2.71",
"@langchain/openai": "^0.5.10",
"@modelcontextprotocol/sdk": "^1.11.5",
"@nestjs/common": "^9.4.0",
"@nestjs/core": "^9.4.0",
"@nestjs/platform-express": "^9.4.0",
"@rekog/mcp-nest": "^1.5.2",
"@subql/common": "workspace:*",
"@subql/utils": "workspace:*",
"@subql/x-graphile-build-pg": "4.13.0-0.2.5",
Expand All @@ -50,12 +56,14 @@
"graphql": "^15.8.0",
"graphql-query-complexity": "^0.11.0",
"graphql-ws": "^5.16.0",
"langchain": "^0.3.24",
"lodash": "^4.17.21",
"pg": "^8.12.0",
"pg-tsquery": "^8.4.2",
"postgraphile": "^4.13.0",
"postgraphile-plugin-connection-filter": "^2.2.2",
"rxjs": "^7.1.0",
"typeorm": "^0.3.23",
"ws": "^8.18.0",
"yargs": "^16.2.0"
},
Expand Down
6 changes: 5 additions & 1 deletion packages/query/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import {Module} from '@nestjs/common';
import {ConfigureModule} from './configure/configure.module';
import {GraphqlModule} from './graphql/graphql.module';
import {ChatModule} from './llm/chat.module';
import {MCPModule} from './mcp/mcp.module';

@Module({
imports: [ConfigureModule.register(), GraphqlModule],
// the order is essential, the ChatModule must be before the GraphqlModule so /v1/chat/completions
// can be handled without interference from the GraphqlModule
imports: [ConfigureModule.register(), ChatModule, MCPModule, GraphqlModule],
controllers: [],
})
export class AppModule {}
30 changes: 27 additions & 3 deletions packages/query/src/configure/configure.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import {ConnectionOptions} from 'tls';
import {CommonConnectionOptions, SecureContextOptions} from 'tls';
import {DynamicModule, Global, Module} from '@nestjs/common';
import {getFileContent, CONNECTION_SSL_ERROR_REGEX} from '@subql/common';
import {Pool, PoolConfig} from 'pg';
import {DataSource} from 'typeorm';
import {getLogger} from '../utils/logger';
import {getYargsOption} from '../yargs';
import {Config} from './config';
Expand Down Expand Up @@ -40,7 +41,8 @@ export class ConfigureModule {
});

const dbSslOption = () => {
const sslConfig: ConnectionOptions = {rejectUnauthorized: false};
// This is a subset of ConnectionOptions and TlsOptions to satisfy PgPool and DataSource
const sslConfig: SecureContextOptions & CommonConnectionOptions = {rejectUnauthorized: false};
if (opts['pg-ca']) {
try {
sslConfig.ca = getFileContent(opts['pg-ca'], 'postgres ca cert');
Expand Down Expand Up @@ -85,6 +87,24 @@ export class ConfigureModule {
pgClient._explainResults = [];
});
}

// Only establish connection if chat is enabled
let dataSource: DataSource | undefined;
console.log('sslConfig', dbSslOption());
if (opts.chat) {
dataSource = new DataSource({
type: 'postgres',
host: config.get('DB_HOST_READ'),
port: config.get('DB_PORT'),
username: config.get('DB_USER'),
password: config.get('DB_PASS'),
database: config.get('DB_DATABASE'),
schema: config.get<string>('name'),
ssl: opts['pg-ca'] ? dbSslOption() : undefined, // Cannot be an empty object
});
await dataSource.initialize();
}

return {
module: ConfigureModule,
providers: [
Expand All @@ -96,8 +116,12 @@ export class ConfigureModule {
provide: Pool,
useValue: pgPool,
},
{
provide: DataSource,
useValue: dataSource,
},
],
exports: [Config, Pool],
exports: [Config, Pool, DataSource],
};
}
}
8 changes: 3 additions & 5 deletions packages/query/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {playgroundPlugin} from './plugins/PlaygroundPlugin';
import {queryAliasLimit} from './plugins/QueryAliasLimitPlugin';
import {queryComplexityPlugin} from './plugins/QueryComplexityPlugin';
import {queryDepthLimitPlugin} from './plugins/QueryDepthLimitPlugin';
import {ProjectService} from './project.service';

const {argv} = getYargsOption();
const logger = getLogger('graphql-module');
Expand All @@ -45,16 +44,15 @@ class NoInitError extends Error {
}
}
@Module({
providers: [ProjectService],
providers: [],
})
export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
private _apolloServer?: ApolloServer;
private wsCleanup?: ReturnType<typeof useServer>;
constructor(
private readonly httpAdapterHost: HttpAdapterHost,
private readonly config: Config,
private readonly pgPool: Pool,
private readonly projectService: ProjectService
private readonly pgPool: Pool
) {}

private get apolloServer(): ApolloServer {
Expand Down Expand Up @@ -138,7 +136,7 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
const schemaName = this.config.get<string>('name');
if (!schemaName) throw new Error('Unable to get schema name from config');

const dbSchema = await this.projectService.getProjectSchema(schemaName);
const dbSchema = schemaName;
let options: PostGraphileCoreOptions = {
replaceAllPlugins: plugins,
subscriptions: true,
Expand Down
39 changes: 0 additions & 39 deletions packages/query/src/graphql/project.service.ts

This file was deleted.

Loading
Loading