In a Spring Boot application, we're already having a fully functional GraphQL endpoint, serving .graphqls
files via GraphQL Java Tools (we included the graphql-spring-boot-starter
dependency) and handling the data resolution through our base Query
class implementing GraphQLQueryResolver
and subsequent GraphQLResolver
's.
For a business need, we have to re-create standard REST API endpoints, so I wonder why not just making calls to GraphQL (instead of having to re-implement "by hand" the data resolution once again)? And as it's in the same backend application, no need to make HTTP or servlet (ForwardRequest) calls, just call some API's in Java. The thing is I don't know how to proceed.
I read this example, but it's with basic GraphQL Java (not Tools): https://www.graphql-java.com/documentation/v9/execution/
I know this should possible because we are allowed to do this in tests: https://github.com/graphql-java-kickstart/graphql-spring-boot/blob/master/example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java
But how to do it in regular code? There is not such thing as a GraphQLTemplate
.
I also tried to search through examples at:
- https://github.com/graphql-java-kickstart/graphql-java-tools/tree/master/example
- https://github.com/graphql-java-kickstart/graphql-spring-boot
but found nothing relevant to our need.
Found nothing more in Documentation:
What did I miss? Ideally I'm looking to inject some GraphQLSomething
like this:
@RestController
@RequestMapping(path = "api")
public class CompanyController {
@Autowired
private GraphQLSomething graphQLSomething;
@GetMapping("company/{id}")
public ResponseEntity<?> societe(@PathVariable @NotNull Integer id) {
GraphQLSomethingResult result = GraphQLSomething.query("company(id: $id) { id name andsoone }", "{ \"id\": 123456 }").execute(); // not really sure of the GraphQL syntax here, but it'll need some tests...
return result.getDataOrElse();
}
}