UNPKG

@0xobelisk/graphql-server

Version:

Tookit for interacting with dubhe graphql server

138 lines 6.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPostGraphileConfig = createPostGraphileConfig; exports.createPlaygroundHtml = createPlaygroundHtml; const query_filter_1 = require("./query-filter"); const simple_naming_1 = require("./simple-naming"); const all_fields_filter_plugin_1 = require("./all-fields-filter-plugin"); const enhanced_playground_1 = require("./enhanced-playground"); const postgraphile_plugin_connection_filter_1 = __importDefault(require("postgraphile-plugin-connection-filter")); const pg_simplify_inflector_1 = __importDefault(require("@graphile-contrib/pg-simplify-inflector")); const postgraphile_1 = require("postgraphile"); const pg_pubsub_1 = __importDefault(require("@graphile/pg-pubsub")); // Create PostGraphile configuration function createPostGraphileConfig(options) { const { port, nodeEnv, graphqlEndpoint, enableSubscriptions, enableCors, availableTables } = options; // Build GraphQL and WebSocket endpoint URLs const baseUrl = `http://localhost:${port}`; const _graphqlUrl = `${baseUrl}${graphqlEndpoint}`; const _subscriptionUrl = enableSubscriptions === 'true' ? `ws://localhost:${port}${graphqlEndpoint}` : undefined; // Create plugin hook to support WebSocket and subscriptions const pluginHook = (0, postgraphile_1.makePluginHook)([pg_pubsub_1.default]); const config = { // Basic configuration - disable default GraphiQL graphiql: false, enhanceGraphiql: false, showErrorStack: nodeEnv === 'development', extendedErrors: nodeEnv === 'development' ? ['hint', 'detail', 'errcode'] : [], // Feature configuration - enable subscriptions subscriptions: enableSubscriptions === 'true', live: enableSubscriptions === 'true', // Enable live functionality to support subscriptions enableQueryBatching: true, enableCors: enableCors === 'true', // Add plugin hook to support WebSocket pluginHook, // Disable all mutation functionality - only keep queries and subscriptions disableDefaultMutations: true, // Schema configuration dynamicJson: true, setofFunctionsContainNulls: false, ignoreRBAC: false, ignoreIndexes: true, // Log control configuration // Control SQL query logs through CLI parameters disableQueryLog: options.disableQueryLog || (nodeEnv === 'production' && !options.enableQueryLog), // Enable query execution plan explanation (development environment only) allowExplain: nodeEnv === 'development', // Monitor PostgreSQL changes (development environment only) watchPg: nodeEnv === 'development', // GraphQL query timeout setting queryTimeout: options.queryTimeout, // GraphQL endpoint - explicitly specify route graphqlRoute: graphqlEndpoint, graphiqlRoute: '/graphiql', // GraphiQL interface route // Add custom plugins appendPlugins: [ query_filter_1.QueryFilterPlugin, // Must execute before SimpleNamingPlugin pg_simplify_inflector_1.default, // Simplify field names, remove ByXxxAndYyy suffixes simple_naming_1.SimpleNamingPlugin, // Fixed field loss issue postgraphile_plugin_connection_filter_1.default, all_fields_filter_plugin_1.AllFieldsFilterPlugin ], // Advanced configuration options for Connection Filter plugin graphileBuildOptions: { // Enable logical operators (and, or, not) connectionFilterLogicalOperators: true, // Enable relationship filtering connectionFilterRelations: true, // Enable computed column filtering connectionFilterComputedColumns: true, // Enable array filtering connectionFilterArrays: true, // Enable function filtering connectionFilterSetofFunctions: true, // Allow null input and empty object input connectionFilterAllowNullInput: true, connectionFilterAllowEmptyObjectInput: true }, // Only include detected tables includeExtensionResources: false, // Exclude unnecessary tables ignoreTable: (tableName) => { // If no tables detected, allow all tables if (availableTables.length === 0) { return false; } // Otherwise only include detected tables return !availableTables.includes(tableName); }, // Export schema (development environment) exportGqlSchemaPath: nodeEnv === 'development' ? 'sui-indexer-schema.graphql' : undefined }; // If subscriptions are enabled, add additional PostgreSQL subscription configuration if (enableSubscriptions === 'true') { return { ...config, // Use dedicated subscription connection pool ownerConnectionString: options.databaseUrl, // WebSocket configuration websocketMiddlewares: [], // PostgreSQL settings - optimized for long-running subscriptions pgSettings: { statement_timeout: '0', // No timeout for subscription queries idle_in_transaction_session_timeout: '0', // Allow long transactions default_transaction_isolation: 'read committed' }, // Retry on connection failure retryOnInitFail: true, // Performance optimization for subscriptions pgDefaultRole: undefined, jwtSecret: undefined, // Additional configuration for development environment ...(nodeEnv === 'development' && { queryCache: false, // Disable cache for real-time data allowExplain: true }) }; } return config; } // Export enhanced playground HTML generator function createPlaygroundHtml(options) { const { graphqlEndpoint, enableSubscriptions, availableTables } = options; // Use relative URLs so playground connects to the same domain as the server const graphqlUrl = graphqlEndpoint; const subscriptionUrl = enableSubscriptions === 'true' ? graphqlEndpoint : undefined; return (0, enhanced_playground_1.createEnhancedPlayground)({ url: graphqlUrl, subscriptionUrl, title: 'Sui Indexer GraphQL Playground', subtitle: `Powerful GraphQL API | ${availableTables.length} tables discovered | ${enableSubscriptions === 'true' ? 'Real-time subscriptions supported' : 'Real-time subscriptions disabled'}` })(null, null, {}); } //# sourceMappingURL=postgraphile-config.js.map