架构输入类型总是 "LinkedHashMap cannot be cast..."

Schema input type always "LinkedHashMap cannot be cast..."

我的 schema.grapqls 看起来像...

scalar BigDecimal
...
input TemplateLineItemInput {
    partMasterId: String!
    partMasterIdPath: String!
    actionType: String!
    actionQuantity: BigDecimal!
}
...
type Mutation {
    ...
    createLineItem(id: ID! input: TemplateLineItemInput!): TemplateBom
}

我的 POJO 都是 Lombok-ie,并且在我尝试创建 input.

之前一直运行良好
import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;

@Data
@Builder
public class TemplateLineItemInput {
  private final String partMasterId;
  private final String partMasterIdPath;
  private final String actionType;
  private final BigDecimal actionQuantity;
}

我相信我已经正确连接了突变...

@Component
public class TemplateBomDataWiring  implements RuntimeWiringBuilderCustomizer {
  private static final Logger LOG = LoggerFactory.getLogger(TemplateBomDataWiring.class);

  private final TemplateBomService templateBomService;

  public TemplateBomDataWiring(TemplateBomService templateBomService) {
    this.templateBomService = templateBomService;
  }

  @Override
  public void customize(RuntimeWiring.Builder builder) {
    builder.scalar(Scalars.GraphQLBigDecimal);
    builder.scalar(GraphQLDate);
    builder.type("Query", typeWiring -> typeWiring.dataFetcher("templateBom", env -> {
      final String id = env.getArgument("id");
      return templateBomService.getTemplateBom(id);
    }));
    builder.type("Mutation", typeWiring -> typeWiring.dataFetcher("create", env -> {
      final String partMasterId = env.getArgument("partMasterId");
      return templateBomService.createTemplateBom(partMasterId);
    }));
    builder.type("Mutation", typeWiring -> typeWiring.dataFetcher("createLineItem", env -> {
      final String id = env.getArgument("id");
      final TemplateLineItemInput input = env.getArgument("input");
      return templateBomService.createTemplateLineItem(id, input);
    }));
  }

但我无法在 GraphiQL 中获取要解析的查询。我没有堆栈跟踪服务器端,我只是在 GraphiQL 中得到这个 UI...

{
  "errors": [
    {
      "message": "class java.util.LinkedHashMap cannot be cast to class com.example.boa.domain.TemplateLineItemInput (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.example.boa.domain.TemplateLineItemInput is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @617a9621)",
      "locations": [
        {
          "line": 42,
          "column": 3
        }
      ],
      "path": [
        "createLineItem"
      ],
      "extensions": {
        "classification": "INTERNAL_ERROR"
      }
    }
  ],
  "data": {
    "createLineItem": null
  }
}

我的工作几乎与 https://www.graphql-java.com/documentation/v16/ 文档有关

如何让我的 input 类型映射到 graphql-java:16.2 上的 TemplateLineItemInput POJO?

您可以使用 spring 引导来自动定义您的运行时连接。 你可以通过实现

来编写查询获取器或变异获取器
GraphQLQueryResolver
GraphQLMutationResolver

如果您正在进行运行时连接,除了基本类型之外的复杂输入参数将始终作为 LinkedHashmap 中的键值对传递。 因此,您必须将传递的 linkedHashmap 转换为您期望的 class,如下所示。

ObjectMapper objectMapper = new ObjectMapper();
            String input = objectMapper.writeValueAsString(env.getArgument("input"));
            final String arg = env.getArgument("input");
            return templateBomService.createTemplateLineItem(id, objectMapper.readValue(arg,TemplateLineItemInput.class));