Skip to content

yashicon21/graphql-complexity-vscode

Repository files navigation

GraphQL Complexity Analyzer for VS Code

VS Code Marketplace License: MIT TypeScript

A powerful VS Code extension that analyzes GraphQL query complexity in real-time, helping developers write efficient queries and prevent expensive operations from reaching production.

GraphQL Complexity Demo

✨ Features

  • πŸ” Real-time Complexity Analysis: See complexity scores as you type GraphQL queries
  • ⚠️ Intelligent Warnings: Get notified when queries exceed complexity thresholds
  • πŸ“Š Code Lens Integration: Complexity scores displayed directly in the editor
  • 🎯 Configurable Thresholds: Customize complexity limits for your specific needs
  • πŸš€ Schema Auto-loading: Automatically fetches and caches your GraphQL schema
  • πŸ”§ Zero Configuration: Works out of the box with sensible defaults
  • πŸ—οΈ Clean Architecture: Modular codebase with separation of concerns
  • πŸ”’ Type Safety: Built with TypeScript for reliability and maintainability
  • ⚑ Performance Optimized: Efficient caching and minimal resource usage
  • πŸ› οΈ Developer Friendly: Comprehensive error handling and debugging support

πŸš€ Quick Start

  1. Install the Extension

    # From VS Code Marketplace
    code --install-extension yashar-zolmajdi.graphql-complexity-vscode
  2. Open a GraphQL File

    • Create or open any .graphql file
    • The extension activates automatically
  3. Configure Your GraphQL Endpoint (Optional)

    {
      "graphqlComplexity.endpoint": "https://api.example.com/graphql",
      "graphqlComplexity.maxThreshold": 1000,
      "graphqlComplexity.scalarCost": 1
    }
  4. Start Writing Queries

    • Complexity scores appear above your queries
    • Warnings show when thresholds are exceeded

πŸ“‹ Requirements

  • VS Code: Version 1.80.0 or higher
  • GraphQL Endpoint: A running GraphQL server with introspection enabled
  • Network Access: To fetch schema introspection (can be configured for local development)

βš™οΈ Configuration

Configure the extension through VS Code settings:

Basic Settings

Setting Default Description
graphqlComplexity.endpoint http://localhost:4000/graphql GraphQL endpoint URL for schema introspection
graphqlComplexity.maxThreshold 1000 Maximum allowed complexity score
graphqlComplexity.scalarCost 1 Cost assigned to scalar fields

Example Configuration

{
  "graphqlComplexity.endpoint": "https://api.github.com/graphql",
  "graphqlComplexity.maxThreshold": 500,
  "graphqlComplexity.scalarCost": 2
}

🎯 How It Works

The extension calculates query complexity using a depth-first approach:

  1. Schema Introspection: Fetches your GraphQL schema automatically
  2. AST Analysis: Parses queries into Abstract Syntax Trees
  3. Complexity Calculation: Assigns costs based on field depth and types
  4. Real-time Feedback: Updates complexity scores as you type

Complexity Calculation Example

query GetUser {
  user(id: "123") {
    # Cost: 1
    name # Cost: 1
    posts {
      # Cost: 1 * number of posts
      title # Cost: 1 per post
      comments {
        # Cost: 1 * posts * comments
        content # Cost: 1 per comment
      }
    }
  }
}
# Total complexity varies based on data size

πŸ› οΈ Development

Prerequisites

  • Node.js 18+
  • pnpm (recommended) or npm
  • VS Code

Setup

  1. Clone the Repository

    git clone https://github.com/yasharzolmajdi/graphql-complexity-vscode.git
    cd graphql-complexity-vscode
  2. Install Dependencies

    pnpm install
  3. Build the Extension

    pnpm build
    # or
    npm run build
  4. Run in Development Mode

    • Press F5 in VS Code to open a new Extension Development Host window
    • Or use the "Run Extension" launch configuration

Project Structure

src/
β”œβ”€β”€ extension.ts                 # Main extension entry point
β”œβ”€β”€ config/                      # Configuration management
β”‚   β”œβ”€β”€ index.ts
β”‚   └── manager.ts
β”œβ”€β”€ schema/                      # GraphQL schema handling
β”‚   β”œβ”€β”€ index.ts
β”‚   β”œβ”€β”€ loader.ts
β”‚   └── complexity-calculator.ts
β”œβ”€β”€ diagnostics/                 # VS Code diagnostics
β”‚   β”œβ”€β”€ index.ts
β”‚   └── manager.ts
β”œβ”€β”€ providers/                   # VS Code providers
β”‚   β”œβ”€β”€ index.ts
β”‚   β”œβ”€β”€ code-lens.ts
β”‚   └── document-manager.ts
└── types/                       # Type definitions
    β”œβ”€β”€ index.ts
    └── config.ts

See ARCHITECTURE.md for detailed architecture documentation.

Available Scripts

# Build the extension
pnpm build

# Watch for changes during development
pnpm watch

# Lint the code
pnpm lint

# Fix linting issues
pnpm lint:fix

# Format the code
pnpm format

# Check formatting
pnpm format:check

# Run all checks (lint + format + build)
pnpm check

# Package for distribution
vsce package

Code Quality

This project uses modern development tools:

  • TypeScript 5.8+: Strong typing and latest language features
  • ESLint 9+: Modern flat config with TypeScript rules
  • Prettier 3+: Consistent code formatting
  • esbuild: Fast TypeScript compilation and bundling
  • Husky: Git hooks for code quality
  • lint-staged: Pre-commit linting and formatting
  • VS Code: Built-in debugging and testing support

Testing

Testing framework setup is planned for future releases. The extension currently uses:

  • Manual testing in Extension Development Host
  • Real GraphQL endpoints for integration testing
  • TypeScript compiler for static analysis

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Ways to Contribute

  • πŸ› Report Bugs: Open an issue with reproduction steps
  • πŸ’‘ Suggest Features: Share your ideas for improvements
  • πŸ“ Improve Documentation: Help make our docs clearer
  • πŸ”§ Submit Code: Fix bugs or implement new features
  • πŸ§ͺ Write Tests: Help us maintain code quality

Development Workflow

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and commit: git commit -m 'Add amazing feature'
  4. Push to your branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“ Changelog

See CHANGELOG.md for a detailed history of changes.

Latest Version (0.0.1) - July 2025

  • ✨ Initial release with modern ES modules architecture
  • πŸ” Real-time complexity analysis using graphql-validation-complexity
  • ⚠️ Configurable warning thresholds (default: 10)
  • πŸ“Š Code lens integration for inline complexity display
  • 🎯 Schema auto-loading with introspection caching
  • πŸ› οΈ Modern TypeScript 5.8+ with ESLint flat config
  • ⚑ Fast esbuild compilation and bundling

πŸ› Known Issues

  • Schema Caching: Schema is cached until VS Code restart (manual refresh command planned)
  • Large Schemas: Performance may be impacted with very large schemas (optimization planned)
  • Authentication: Currently no support for authenticated endpoints (headers support planned)
  • Custom Directives: Limited support for custom GraphQL directives
  • Error Recovery: Schema errors may require extension reload

See our Issues page for the latest status and planned improvements.

πŸ“š Related Projects

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments


Made with ❀️ by Yashar Zolmajdi

If you find this extension helpful, please consider:

  • ⭐ Starring the repository
  • πŸ“ Leaving a review on the VS Code Marketplace
  • πŸ› Reporting issues or suggesting improvements
  • 🀝 Contributing to the project

Have questions or need help? Open an issue or reach out on Twitter.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •