Skip to content

equalogic/graphql-info-inspector

Repository files navigation

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 optimize your response by determining which fields the client selected to retrieve on the object being resolved. 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[]> {
  let withImages: boolean = false;
  let withImageTags: boolean = false;

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

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

  return await loadProducts({ withImages, withImageTags });
}

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);

  let withImages: boolean = false;
  let withImageTags: boolean = false;

  if (queryInspection.has('image')) {
    // Client requested `products { image { ... } }` so we need to fetch that data
    withImages = true;
  }

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

  return await loadProducts({ withImages, withImageTags });
}

License

MIT