如何在 OpenAPI-to-GraphQL 服务器中将 "where" 小句与 graphQL 一起使用?
How to use "where" clausule with graphQL in OpenAPI-to-GraphQL server?
我将 LoopBack 4 与 oasgraph(重命名为 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
属性属于 "object" 类型。但是在 graphQL 编辑器中它需要一个 String:
graphql editor - expected type string
问题是当我 运行 查询时字符串产生错误:
graphql editor - where clause is not an object
因此,我无法使用 where
子句执行查询。
您可以使用 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
我将 LoopBack 4 与 oasgraph(重命名为 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
属性属于 "object" 类型。但是在 graphQL 编辑器中它需要一个 String:
graphql editor - expected type string
问题是当我 运行 查询时字符串产生错误:
graphql editor - where clause is not an object
因此,我无法使用 where
子句执行查询。
您可以使用 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