1.2.0 • Published 8 months ago

graphql-to-js v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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 使用

    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 参数

paramtypedescription
queryStringgraphql 语句
dataObject动态参数
1.2.0

8 months ago

1.1.0

9 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago