What is @apollographql/graphql-language-service-utils?
@apollographql/graphql-language-service-utils is a utility package designed to assist with GraphQL language services. It provides various tools and utilities to help with tasks such as parsing, validation, and completion of GraphQL queries and schemas.
What are @apollographql/graphql-language-service-utils's main functionalities?
GraphQL Parsing
This feature allows you to parse a GraphQL query string into an Abstract Syntax Tree (AST). The `parse` function takes a query string and returns its AST representation.
const { parse } = require('@apollographql/graphql-language-service-utils');
const query = `query { user(id: 1) { name } }`;
const parsedQuery = parse(query);
console.log(parsedQuery);
GraphQL Validation
This feature allows you to validate a GraphQL query against a schema. The `validate` function takes a schema and a parsed query, and returns an array of validation errors, if any.
const { validate } = require('@apollographql/graphql-language-service-utils');
const { buildSchema } = require('graphql');
const schema = buildSchema(`type Query { user(id: ID!): User } type User { id: ID! name: String }`);
const query = `query { user(id: 1) { name } }`;
const errors = validate(schema, parse(query));
console.log(errors);
GraphQL Completion
This feature provides autocomplete suggestions for a given position in a GraphQL query. The `getAutocompleteSuggestions` function takes a schema, a query string, and a position, and returns a list of suggestions.
const { getAutocompleteSuggestions } = require('@apollographql/graphql-language-service-utils');
const { buildSchema } = require('graphql');
const schema = buildSchema(`type Query { user(id: ID!): User } type User { id: ID! name: String }`);
const query = `query { user(`;
const suggestions = getAutocompleteSuggestions(schema, query, { line: 1, character: 12 });
console.log(suggestions);
Other packages similar to @apollographql/graphql-language-service-utils
graphql
The `graphql` package is the core reference implementation of GraphQL for JavaScript. It provides tools for parsing, validating, executing, and introspecting GraphQL queries. Compared to @apollographql/graphql-language-service-utils, it offers a more comprehensive set of features but may require more setup for language service-specific tasks.
graphql-tools
The `graphql-tools` package provides a set of utilities for building and manipulating GraphQL schemas. It includes features for schema stitching, mocking, and schema transformations. While it overlaps with some functionalities of @apollographql/graphql-language-service-utils, it is more focused on schema construction and manipulation rather than language services.
codemirror-graphql
The `codemirror-graphql` package integrates GraphQL language support into the CodeMirror editor. It provides syntax highlighting, linting, and autocomplete features for GraphQL. This package is more specialized for editor integration compared to @apollographql/graphql-language-service-utils, which offers a broader range of utilities for language services.