What is graphql-language-service-utils?
The graphql-language-service-utils package provides utility functions for working with GraphQL language services. It includes features for parsing, validating, and formatting GraphQL queries, as well as for handling GraphQL schema and documents.
What are graphql-language-service-utils's main functionalities?
parseGraphQL
This feature allows you to parse a GraphQL query string into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple GraphQL query.
const { parse } = require('graphql-language-service-utils');
const query = `query { user(id: 1) { name } }`;
const parsedQuery = parse(query);
console.log(parsedQuery);
validateGraphQL
This feature allows you to validate a GraphQL query against a given schema. The code sample demonstrates how to validate a simple GraphQL query using a schema.
const { validate } = require('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);
formatError
This feature allows you to format GraphQL errors in a standardized way. The code sample demonstrates how to format a simple error message.
const { formatError } = require('graphql-language-service-utils');
const error = new Error('Something went wrong');
const formattedError = formatError(error);
console.log(formattedError);
Other packages similar to graphql-language-service-utils
graphql
The graphql package is the core reference implementation of GraphQL for JavaScript. It provides similar functionalities such as parsing, validating, and executing GraphQL queries. However, it is more comprehensive and includes additional features for building and executing GraphQL schemas.
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 transforming schemas. While it overlaps with some functionalities of graphql-language-service-utils, it is more focused on schema management and manipulation.
apollo-server
Apollo Server is a community-maintained open-source GraphQL server that works with any GraphQL schema. It includes tools for parsing, validating, and executing GraphQL queries, similar to graphql-language-service-utils, but it is designed to be a complete server solution with additional features for performance monitoring and schema stitching.