What is apollo-utilities?
The apollo-utilities package provides a set of utility functions for working with GraphQL data in Apollo Client. These utilities help with tasks such as parsing, transforming, and comparing GraphQL queries and results.
What are apollo-utilities's main functionalities?
GraphQL Query Parsing
This feature allows you to parse a GraphQL query and extract the operation definition. The code sample demonstrates how to use the `getOperationDefinition` function to parse a query and log the operation definition.
const { getOperationDefinition } = require('apollo-utilities');
const gql = require('graphql-tag');
const query = gql`
query GetUser {
user(id: 1) {
id
name
}
}
`;
const operationDefinition = getOperationDefinition(query);
console.log(operationDefinition);
Deep Equality Check
This feature provides a deep equality check for comparing two objects. The code sample demonstrates how to use the `isEqual` function to compare two nested objects.
const { isEqual } = require('apollo-utilities');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
console.log(isEqual(obj1, obj2)); // true
Query Transformation
This feature allows you to transform a GraphQL query by adding `__typename` fields to it. The code sample demonstrates how to use the `addTypenameToDocument` function to transform a query.
const { addTypenameToDocument } = require('apollo-utilities');
const gql = require('graphql-tag');
const query = gql`
query GetUser {
user(id: 1) {
id
name
}
}
`;
const transformedQuery = addTypenameToDocument(query);
console.log(transformedQuery);
Other packages similar to apollo-utilities
graphql-tools
The graphql-tools package provides a set of utilities for building and manipulating GraphQL schemas in JavaScript. It offers functionalities such as schema stitching, schema transformation, and mock data generation. Compared to apollo-utilities, graphql-tools focuses more on schema-related tasks rather than query manipulation.
graphql-tag
The graphql-tag package is used to parse GraphQL queries into the standard GraphQL AST format. It is often used in conjunction with Apollo Client to define queries. While graphql-tag focuses on parsing queries, apollo-utilities provides additional functionalities such as query transformation and deep equality checks.
lodash
The lodash package is a utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. While it is not specific to GraphQL, lodash offers deep equality checks and other utility functions that can be useful in a GraphQL context. Compared to apollo-utilities, lodash is more general-purpose and not tailored specifically for GraphQL.