简体   繁体   English

如何在graphql中定义一个Java Map?

[英]How to define a Java Map in graphql?

I have a filterModel which is a Map Map<String, ColumnFilter> filterModel in order to filter my Actor data.我有一个filterModel ,它是一个 Map Map<String, ColumnFilter> filterModel以过滤我的 Actor 数据。 I get an filterModel Object from the client when I try to filter my data.当我尝试过滤我的数据时,我从客户端得到了一个 filterModel Object。 How do I define this Java Map in my graphql Query?如何在我的 graphql 查询中定义这个 Java Map?

filterModel Object from client:来自客户端的 filterModel Object:

filterModel: {
               firstname: {filterType: 'text', type: 'contains', filter: 'Tom'}, ...
              }

schema:模式:

type Actor {
    actorId: ID!,
    firstName: String,
    lastName: String,
}

type Query {
    rows(
        filterModel: [filterModel]
    ): [Actor]
}

input filterModel {
    key: String,
    value: ColumnFilter
}

input ColumnFilter {
    filterType: String,
    type: String,
    filter: String
}

There is no map type in GraphQL. Because maps are basically containing dynamic keys and, they do not play well into the static types that GraphQL is expecting. GraphQL 中没有 map 类型。因为映射基本上包含动态键,所以它们不能很好地适应 GraphQL 期望的 static 类型。

If you are really unsure of what type the data is going to be in that map, you can always store that dynamic information as a String.如果您真的不确定 map 中的数据是什么类型,您始终可以将该动态信息存储为字符串。 This is something I personally would do.这是我个人会做的事情。

So your filterModel would become filterModel:String and that string would be a JSON representation of the object. You can do the JSON parsing in the code to convert it back to a Map因此,您的 filterModel 将变为filterModel:String并且该字符串将是 object 的JSON表示。您可以在代码中执行 JSON 解析以将其转换回Map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM