kotlin - Page 类的 graphql 架构定义应该是什么样子?

标签 kotlin spring-data graphql graphql-java

所以我在 kotlin 中有这个类:

@Component
class UserResolver @Autowired
constructor(private val userService: UserService): GraphQLMutationResolver, GraphQLQueryResolver {

    fun createUser(user: User): User {
        userService.save(user)
        return user
    }

    fun users(): Page<User> {
        val pageable = QPageRequest(0, 10)

        return userService.all(pageable)
    }
}

我希望方法用户返回 Page 对象,我对 graphql 完全陌生。我尝试过这样的事情:

type Page {
    number: Int
    size: Int
    numberOfElements: Int
    content: []
    hasContent: Boolean
    isFirst: Boolean
    isLast: Boolean
    hasNext: Boolean
    hasPrevoius: Boolean
    totalPages: Int
    totalElements: Float
}

但是我的 Spring Boot 应用程序无法启动,我不知道这个类的架构定义是什么 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Page.html应该看起来像。有人有想法吗?

编辑:错误是:

Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.coxautodev.graphql.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is com.coxautodev.graphql.tools.TypeClassMatcher$RawClassRequiredForGraphQLMappingException: Type org.springframework.data.domain.Page<sk.matusko.wixit.common.dao.User > cannot be mapped to a GraphQL type! Since GraphQL-Java deals with erased types at runtime, only non-parameterized classes can represent a GraphQL type. This allows for reverse-lookup by java class in interfaces and union types.

最佳答案

过去几个月,GraphQL Java 工具对泛型的处理发生了一些变化。您使用什么版本?我刚刚在 5.4.1 版本上尝试过,它似乎可以工作,但在 5.2.4 版本上却不行。我想知道是否修复了 this问题可能相关。

如果有帮助,这是我使用的测试 Material :

首先,我声明了一个这样的类:class Page<T>(val number: Int, val size: Int)

其次,在解析器中我有 fun users() = Page<User>(...)

第三,在 GraphQL 模式文件中我有这个

type Page {
    number: Int
    size: Int!
}

type Query {
    users: Page
}

上述的限制是 GraphQL 架构中只有一个类型,简称为 Page 。但想必您想要有关 User 的具体信息物体?这是否意味着在 content 中你的例子中的属性(property)?如果是这样,我认为您需要在 GraphQL 模式中为可能传递到 Page 泛型类型参数的每种类型声明一个单独的类型。 ,例如UserPage , CustomerPage等等。然后在代码中,每一个都需要映射到正确的 Kotlin 类,例如Page<User> , Page<Customer> 。如果没有每个泛型的具体实现,我不知道如何在代码中做到这一点(希望如果可能的话其他人可以解释如何做到这一点)。如果 GraphQL 类型名称具有相同的名称,则默认情况下它们会与 Kotlin 类名称结合,或者在构建 Schema 时使用显式提供的映射。使用SchemaParser.newParser().dictionary(...

因此,如果您乐意为提供给它的每种类型创建泛型类的具体子类,您可以执行以下操作:

open class Page<T>(val number: Int, val size: Int, val content: List<T>)

class UserPage(number: Int, size: Int, content: List<User>): Page<User>(number, size, content)

fun users(): UserPage {...

在 GraphQL 架构中,您将拥有以下内容:

type UserPage {
    number: Int!
    size: Int!
    content: [User]!
}

type Query {
    users: UserPage
}

关于kotlin - Page 类的 graphql 架构定义应该是什么样子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53472601/

相关文章:

java - Spring data Mongo 中用户聚合时排序不起作用

python - 在突变中传递链接数组 [graphene/python/graphql]

javascript - 嵌套的 React/Relay 组件没有收到 Prop

android - 如何处理 Jetpack Compose 中的导航?

android - 取消 ViewModel 的 onCleared() 上的所有 Kotlin 协程挂起作业

java - Spring Data Mongo - 继承和可嵌入

java - 将 Jackson JsonNode 嵌入存储在 CrudRepository 中的 POJO 中

vue.js - graphql-标签/加载器 : Module build failed with GraphQLError: Syntax Error

kotlin - 如何在 Kotlins "-> empty"中处理 "when"

angular - 发布到AWS S3预签名URL时的SignatureDoesNotMatch错误