【问题标题】:InvalidSyntaxException passing GraphQL query parametersInvalidSyntaxException 传递 GraphQL 查询参数
【发布时间】: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


    【解决方案1】:

    解决了。问题来自 Voyager 依赖项:

    提出问题时的pom.xml:

            <!--         GraphQL -->
            <dependency>
                <groupId>com.graphql-java-kickstart</groupId>
                <artifactId>graphql-spring-boot-starter</artifactId>
                <version>${graphql.version}</version>
            </dependency>
    
            <!--         Voyager-->
            <dependency>
                <groupId>com.graphql-java-kickstart</groupId>
                <artifactId>voyager-spring-boot-autoconfigure</artifactId>
                <version>${voyager.version}</version>
                <scope>runtime</scope>
            </dependency>
    

    工作 pom:

            <!--         GraphQL -->
            <dependency>
                <groupId>com.graphql-java-kickstart</groupId>
                <artifactId>graphql-spring-boot-starter</artifactId>
                <version>${graphql.version}</version>
            </dependency>
    
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 2023-01-26
      • 2018-03-08
      • 2017-05-28
      • 2021-06-07
      • 1970-01-01
      • 2020-12-31
      • 2018-02-16
      相关资源
      最近更新 更多