在Spring中为GraphQL重写属性@SchemaMapping

问题描述 投票:0回答:1

我不知道如何覆盖我的实体中的属性。

给定这些类(getter 和 setter 等):

class Action {
    int Id;
    actionStatus ActionStatus;
}

class ActionStatus {
    int Id;
    String Code;
}

这个 GraphQL 模式:

type Action {
    id: Int
    actionStatus: String
}

我尝试使用如下方法解析 ActionController 中的 Action.actionStatus 属性:

@SchemaMapping
public String actionStatus(Action action) {
    return action.getStatus().getCode();
}

但是spring在获取status属性时会忽略该函数。 并收到无效输入的 Spring GraphGL 序列化错误

java spring spring-boot graphql-java spring-graphql
1个回答
0
投票

看来您没有正确定义您的合同。

根据您提供的类,

ActionStatus
是一个包含2个字段的复杂对象,但在您的
graphql
模式中,您已将其定义为字符串。

为了让您能够访问它的嵌套字段,您也应该在架构中定义它。

type Action {
  id: Int
  actionStatus: ActionStatus
}
 
type ActionStatus {
  id: Int!
  code: String!
}
© www.soinside.com 2019 - 2024. All rights reserved.