SubSelectionRequired 类型的验证错误:字段时间戳类型需要子选择
Posted
技术标签:
【中文标题】SubSelectionRequired 类型的验证错误:字段时间戳类型需要子选择【英文标题】:Validation error of type SubSelectionRequired: Sub selection required for type Timestamp of field 【发布时间】:2019-09-20 12:09:59 【问题描述】:我使用 GraphQL-SPQR 库 问题是“SubSelectionRequired 类型的验证错误:Timestamp 类型需要子选择” 时间戳查询中可能有表达式 或Entity中的格式
"query":
"findUserPointByUserId(userId:73)rowNum userAccountPointUserId totalPoint pointTypeDescription point userAccountCreatedDate findUserAccountImgByUserId(userId:73)imageId,userId,presentImgNum"
错误
"errors": [
"message": "Validation error of type SubSelectionRequired: Sub selection required for type Timestamp of field userAccountCreatedDate",
"locations": [
"line": 1,
"column": 103
]
]
实体
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "view_user_account_point", schema = "public", catalog = "corus")
public class ViewUserAccountPoint
@Id
@Basic
@GraphQLQuery(name = "rowNum")
@Column(name = "row_num", nullable = true)
private Long rowNum;
@Basic
@Column(name = "user_account_point_userid", nullable = true)
@GraphQLQuery(name = "userAccountPointUserId")
private Integer userAccountPointUserId;
@Basic
@Column(name = "subject_id", nullable = true)
@GraphQLQuery(name = "subjectId")
private Integer subjectId;
@Basic
@Column(name = "point", nullable = true)
@GraphQLQuery(name = "point")
private Integer point;
@Basic
@Column(name = "user_account_point_typeid", nullable = true)
@GraphQLQuery(name = "userAccountPointTypeId")
private Integer userAccountPointTypeId;
@Basic
@Column(name = "date_created", nullable = true)
@GraphQLQuery(name = "userAccountCreatedDate")
private Timestamp userAccountCreatedDate;
服务
public List<ViewUserAccountPoint> findUserPointByUserId(@GraphQLArgument(name = "userId") Integer userId)
return viewUserAccountPointRepository.findByUserAccountPointUserIdOrderByUserAccountCreatedDateDesc(userId);
控制器
private final GraphQL graphQL;
public UserController(UserAccountService userAccountService)
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withResolverBuilders(
//Resolve by annotations
new AnnotatedResolverBuilder())
.withOperationsFromSingleton(userAccountService,UserAccountService.class)
.withValueMapperFactory(new JacksonValueMapperFactory())
.generate();
graphQL = GraphQL.newGraphQL(schema).build();
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> graphql(@RequestBody Map<String, String> request, HttpServletRequest raw)
ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput()
.query(request.get("query"))
.operationName(request.get("operationName"))
.context(raw)
.build());
return executionResult.toSpecification();
我搜索所有查询时间戳格式 但是,我找不到 我希望听到解决方案。 谢谢
【问题讨论】:
【参考方案1】:出于某种原因,Timestamp
的映射不正确。它最终成为一个对象而不是一个标量。
正如the issue you opened 中所提到的,您的代码中的Timestamp
来自何处尚不清楚。
java.sql.Timestamp
在 GraphQL SPQR 的最新版本中是 supported out of the box,因此您可能使用的是旧版本。
如果不是这样,则意味着Timestamp
不是java.sql.Timestamp
,您需要为其注册一个自定义映射器。
public class TimestampMapper implements TypeMapper
// Define the scalar as needed, see io.leangen.graphql.util.Scalars for inspiration
private static final GraphQLScalarType TIMESTAMP = ...;
@Override
public GraphQLOutputType toGraphQLType(AnnotatedType javaType, OperationMapper operationMapper, Set<Class<? extends TypeMapper>> mappersToSkip, BuildContext buildContext)
return TIMESTAMP; //it's important to always return the same instance
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, OperationMapper operationMapper, Set<Class<? extends TypeMapper>> mappersToSkip, BuildContext buildContext)
return TIMESTAMP; //same as above
@Override
public boolean supports(AnnotatedType type)
return ClassUtils.isSuperClass(Timestamp.class, type);
然后注册你的映射器:
generator.withTypeMappers(new TimestampMapper())
【讨论】:
【参考方案2】:对于我的情况,查询正文不正确,请确保您有正确的。
【讨论】:
以上是关于SubSelectionRequired 类型的验证错误:字段时间戳类型需要子选择的主要内容,如果未能解决你的问题,请参考以下文章