graphql中未知对象的模式类型
Posted
技术标签:
【中文标题】graphql中未知对象的模式类型【英文标题】:schema type for an unknown object in graphql 【发布时间】:2020-07-02 20:03:59 【问题描述】:我从 db 返回以下对象:
"total_rows": 200,
"bookmark": "g1AAAABteJzLYWBgYMpgTmEQTM4vTc5ISXIwNDLXMwBCwxyQVCJDUv3___-zMpjc7D8wgEEiCx71eSwgJQ1A6j-GtiwA6MscCg",
"rows": [
"id": "51a1ff51b3b4719d05e40ac4bb0d0566",
"objects":
"0":
"type": "ipv4-addr",
"value": "192.168.1.10",
"resolves_to_refs": "2"
,
"1":
"type": "network-traffic"
],
"counts":
"created_by_ref":
"0203a7e6-b174-4af9-812d-ab889816e868": 1,
"0250789a-14c3-4751-b4a0-c017af82b8f1": 1,
"03c63db6-2a84-4627-88be-a83208d524e6": 1,
"05cba3da-11ff-4a7a-aae9-0b1614cd5300": 1,
"fc825d33-26ea-4563-9478-2e1887b87112": 1
,
"file.hashes.MD5":
"UNDEFINED": 200
,
"file.name":
"UNDEFINED": 200
,
"ipv4_addr.value":
"127.0.0.1": 200,
"192.168.1.10": 200
,
"last_observed":
"1583503380000": 5,
"1583589780000": 9,
"1585749840000": 12
,
"num_of_rows": 10
我正在尝试将 graphql 模式融入上述内容。我有以下效果:
const graphql = require("graphql");
const GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLInt, GraphQLList = graphql;
const SearchResultType = new GraphQLObjectType(
name: "SearchResult",
fields: ()=>(
total_rows: type: GraphQLInt ,
bookmark: type: GraphQLString ,
//rows: type: new GraphQLList(GraphQLInt) ,
num_of_rows: type: GraphQLInt
)
);
const RootQuery = new GraphQLObjectType(
name: "RootQueryType",
fields:
searchResult:
type: SearchResultType,
args: id: type: GraphQLString ,
resolve(parentValue: any, args: any)
console.log(args)
return resultMock;
);
module.exports = new GraphQLSchema(
query: RootQuery,
);
以上适用于已定义的数据类型。但是上面的 mockResult 中有一些对象,例如:
"objects":
"0":
"type": "ipv4-addr",
"value": "192.168.1.10",
"resolves_to_refs": "2"
,
"1":
"type": "network-traffic"
或
"counts":
"created_by_ref":
"0203a7e6-b174-4af9-812d-ab889816e868": 1,
"0250789a-14c3-4751-b4a0-c017af82b8f1": 1,
"03c63db6-2a84-4627-88be-a83208d524e6": 1,
"05cba3da-11ff-4a7a-aae9-0b1614cd5300": 1,
"fc825d33-26ea-4563-9478-2e1887b87112": 1
因此,正如您所见,这些对象的键是随机的,或者至少在我们收到它们之前是不可猜测的。有什么方法可以定义这样的:行:类型:新的GraphQLList(我们不知道的任何随机对象),作为下面模式中的类型:
const SearchResultType = new GraphQLObjectType(
name: "SearchResult",
fields: ()=>(
total_rows: type: GraphQLInt ,
bookmark: type: GraphQLString ,
rows: type: new GraphQLList(any random object we do not know ) ,
num_of_rows: type: GraphQLInt
)
);
【问题讨论】:
【参考方案1】:您可以使用 GraphQL JSON Scalar(例如来自 implementation)。不过我不建议这样做(事实上,几年前我做了一个演讲“GraphQL JSON Scalar 被认为是有害的”)。相反,您可能希望将类似地图的对象转换为键值对列表。
例如,对于您的 counts
对象,您可以执行以下操作:
type CreatedByRef
key: ID
count: Int
Object.keys(counts.created_by_ref).map(key => (
key,
count: counts.created_by_ref[key],
));
这将改变结果的形状,但保留 GraphQL 的所有属性。
【讨论】:
您能举个例子吗:相反,您可能希望将类似地图的对象转换为键值对列表。我无法理解 :) 谢谢 :) Rhanks 我正在尝试将您的示例融入我的场景,但仍然感到困惑:这里有 const SearchResultType = new GraphQLObjectType( name: "SearchResult", fields: ()=>( total_rows: type: GraphQLInt , bookmark: type: GraphQLString , //rows: type: new GraphQLList(GraphQLInt) , num_of_rows: type: GraphQLInt , rows: type: GraphQLJSON , counts: created_by_ref: 类型:键:ID计数:Int ) ); 你是这个意思吗? codesandbox.io/s/polished-http-351sm 也许这有帮助? 感谢您的一百万时间以上是关于graphql中未知对象的模式类型的主要内容,如果未能解决你的问题,请参考以下文章
GraphQL - 如果父返回类型是对象列表,则模式拼接上的模式委派不起作用
Gatsby Contentful - GraphQL 查询错误:未知类型“ContentfulFixed”
出现错误:“Query”类型的“user”字段上的未知参数“id”[GraphQL,Relay,React]