1

我正在使用带有 oasgraph 的 LoopBack 4(重命名为 OpenAPI-to-GraphQL)。我的 OpenAPI 端点之一具有filter以下架构的参数:

"parameters": [
          {
            "name": "filter",
            "in": "query",
            "style": "deepObject",
            "explode": true,
            "schema": {
              "properties": {
                "where": {
                  "type": "object"
                },
                "fields": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "boolean"
                    },
                    "idOwner": {
                      "type": "boolean"
                    },
                    "createdTimestamp": {
                      "type": "boolean"
                    },
                    "modifiedTimestamp": {
                      "type": "boolean"
                    },
                    "idUserCreated": {
                      "type": "boolean"
                    },
                    "idUserModified": {
                      "type": "boolean"
                    },
                    "value": {
                      "type": "boolean"
                    },
                    "dicContactId": {
                      "type": "boolean"
                    },
                    "counterpartyId": {
                      "type": "boolean"
                    }
                  }
                },
                "offset": {
                  "type": "integer",
                  "minimum": 0
                },
                "limit": {
                  "type": "integer",
                  "minimum": 0
                },
                "skip": {
                  "type": "integer",
                  "minimum": 0
                },
                "order": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "include": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "relation": {
                        "type": "string"
                      },
                      "scope": {
                        "properties": {
                          "where": {
                            "type": "object"
                          },
                          "fields": {
                            "type": "object",
                            "properties": {}
                          },
                          "offset": {
                            "type": "integer",
                            "minimum": 0
                          },
                          "limit": {
                            "type": "integer",
                            "minimum": 0
                          },
                          "skip": {
                            "type": "integer",
                            "minimum": 0
                          },
                          "order": {
                            "type": "array",
                            "items": {
                              "type": "string"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              },
              "type": "object"
            }
          }
        ],

如您所见,该where属性属于“对象”类型。然而,在 graphQL 编辑器中,它需要一个字符串:

graphql 编辑器 - 预期类型字符串

问题是当我运行查询时字符串会产生错误:

graphql 编辑器 - where 子句不是对象

结果,我无法使用where子句执行查询。

4

1 回答 1

0

您可以npm qs使用节点模块来对您的 where 子句对象进行字符串化。因为 Loopbackqs在后台使用来解析查询字符串。

import * as qs from 'qs';

let query = {
  // where condition
}

qs.stringify(query, { addQueryPrefix: true });

你可以在这里找到更多信息qs

Loopback4 查询字符串问题讨论:- https://github.com/strongloop/loopback-next/issues/2468

于 2019-07-24T04:04:38.700 回答