# @antstackio/graphql-body-parser

> an express graphql parser middleware

Latest version **0.1.0** (published 2021-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @antstackio/graphql-body-parser
pnpm add @antstackio/graphql-body-parser
yarn add @antstackio/graphql-body-parser
bun add @antstackio/graphql-body-parser
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-06-29 |
| First published | 2021-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | khubo |
| Maintainers | khubo, raalzz, vishwasnavadak |

## Links

- npm: https://www.npmjs.com/package/@antstackio/graphql-body-parser
- Repository: https://github.com/antstackio/graphql-body-parser
- Homepage: https://github.com/antstackio/graphql-body-parser#readme
- Issues: https://github.com/antstackio/graphql-body-parser/issues
- npm.io page: https://npm.io/package/@antstackio/graphql-body-parser

## Dependencies (2)

- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.12.5

## Recent versions

- 0.1.0 (latest) — 2021-06-29

## README

# graphql-body-parser

Parses and exposes the graphql query as graphql object in the request. Works as an express middleware, and build on top of `body-parser` module. As of now it only works with graphql request send with `application/json` as `Content-Type`.

## How it works?

It looks for `query` and `variable` object in request body, and then parses the query into its AST. It then attaches both
the AST object as well as variables to req as `gqlObject` property.

Request Body

```json
  {
    "query": "query($name: String) {
               hello(name: $name){
               name
              }
            }",
    "variables": {
      "name": "khubo"
    }
  }
```

the parsed AST and variable will be attached as follows

`req.gqlObject`

```json
{
  "operation": {
    "kind": "Document",
    "definitions": [
      {
        "kind": "OperationDefinition",
        "operation": "query",
        "variableDefinitions": [
          {
            "kind": "VariableDefinition",
            "variable": {
              "kind": "Variable",
              "name": {
                "kind": "Name",
                "value": "name"
              }
            },
            "type": {
              "kind": "NamedType",
              "name": {
                "kind": "Name",
                "value": "String"
              }
            },
            "directives": []
          }
        ],
        "directives": [],
        "selectionSet": {
          "kind": "SelectionSet",
          "selections": [
            {
              "kind": "Field",
              "name": {
                "kind": "Name",
                "value": "hello"
              },
              "arguments": [
                {
                  "kind": "Argument",
                  "name": {
                    "kind": "Name",
                    "value": "name"
                  },
                  "value": {
                    "kind": "Variable",
                    "name": {
                      "kind": "Name",
                      "value": "name"
                    }
                  }
                }
              ],
              "directives": [],
              "selectionSet": {
                "kind": "SelectionSet",
                "selections": [
                  {
                    "kind": "Field",
                    "name": {
                      "kind": "Name",
                      "value": "name"
                    },
                    "arguments": [],
                    "directives": []
                  }
                ]
              }
            }
          ]
        }
      }
    ],
    "loc": {
      "start": 0,
      "end": 94
    }
  },
  "variables": {
    "name": "khubo"
  }
}
```

## Installation

```sh
$ npm install @antstackio/graphql-body-parser
$ yarn add @antstackio/graphql-body-parser
```

## Usage

This can be used instead of normal json parser used with express

```js
import * as express from "express";
import * as gqlParser from "@antstackio/graphql-body-parser";

const app = express();

app.use(gqlParser());

app.listen(process.env.PORT, () => {
  logger.info(`Node app running on port ${process.env.PORT} 🚀🚀`);
});
```

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