GraphQL Language Service Server
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
-
Cannot use import statement outside of a module
cause Attempting to use ES module `import` syntax in a CommonJS (`.js` without `"type": "module"` in `package.json`, or a `.cjs` file) context.fixEnsure your Node.js project is configured for ES modules (`"type": "module"` in `package.json`) or use a transpiler like Babel/TypeScript to convert `import` to `require` for CommonJS environments. -
Error: A graphql-config file (.graphqlrc.yml, graphql.config.js, etc.) is required in your project.
cause The language server failed to find or load a `graphql-config` file in the expected locations.fixCreate a valid `graphql-config` file (e.g., `.graphqlrc.yml`, `graphql.config.js`) in your project's root directory or the directory where the server is expected to operate. -
[ERROR] .../vim/lsp/rpc.lua:430 "rpc" "graphql-lsp" "stderr" '... graphql-language-service'
cause A generic error indicating a failure in the Language Server Protocol (LSP) communication or an issue within the `graphql-language-service-server` process itself, often seen in LSP client logs.fixCheck the server's standard error output for more specific messages. Verify Node.js version, `graphql-config` validity, and ensure all peer dependencies are installed. Debug the server process if running manually.
Warnings
- gotcha A `graphql-config` file (e.g., `.graphqlrc.yml`, `graphql.config.js`) is mandatory for the language server to function correctly. Without it, the server cannot determine your schema, documents, or project settings.
- breaking The server requires Node.js version `^18.18.0` or `>=20.9.0` for full compatibility and optimal performance. Older Node.js versions may encounter errors or unexpected behavior.
- gotcha Changes to your `graphql-config` file or other relevant project settings may not immediately be picked up by a running language server. You might need to restart your IDE or the language server process manually for changes to take effect.
- gotcha While `graphql-config` supports multi-project setups, the GraphQL Language Service Server does not yet fully support multi-root workspaces within IDEs like VS Code. This can lead to unexpected behavior when dealing with multiple distinct project roots in a single editor instance.
Install
-
npm install graphql-language-service-server -
yarn add graphql-language-service-server -
pnpm add graphql-language-service-server
Imports
- startServer
const { startServer } = require('graphql-language-service-server');import { startServer } from 'graphql-language-service-server'; - startServer
await startServer({ method: 'node' });
Quickstart
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);
});