如何在GraphQl中定义Java地图?

发布于 2025-01-23 02:48:26 字数 676 浏览 2 评论 0原文

我有一个filtermodel是映射map< string,columnfilter> FILTERMODEL为了过滤我的演员数据。当我尝试过滤数据时,我会从客户端获取一个filtermelodel对象。如何在GraphQl查询中定义此Java地图?

来自客户端的FILTERMODEL对象:

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

架构:

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
}

I have a filterModel which is a Map Map<String, ColumnFilter> filterModel in order to filter my Actor data. I get an filterModel Object from the client when I try to filter my data. How do I define this Java Map in my graphql Query?

filterModel Object from client:

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2025-01-30 02:48:26

GraphQl中没有地图类型。因为地图基本上包含动态键,并且它们在GraphQL期望的静态类型中的发挥作用不佳。

如果您真的不确定该数据将在该地图中使用哪种类型,则可以随时将该动态信息存储为字符串。这是我个人会做的。

因此,您的FILTERMODEL将成为
filtermodel:字符串,该字符串将是json对象的表示。您可以在代码中进行JSON解析以将其转换回MAP

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.

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. 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

清风挽心 2025-01-30 02:48:26

0输入

mutation{ xxxx(employeeBean:{ xxxxx , attributes: {\"key\":\"value\"}" }) {
xxxxxxx attributes }}

1. SCHEMA文件

scalar JSON
type Employee {
   
   ........
   attributes: JSON
}

1.配置

@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
    return wiringBuilder -> wiringBuilder
            .scalar(jsonScalar());
}

public GraphQLScalarType jsonScalar() {
    ObjectMapper objectMapper = new ObjectMapper();
    return GraphQLScalarType.newScalar()
            .name("JSON") //graphql type define in the schema file
            .description("Java MAP as scalar.")
            .coercing(new Coercing<Map<String, String>, String>() {
                @Override
                public String serialize(final Object dataFetcherResult) {
                    if (dataFetcherResult instanceof Map) {
                        try {
                            return objectMapper.writeValueAsString(dataFetcherResult);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        throw new CoercingSerializeException("Expected a Map object.");
                    }
                }

                @Override
                public Map<String, String> parseValue(final Object input) {

                    if (input instanceof StringValue) {
                        try {
                            return objectMapper.readValue(input.toString()
                                    , new TypeReference<Map<String, String>>() {
                                    });


                        } catch (JsonProcessingException e) {
                            throw new CoercingParseLiteralException(e);
                        }
                    } else {
                        throw new CoercingParseValueException("Expected a String");
                    }
                }

                @Override
                public Map<String, String> parseLiteral(final Object input) {
                    if (input instanceof StringValue) {
                        try {
                            return objectMapper.readValue(((StringValue) input).getValue()
                                    , new TypeReference<Map<String, String>>() {
                                    });
                        } catch (JsonProcessingException e) {
                            throw new CoercingParseLiteralException(e);
                        }
                    } else {
                        throw new CoercingParseLiteralException("Expected a StringValue.");
                    }
                }
            }).build();

}

3 bean

public class EmployeeBean {

private Map<String,String> attributes;
}

Note

GraphQlargumentBinder-&gt;公共对象绑定(
dataFetchingEnvironment环境,@nullable String参数名称,ResolvableType targetType)
投掷bindexception {}
无法将值绑定到地图对象,因为它使用映射名称在模式中定义,然后将DOT放置,然后将第一个密钥名称和搜索该名称搜索在BEAN中找到地图,并且目前尚未工作

0 input

mutation{ xxxx(employeeBean:{ xxxxx , attributes: {\"key\":\"value\"}" }) {
xxxxxxx attributes }}

1.schema file

scalar JSON
type Employee {
   
   ........
   attributes: JSON
}

1.configuration

@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
    return wiringBuilder -> wiringBuilder
            .scalar(jsonScalar());
}

public GraphQLScalarType jsonScalar() {
    ObjectMapper objectMapper = new ObjectMapper();
    return GraphQLScalarType.newScalar()
            .name("JSON") //graphql type define in the schema file
            .description("Java MAP as scalar.")
            .coercing(new Coercing<Map<String, String>, String>() {
                @Override
                public String serialize(final Object dataFetcherResult) {
                    if (dataFetcherResult instanceof Map) {
                        try {
                            return objectMapper.writeValueAsString(dataFetcherResult);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        throw new CoercingSerializeException("Expected a Map object.");
                    }
                }

                @Override
                public Map<String, String> parseValue(final Object input) {

                    if (input instanceof StringValue) {
                        try {
                            return objectMapper.readValue(input.toString()
                                    , new TypeReference<Map<String, String>>() {
                                    });


                        } catch (JsonProcessingException e) {
                            throw new CoercingParseLiteralException(e);
                        }
                    } else {
                        throw new CoercingParseValueException("Expected a String");
                    }
                }

                @Override
                public Map<String, String> parseLiteral(final Object input) {
                    if (input instanceof StringValue) {
                        try {
                            return objectMapper.readValue(((StringValue) input).getValue()
                                    , new TypeReference<Map<String, String>>() {
                                    });
                        } catch (JsonProcessingException e) {
                            throw new CoercingParseLiteralException(e);
                        }
                    } else {
                        throw new CoercingParseLiteralException("Expected a StringValue.");
                    }
                }
            }).build();

}

3 bean

public class EmployeeBean {

private Map<String,String> attributes;
}

Note

GraphQlArgumentBinder-> public Object bind(
DataFetchingEnvironment environment, @Nullable String argumentName, ResolvableType targetType)
throws BindException {}
not able to bind the value to the map object because its using map name define in the schema and then put dot and then the first key name and searching of that name in the bean to find the map and its not working as of now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文