Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/equalogic/graphql-info-inspector

A toolkit for working with GraphQLResolveInfo objects.
https://github.com/equalogic/graphql-info-inspector

graphql resolveinfo

Last synced: 3 months ago
JSON representation

A toolkit for working with GraphQLResolveInfo objects.

Awesome Lists containing this project

README

        

















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](https://www.npmjs.com/package/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](https://graphql.org/graphql-js/type/#graphqlobjecttype:~:text=type-,GraphQLResolveInfo,-%3D%20%7B),
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.

```ts
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 {
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:

```ts
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 {
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