graphql - 如何在 OpenAPI-to-GraphQL 服务器中使用带有 graphQL 的“where”子句?
问题描述
我正在使用带有 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 编辑器中,它需要一个字符串:
问题是当我运行查询时字符串会产生错误:
结果,我无法使用where
子句执行查询。
解决方案
您可以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
推荐阅读
- database - Oracle 示例 HR 模式 :: 将 HR 的默认表空间指定为参数 2
- python - 查找列表中 X 个数之和的所有连续索引
- macos - macOS:模拟鼠标事件仅在启动二进制文件时有效,而不是应用程序包
- c# - 如何在 Unity 中检查构建中有多少场景
- python - 从谷歌找到并点击创建新帐户按钮
- c++ - C++ 如何在文件系统路径中支持非 ASCII 字符需要在所有目标操作系统(Windows、Linux、Mac)上支持具有宽字符的路径
- javascript - 为单页应用刷新 Google Ads
- vim - 获取所有 vim 缓冲区,同时将我的窗口/缓冲区保持在同一个地方?
- javascript - 父/子进程或从 js 文件运行 npm 任务
- javascript - 动态站点再生 GatsbyJS