graphql-info-inspector
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

A toolkit for working with GraphQLResolveInfo objects. Enables resolvers to easily determine whether a client's GraphQL query selected a particular field.


Installation

npm i graphql-info-inspector

This library is written in TypeScript, so type definitions are included in the box.

You must also install graphql v14.x or higher as a peer dependency.


Introduction

GraphQL servers commonly pass an info object to resolver functions. This info object, which has the shape defined by GraphQLResolveInfo, conveys to the resolver extensive detail about the client's query (starting from the point of resolution).

It can be used to determine which fields the client selected to retrieve on the object being resolved to optimize your response. But the structure of GraphQLResolveInfo is complex.

graphql-info-inspector is a toolkit that helps to make the retrieval of information from GraphQLResolveInfo easier.


Usage

Functions

isFieldSelected(fieldPath: string, info: GraphQLResolveInfo): boolean

Returns true if the specified field was selected in the GraphQL query, or false if it's not found. The fieldPath can reference nested fields using dotted parentField.childField.grandchildField notation.

import { GraphQLResolveInfo } from 'graphql';
import { isFieldSelected } from 'graphql-info-inspector';

// Example resolver function for a "products" query in your GQL schema
async function products(source: any, args: any, context: any, info: GraphQLResolveInfo): Promise<Product[]> {
  const products = await loadProducts();

  if (isFieldSelected('image', info)) {
    // Client requested `products { image { ... } }` so we need to fetch that data somehow
    await loadImages(products);
  }

  if (isFieldSelected('image.tags', info)) {
    // Client requested `products { image { tags: { ... } } }` so we need to fetch that data as well
    await loadImageTags(products);
  }

  return products;
}

findSelectionNode(fieldPath: string, info: GraphQLResolveInfo): SelectionNode | null

Like isFieldSelected, but it returns the SelectionNode for the specified field if it was selected in the GraphQL query, or null if it's not found.

GraphQLInspection class

If you prefer an object-oriented style, you can use GraphQLInspection class like this:

import { GraphQLResolveInfo } from 'graphql';
import { GraphQLInspection } from 'graphql-info-inspector';

// Example resolver function for a "products" query in your GQL schema
async function products(source: any, args: any, context: any, info: GraphQLResolveInfo): Promise<Product[]> {
  const queryInspection = new GraphQLInspection(info);

  const products = await loadProducts();

  if (queryInspection.has('image')) {
    // Client requested `products { image { ... } }` so we need to fetch that data somehow
    await loadImages(products);
  }

  if (queryInspection.has('image.tags')) {
    // Client requested `products { image { tags: { ... } } }` so we need to fetch that data as well
    await loadImageTags(products);
  }

  return products;
}

License

MIT

Package Sidebar

Install

npm i graphql-info-inspector

Weekly Downloads

113

Version

1.0.0

License

MIT

Unpacked Size

14.4 kB

Total Files

11

Last publish

Collaborators

  • sgarner