# graphql-parse-fields

> Parse fields from AST (GraphQLResolveInfo) into a JSON tree

Latest version **1.2.0** (published 2016-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-parse-fields
pnpm add graphql-parse-fields
yarn add graphql-parse-fields
bun add graphql-parse-fields
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2016-11-22 |
| First published | 2016-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Tejesh Mehta |
| Maintainers | tjmehta |
| Keywords | graphql, ast, info, resolve, graphqlresolveinfo, fields, names, selection, fragments, selections, fragment |

## Links

- npm: https://www.npmjs.com/package/graphql-parse-fields
- Repository: https://github.com/tjmehta/graphql-parse-fields
- Homepage: https://github.com/tjmehta/graphql-parse-fields#readme
- Issues: https://github.com/tjmehta/graphql-parse-fields/issues
- npm.io page: https://npm.io/package/graphql-parse-fields

## Dependencies (1)

- [cast-array](https://npm.io/package/cast-array.md) ^1.0.1

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2016-11-22
- 1.1.0 — 2016-09-08
- 1.0.0 — 2016-09-08

## README

# graphql-parse-fields [![Build Status](https://travis-ci.org/tjmehta/graphql-parse-fields.svg)](https://travis-ci.org/tjmehta/graphql-parse-fields?branch=master) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
Parse fields from AST (GraphQLResolveInfo) into a JSON tree

## Installation
```bash
npm i --save graphql-parse-fields
```

## Usage
Example1: Parse GraphQL resolve `info`
 * @param {Object} info - graphql resolve info
 * @param {Boolean} [ignoreRoot] - default: true
 * @return {Object} fieldTree
```js
var parseFields = require('graphql-parse-fields')

var GraphQLObjectType = //...
var UserType = //...

var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    user: {
      type: UserType,
      resolve: function (root, args, ctx, info) {
        var fields = parseFields(info)
        /*
        Fields parsed from query (at bottom of this example):
          note: since keepRoot was not passed, parseFields ignores the root key "user" for convenience
        {
          id: true,
          name: true,
          widgets: {
            edges {
              node {
                id: true
              }
            }
          }
        }
        */

       var fieldsWithRoot = parseFields(info, true) // keepRoot: true
       /*
        Fields parsed from query (at bottom of this example):

        {
          user {
            id: true,
            name: true,
            widgets: {
              edges {
                node {
                  id: true
                }
              }
            }
          }
        }
        */
      },
    }
  }
})

/*
Query:

query userQuery {
  user {
    id
    name
    widgets {
      edges {
        node {
          id
        }
      }
    }
  }
}
*/
```

Example2: Parse GraphQL ASTs
 * @param {Array} asts - ast array
 * @param {Object} [fragments] - optional fragment map
 * @param {Object} [fieldTree] - optional initial field tree
 * @return {Object} fieldTree
```js
var ast = {
  "kind": "Field",
  "alias": null,
  "name": {
    "kind": "Name",
    "value": "user"
  },
  "selectionSet": {
    "kind": "SelectionSet",
    "selections": [
      {
        "kind": "Field",
        "name": {
          "kind": "Name",
          "value": "id"
        }
        selectionSet: null
        //...
      }
    ]
    //...
  }
  //...
}
parseAst(ast)
parseAst([ast])
/*
Both result in:
{
  user: {
    id: true
  }
}
*/
```

## Changelog
[CHANGELOG.md](https://github.com/tjmehta/graphql-parse-fields/blob/master/CHANGELOG.md)

## License
MIT

---
_Source: https://npm.io/package/graphql-parse-fields · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
