What is graphql-language-service?
The graphql-language-service npm package provides a set of tools and utilities for working with GraphQL language features. It includes functionalities such as parsing, validation, and autocompletion, which are essential for building GraphQL development tools and editors.
What are graphql-language-service's main functionalities?
Parsing
This feature allows you to parse a GraphQL query string into an Abstract Syntax Tree (AST). The AST can then be used for further analysis or transformation.
const { parse } = require('graphql-language-service-parser');
const query = `query { user(id: 1) { name } }`;
const ast = parse(query);
console.log(JSON.stringify(ast, null, 2));
Validation
This feature allows you to validate a GraphQL query against a schema. It helps in identifying errors and ensuring that the query adheres to the schema's rules.
const { validate } = require('graphql');
const { parse } = require('graphql-language-service-parser');
const { specifiedRules } = require('graphql-language-service');
const schema = /* GraphQLSchema object */;
const query = `query { user(id: 1) { name } }`;
const ast = parse(query);
const errors = validate(schema, ast, specifiedRules);
console.log(errors);
Autocomplete
This feature provides autocompletion suggestions for a given position in a GraphQL query. It is useful for building IDE extensions and other developer tools that enhance the GraphQL development experience.
const { getAutocompleteSuggestions } = require('graphql-language-service-interface');
const schema = /* GraphQLSchema object */;
const query = `query { user(`;
const position = { line: 0, character: 12 };
const suggestions = getAutocompleteSuggestions(schema, query, position);
console.log(suggestions);
Other packages similar to graphql-language-service
graphql
The 'graphql' package is the reference implementation of GraphQL for JavaScript. It provides a complete suite of tools for building GraphQL servers and clients, including parsing, validation, and execution. While it offers similar functionalities to 'graphql-language-service', it is more focused on server-side operations and less on language services for development tools.
codemirror-graphql
The 'codemirror-graphql' package provides a set of CodeMirror modes and utilities for working with GraphQL. It includes features like syntax highlighting, linting, and autocompletion. Compared to 'graphql-language-service', it is more focused on integrating GraphQL capabilities into the CodeMirror editor.
graphql-language-service
Changelog | API Docs |
Discord
Note: Still mostly experimental, however it depends mostly on stable libraries.
Migration Note: As of 3.0.0, the LSP Server command line interface has been moved to graphql-language-service-cli
Purpose
This package brings together all the dependencies for building out web or desktop IDE services for the GraphQL Language.
It is named as such to match the convention of other vscode language services.
Interface
Language Service Protocol (LSP) methods written in TypeScript used by graphql-language-service-server
, monaco-graphql
and codemirror-graphql
.
The goal is to provide methods for creating Language Server Protocol compliant services to be used by an IDE plugin, a browser application or desktop application.
Parser
A standalone online, immutable, dependency-free parser for GraphQL, used by the LSP interface methods
Utils
Various utilities