kotlingraphqlgraphql-spqr

How to exclude mutations from Query root node with spqr?


I use io.leangen.graphql.spqr library version 0.9.6 and I need to exclude mutations from Query root node into the Doc.

My GraphQLController.kt looks this way:

@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())

and my BookGraph.kt looks this way:

@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()
    }
}

How can I do it?

I searched for possible solutions both in StackOverflow and SPQR issues but cannot find a solution.

Example of Query root node below, I want to exclude createBook:

enter image description here

While I want Mutation root node to remain untouched:

enter image description here


Solution

  • It's bug. You're using a very old version of SPQR (Feb. 2018). This has been fixed a long long time ago. Please try to follow the releases as much as possible, as lots of things are getting fixed and improved.

    It is possible to work around the bug by customizing the ResolverBuilders, but I wouldn't recommend going that route.

    The Spring Starter (if even relevant to you) is currently lagging behind (not yet on the latest SPQR version) but I'm actively working on the new release. Should be out very soon.

    Btw, your setup has a lot of redundancy. Can be simplified to:

    val schema = GraphQLSchemaGenerator()
                    .withOperationsFromSingleton(bookGraph)
                    //replace with your own root package(s)
                    .withBasePackages("com.example.graphql.spqr")
                    .generate()