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
API Docs
Discord Channel
Note: Still mostly experimental, however it depends mostly on stable libraries.
Migration Note: As of 3.0.0, the LSP 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 languageservices.
It also provides a new LanguageService
class as browser/web-worker runtime friendly alternative to the one that lives in graphql-language-service-interface
, that utilizes the same underlying functions, meaning most fixes and improvements from here on out will continue to be reflected by both implementations.
Usage
Instantiates with these optional parameters:
type GraphQLLanguageConfig = {
parser?: typeof parse;
schemaLoader?: typeof defaultSchemaLoader;
schemaBuilder?: typeof defaultSchemaBuilder;
schemaConfig: SchemaConfig;
};
this is the minimum configuration required:
const languageService = new LanguageService({
schemaConfig: { uri: 'https://my/schema' },
});