GraphQL IntelliJ Plugin

repository·master·Indexed 21 days ago

https://github.com/jetbrains/js-graphql-intellij-plugin

Provides advanced language support, schema discovery, and query execution for GraphQL developers using JetBrains IDEs, including WebStorm and IntelliJ IDEA. Features include schema-aware completion, support for GraphQL Specification and SDL, multi-schema project configuration via graphql-config, and integration with Relay and Apollo. The plugin supports fetching remote schemas via introspection, executing queries directly from the editor, and managing multiple GraphQL projects within a single configuration.

Tokens
6.4K
Snippets
20
Records
34
Agent score
75%

What's inside js-graphql-intellij-plugin

  1. Overview of GraphQL Plugin features

    master

    The plugin provides comprehensive GraphQL tooling, including:

    • Language Support: Full support for GraphQL Specification and Schema Definition Language (SDL).
    • Intelligent Editing: Schema-aware completion, error highlighting, documentation, syntax highlighting, and code formatting.
    • Schema Discovery: Automatic discovery of local schemas and support for fetching remote schemas via introspection.
    • Multi-schema Support: Configuration for multi-schema projects using graphql-config files and project scopes.
    • Framework Integration: Built-in support for Relay and Apollo (recognizing graphql and gql tagged template literals in JS/TS).
    • Query Execution: Execute queries against endpoints with support for custom headers and environment variables.
    • Navigation: Find Usages and Go to Declaration for schema types, fields, and fragments.
    • Advanced Types: Built-in definitions for Relay, Federation, and Apollo Kotlin (must be enabled in Settings | Languages & Frameworks | GraphQL).
  2. How schema discovery works in the GraphQL IntelliJ Plugin

    master

    The plugin's language features (completion, error highlighting, documentation) depend entirely on how schema types are discovered. The discovery method depends on your project structure:

    Single-schema projects

    By default, the plugin assumes a single schema and automatically processes:

    • All .graphql files in the "Project files" scope.
    • Injected GraphQL strings in JavaScript/TypeScript files (.js, .jsx, .ts, .tsx) using Tagged Template Literals with the following tags: graphql, .gql, or Relay.QL.

    Multi-schema projects

    For projects with multiple schemas, you must configure specific scopes to prevent type collisions (where types appear to be declared multiple times) and to ensure validation only recognizes types belonging to the current schema. You can configure these scopes using graphql-config v2 files with includes and excludes glob patterns.

  3. Use GraphQL scratch files as playgrounds

    master

    GraphQL scratch files serve as a playground for sending queries. To create one:

    1. Open the GraphQL tool window.
    2. Double-click an endpoint node.
    3. Select New GraphQL Scratch File.

    Configuring Scratch Files

    You can manually associate a scratch file with a specific configuration and project by adding a leading comment following this pattern: # config=<path>[!<optionalProjectName>]

    Valid examples:

    # config=/user/local/project/.graphqlrc.yml
    # config=/user/local/project/.graphqlrc.yml!backend

    Note: This configuration only applies to queries and fragment definitions; type definitions within scratch files are ignored.

    # config=/user/local/project/.graphqlrc.yml
    
    query { 
      user { id }
    }
  4. Use GraphQL scratch files for testing queries

    master

    GraphQL scratch files allow you to write and execute temporary queries or mutations against your GraphQL endpoints outside of your production code.

    To use them:

    1. Create a .graphqlconfig file (Right-click project base dir -> "New" -> "GraphQL Configuration File").
    2. Define your endpoint details in the config file.
    3. Open a scratch file. If you have an existing config, you can jump to it using the "Edit .graphqlconfig" toolbar button in the top left of the scratch file editor.
  5. Use JavaScript or TypeScript for GraphQL configuration

    master

    You can use .js or .ts files for configuration. This requires Node.js to be installed and configured in your IDE. Note that these configurations will not work in Community editions of JetBrains IDEs.

    JavaScript/CommonJS: Use module.exports for standard Node.js configuration.

    ESM: To use ESM, add "type": "module" to your package.json.

    TypeScript: Requires ts-node to be installed locally or globally. If using ESM with TypeScript, you must configure the ts-node ESM loader in your tsconfig.json.

    // JavaScript (CommonJS)
    module.exports = {
        schema: 'https://localhost:8000'
    }
    // tsconfig.json for TypeScript ESM
    {
        "compilerOptions": {
            "module": "ESNext"
        },
        "ts-node": {
            "esm": true
        }
    }
  6. Execute GraphQL queries from the editor

    master

    You can run GraphQL queries directly from your editor files. Place the caret on the query definition and use one of the following methods:

    1. Toolbar: Click the Execute GraphQL action in the GraphQL file toolbar.
    2. Hotkey: Press Ctrl/Cmd + Enter.

    The query is sent to the endpoint currently selected in the toolbar. If your query requires variables, use the Toggle Variables Editor action in the toolbar to provide them in JSON format.

    // Place caret on the query and press Ctrl/Cmd + Enter
    query GetUser($id: ID!) {
      user(id: $id) {
        name
      }
    }
  7. Install the GraphQL IntelliJ Plugin

    master

    The GraphQL plugin provides language support for WebStorm, IntelliJ IDEA, and other IntelliJ Platform IDEs. You can install it via the JetBrains Marketplace or directly within your IDE.

    Steps to install via IDE:

    1. Open your IDE.
    2. Navigate to File | Settings/Preferences | Plugins.
    3. Select the Marketplace tab.
    4. Search for graphql.
    5. Select the GraphQL suggestion and click install.
  8. Configure schema discovery and endpoints using .graphqlconfig

    master

    To enable schema discovery and define GraphQL endpoints within your project, use a .graphqlconfig file. This allows the plugin to automatically identify your GraphQL schemas and associate them with specific endpoints for features like autocomplete and validation.

    For practical implementation patterns, refer to the official example repository.

    https://github.com/jimkyndemeyer/graphql-config-examples/
  9. Configure multiple GraphQL projects in a single configuration file

    master

    If you have multiple independent GraphQL schemas (e.g., in a monorepo), you can define them within a single configuration file using the projects key. Each project entry specifies its own schema and documents patterns.

    Matching Rules:

    • Files are matched against projects in the order they are defined. The first matching project is used.
    • Strict vs. Non-strict matching: If a file does not match any project's documents pattern, it will be associated with the first project that does not define include or exclude keys.
    • To ensure a project only matches its intended files, add an exclude pattern to prevent it from capturing files meant for other projects.
    projects:
      frontend:
        schema: https://my.api.com/graphql
        documents: frontend/**/*.{graphql,js,ts}
        exclude: queries/**  # Enables strict matching for this project
      backend:
        schema: backend/schema.graphql
        documents: backend/**/*.graphql
  10. Configure multi-schema projects using graphql-config

    master

    To manage multiple schemas, use .graphqlconfig files. You can create a new configuration file by right-clicking a folder and selecting "New GraphQL Configuration File" or using the "+" button in the GraphQL Tool window under the "Schemas and Project Structure" tab.

    There are two primary strategies for organizing these files:

    Place a separate .graphqlconfig file in each schema's directory. This automatically creates separate scopes for each schema.

    - project root/
        - product_a/
            - .graphqlconfig
            - schema_files...
        - product_b/
            - .graphqlconfig
            - schema_files...

    Option B: Single config file

    Place one .graphqlconfig file in the project root and use the projects field to separate schemas using includes glob patterns.

    {
      "projects": {
        "product a": {
          "includes": ["product a (schema one)/**"]
        },
        "product b": {
          "includes": ["product b (schema two)/**"]
        }
      }
    }
    {
      "projects": {
        "product a": {
          "includes": ["product a (schema one)/**"]
        },
        "product b": {
          "includes": ["product b (schema two)/**"]
        }
      }
    }