我使用io.leangen.graphql.spqr
库版本0.9.6
,我需要将来自Query
根节点的突变排除到文档中。
我的GraphQLController.kt
样子是这样的:
@RestController
class GraphQLController {
private var graphQL: GraphQL? = null
@Autowired
fun GraphQLController(bookGraph: BookGraph) {
val schema = GraphQLSchemaGenerator()
.withResolverBuilders(
AnnotatedResolverBuilder(),
PublicResolverBuilder("com.example.graphql.spqr"))
.withOperationsFromSingleton(bookGraph)
.withValueMapperFactory(JacksonValueMapperFactory())
.generate()
graphQL = GraphQL.newGraphQL(schema)
.build()
}
@PostMapping(value = ["/graphql"], consumes = [MediaType.APPLICATION_JSON_UTF8_VALUE], produces = [MediaType.APPLICATION_JSON_UTF8_VALUE])
@ResponseBody
fun execute(@RequestBody request: Map<String?, Any?>): ExecutionResult? {
return graphQL!!.execute(ExecutionInput.newExecutionInput()
.query(request["query"] as String?)
.variables(request["variables"] as Map<String, Object>?)
.operationName(request["operationName"] as String?)
.build())
我的BookGraph.kt
样子是这样的:
@Component
class BookGraph {
@Autowired
private lateinit var bookService: BookService
@GraphQLQuery(name = "books")
fun books() : List<Book> {
return bookService.findAll()
}
@GraphQLMutation(name = "createBook")
fun createBook(@GraphQLInputField(name = "name") name: String) : Book {
return bookService.findAll()
}
}
我该怎么做?
我在 StackOverflow 和SPQR 问题中都搜索了可能的解决方案,但找不到解决方案。
下面的查询根节点示例,我想排除createBook
:
虽然我希望 Mutation 根节点保持不变: