如何在 OpenAPI-to-GraphQL 服务器中使用带有 graphQL 的“where”子句?
Posted
技术标签:
【中文标题】如何在 OpenAPI-to-GraphQL 服务器中使用带有 graphQL 的“where”子句?【英文标题】:How to use "where" clausule with graphQL in OpenAPI-to-GraphQL server? 【发布时间】:2019-12-01 21:39:56 【问题描述】:我正在使用带有 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 editor - expected type string
问题是我运行查询时字符串产生错误:
graphql editor - where clause is not an object
因此,我无法使用where
子句执行查询。
【问题讨论】:
【参考方案1】:您可以使用 npm qs
节点模块来字符串化您的 where 子句对象。因为 Loopback 在后台使用 qs
来解析查询字符串。
import * as qs from 'qs';
let query =
// where condition
qs.stringify(query, addQueryPrefix: true );
你可以找到更多关于qs
here的信息
Loopback4 查询字符串问题讨论:- https://github.com/strongloop/loopback-next/issues/2468
【讨论】:
谢谢马尼什。我们找到了一种解决方法,但很难为所有属性实现这个:“where”:“type”:“object”,“properties”:“dicCategoryId”:“type”:“string”,以上是关于如何在 OpenAPI-to-GraphQL 服务器中使用带有 graphQL 的“where”子句?的主要内容,如果未能解决你的问题,请参考以下文章