问题描述
我尝试用对象数组映射一个键字符串.
I try to map a key string with arrays of Objects.
我可以创建一个简单的对象,但我想在这些数组中轻松添加一个对象.地图对象非常适合执行此操作.
I can create a simple Object but i want to add easily an object in these arrays. The Map Object is perfect to do this.
问题:我不知道如何为 GraphQL 定义类型映射 :'(
Problem: I dont know how to define the Type Map for GraphQL :'(
@ObjectType()
export class Inventaire
@Field()
_id: string;
@Field()
stocks: Map<string, Article[]>;
}
推荐答案
GraphQL 是一种强类型语言,不提供任何开箱即用的 map 类型.键值对的 JSON blob 没有强大的架构,所以你不能有这样的东西:
GraphQL is a strongly-typed language, and does not provide any kind of map type out of the box. A JSON blob of key-value pairs do not have a strong schema, so you can't have something like this:
{
key1: val1,
key2: val2,
key3: val3,
...
}
但是,您可以定义一个 GraphQL Schema 以具有键值元组类型,然后定义您的属性以返回这些元组的数组.
However, you can define a GraphQL Schema to have a key-value tuple type, and then define your property to return an array of those tuples.
type articleMapTuple {
key: String
value: Article
}
type Inventaire {
stocks: [articleMapTuple]
}
那么您的返回类型将如下所示:
Then your return types would look something like this:
data [
{
key: foo1,
value: { some Article Object}
},
{
key: foo2,
value: { some Article Object}
},
{
key: foo3,
value: { some Article Object}
},
]
这篇关于在 GraphQL Schema 中定义地图对象的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!