【发布时间】:2022-02-18 00:28:46
【问题描述】:
尝试实现能够执行以下 graphql 查询的代码:
query FIND_BY_BATCHID_LIMIT {
findByBatchIdAndLimit(batch_id: 10, limit: 5) {
id
first_name
last_name
}
}
所以下面的实现:
query.graphqls
type Query {
findByBatchIdAndLimit(batch_id: Int!, limit: Int!): [RawEntity]
}
存储库和查询
@Repository
public interface RawRepository extends JpaRepository<RawEntity, Integer> {
@Query(value = "SELECT * FROM rawdata where batch_id = :batch_id LIMIT :limit", nativeQuery = true)
List<RawEntity> findByBatchIdAndLimit(Integer batch_id, Integer limit);
}
@Component
public class Query implements GraphQLQueryResolver {
@Autowired
private RawRepository repository;
public List<RawEntity> findByBatchIdAndLimit(Integer batch_id, Integer limit) {
return repository.findByBatchIdAndLimit(batch_id, limit);
}
}
但是得到一个:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is graphql.parser.InvalidSyntaxException: Invalid Syntax : offending token '!' at line 46 column 11
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 139 common frames omitted
Caused by: graphql.parser.InvalidSyntaxException: Invalid Syntax : offending token '!' at line 46 column 11
虽然以下查找器实现工作:
query.graphqls
type Query {
findByLimit(limit: Int!): [RawEntity]
}
存储库和查询
@Repository
public interface RawRepository extends JpaRepository<RawEntity, Integer> {
@Query(value = "SELECT * FROM rawdata LIMIT :limit", nativeQuery = true)
List<RawEntity> findByLimit(Integer limit);
}
@Component
public class Query implements GraphQLQueryResolver {
@Autowired
private RawRepository repository;
public List<RawEntity> findByLimit(Integer limit) {
return repository.findByLimit(limit);
}
}
query FIND_BY_LIMIT {
findByLimit(limit: 5) {
id
first_name
last_name
}
}
这可能是我传递 batch_id 的方式,限制参数,但谷歌搜索,听起来是传递参数的方式。
有什么想法吗?
【问题讨论】:
标签: spring spring-boot jpa graphql