Search code examples
javaspring-bootrestgraphqlgraphql-java

How to configure REST controller for graphql-java without datafetcher?


I'm currently using DataFetcher for GraphQL-Java in Springboot based on the tutorial here. This works well, but I'm looking for another way to get the endpoints without implementing DataFetchers, as it seems like extra work where I have to (1) implement the resolver method, then (2) create a separate method for the corresponding DataFetcher. Is there another way to consolidate this to expose my GraphQL API a la some form of Rest controller? I have looked around quite a bit but haven't found any workable solution. Preferably (not sure if it's related) there would be a better way of annotating the endpoint as well (currently provided in the linked example with RuntimeWiring, which I'm hoping to remove as well).

In short, I would like, in Springboot, to not need for RuntimeWiring and DataFetcher (primarily this so as to remove double code for the same method and improve maintainability), and instead have another way to configure the global REST controller for my GraphQL-Java code, while also having another way to annotate the endpoint (maybe some annotator on top of the implemented methods).

Thanks!


Solution

  • Managed to fix it by using graphql.kick.start.tools' SchemaParser and GraphQLResolver as follows:

    return SchemaParser.newParser()
            .files(schemaFiles)
            .scalars(scalars)
            .resolvers(resolvers)
            .dictionary(typeDictionary)
            .build()
            .makeExecutableSchema();
    

    where the resolvers were implemented using this amazing plugin as codegen.