# graphql-to-js

> 前端侧解码graphql语句

Latest version **1.2.0** (published 2023-09-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install graphql-to-js
pnpm add graphql-to-js
yarn add graphql-to-js
bun add graphql-to-js
```

## 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 | 2023-09-12 |
| First published | 2023-07-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | huangchuancong |
| Maintainers | kangaroo711 |

## Links

- npm: https://www.npmjs.com/package/graphql-to-js
- npm.io page: https://npm.io/package/graphql-to-js

## Recent versions

- 1.2.0 (latest) — 2023-09-12
- 1.1.0 — 2023-08-28
- 1.0.4 — 2023-07-21
- 1.0.3 — 2023-07-20
- 1.0.2 — 2023-07-20
- 1.0.1 — 2023-07-20

## README

# Description 描述

正常 graphql 前端请求是通过 query 和 variables 给到后端去解码(schema);

官方示例: https://graphql.cn/graphql-js/graphql-clients/ ;

但是有时候需求就是剑走偏锋, 接口走公共网关只接受解码好的请求, 所以解码工作落到了前端手上,故诞生了此插件以便解决以上问题;

如果你也和我一样有这种需求, 那真的是泰裤辣!

本插件实现了:

1. query 中用$代表变量 (√)
2. 有传参替换$, 没有则删除 query 中定义的参数(键值), 且兼容\_like(自动在字符串前后加百分号规则), 花括号, 逗号存在的情况 (√)
3. 兼容\_where 为空对象自行删除对应的 query 中的定义(可自行在数组拓展其他情况) (√)
4. 字符串会自动添加上"value"(双引号) (√)
5. 递归遍历对象数组判断值类型是字符串重复第 4 点 (√)

## Install 安装

npm

```
$ npm install graphql-to-js --save
```

yarn

```
$ yarn add graphql-to-js --save
```

## Usage 使用

```javascript

    import {transformFun} from 'graphql-to-js';

    // query 和 参数, 通过添加$代表变量, 如果没有传该变量或变量值为"", 则会删除这个请求参数(因为graphql的硬性要求)
    let query= `{
        product(
          _where: { id: $id  name: $name  other.age: $age }
          ) {
          id
          name
          other {
            age
          }
        }
    }`;

    let data = { id : 119 ,name: "小明" }

    // axios request
    {
      data:{
           payload: {
            query: transformFun(query, data), // "{ product(_where: { id: 119 name:"小明"}) { id name }}"
          },
      }
    }

    // console.log()//
```

## Options 参数

| param |  type  | description  |
| :---: | :----: | :----------- |
| query | String | graphql 语句 |
| data  | Object | 动态参数     |

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