GraphQL Language Service Server

2.14.8 · active · verified Sun Apr 19

The GraphQL Language Service Server, currently at version 2.14.8, serves as the backend process for providing rich GraphQL language features within Integrated Development Environments (IDEs). It implements a significant portion of Microsoft's Language Server Protocol (LSP), offering core functionalities such as real-time GraphQL syntax diagnostics, intelligent autocomplete suggestions, and precise hyperlink navigation to fragment definitions and named type declarations. It also provides outline view support for GraphQL queries and facilitates the detection and parsing of GraphQL within template literal tags (e.g., `gql`, `graphql`) across various file types including JavaScript, TypeScript, JSX, TSX, Vue, and Svelte. The package is part of the broader GraphiQL ecosystem, which maintains a consistent release cadence for its components. A key differentiator is its reliance on the `graphql-config` standard for project configuration, enabling robust schema management and multi-project support, although full multi-root workspace support is an ongoing development. This server is the official, specification-compliant backend for GraphQL language tooling.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically start the GraphQL Language Service Server. It initializes the server using Node.js IPC for communication and highlights the essential `graphql-config` setup required for operation.

import { startServer } from 'graphql-language-service-server';
import { loadConfig } from 'graphql-config'; // Ensure 'graphql-config' is installed

async function initializeLanguageServer() {
  // A graphql.config.js, .graphqlrc.yml, etc. is required in your project root.
  // Example graphql.config.js:
  // module.exports = {
  //   schema: './schema.graphql',
  //   documents: './src/**/*.graphql',
  //   extensions: {
  //     endpoints: {
  //       default: {
  //         url: 'http://localhost:4000/graphql',
  //         headers: {
  //           Authorization: `Bearer ${process.env.GRAPHQL_TOKEN || ''}`,
  //         },
  //       },
  //     },
  //   },
  // };

  const config = await loadConfig();

  if (!config) {
    console.error('Error: A graphql-config file (.graphqlrc.yml, graphql.config.js, etc.) is required in your project.');
    process.exit(1);
  }

  console.log('Starting GraphQL Language Service Server via IPC (Node.js method)...');
  // 'method: "node"' uses Node.js IPC (stdin/stdout) for communication with the LSP client.
  // Other methods include 'socket' and 'streams'.
  await startServer({
    method: 'node',
    // You can pass the loaded config directly if needed, but the server
    // typically discovers it automatically based on the 'configDir'.
    // config,
    // configDir: process.cwd(),
  });
  console.log('GraphQL Language Service Server successfully started. Waiting for LSP client messages.');
}

initializeLanguageServer().catch(error => {
  console.error('Failed to initialize GraphQL Language Service Server:', error);
  process.exit(1);
});

view raw JSON →