How to GraphQL in Java

How to GraphQL in Java

This tutorial shows how to build and test a GraphQL API with Java and JUnit 5.

sankar singu

public List<food> getFoods() {
can we change the return type to List<jsonobject> after that, query field is put in Json.put(“queryfield”), how can we do that?

Matt Raible

Hello Sankar,

Why do you want to do this? It think it’s better to have a strongly-typed response.

Nguyễn Quân

problem: “Error creating bean with name ‘graphQLSchema’” when i add depdencies graphql-spqr-spring-boot-starter


You’ll really have to be more specific about the error.


I want to consume GraphQL API’s in java, can you direct me to some example resources.

Matt Raible

I googled for “java graphql client” and found…. I haven’t used it myself, but it looks like it’ll work.

abhilash alampalli

Hi, I have rest api end point . I want wrap this rest api with graphql is there a step by step process in spring boot for this.

Matt Raible

No. You should choose REST or GraphQL for your endpoint rather than trying to mix and match.



I have added Handler Interceptor for pre-validating the header information while calling
/graphql endpoint, and when i return false from the interceptor i am throwing an InvalidRequestException ::

but results in a error as

Circular view path [graphql]: would dispatch back to the current handler URL [/graphql] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

How can i send a error response so that in graphql gui it appears as

“code” : 400,
“message”: “Invalid Version.Please update to latest version”

any suggestion or idea will be a lot of Help, Thank you!.


Resloved my issue instead of throwing an exception when condition fails in preHandle method, i have set error message to HttpServeletResponse reference using getWriter() method, it perfectly renders the error message response to the graphQL gui :slight_smile:

Matt Raible

Cool! Thanks for letting us know how you fixed it.

Amir Shachar

Where does the schema get exported to? What if I wanted to use the schema file? Where is it generated?

Ehsan Moradi

Hi, Is it possible to use filter such as greater than, less than or not-equal, etc. in this library or any other java library?

Matt Raible

Yes. See this answer on Stack Overflow.

Ehsan Moradi

Thanks, I use this method, but this library does not provide “where” clause or anything like “equal”, “not-equal”, “less-than”, … . I found this
graphql-jpa-query but is not not fit my needs too. it use jpa entities directly, i would like do some work on retrieved data and then publish them, also no security can attached.

Matt Raible

I’m sorry. I don’t know how to do what you’re asking for. I’d have to do my own research to figure it out. We’ll consider adding it our next blog post on GraphQL in Java. If you figure out a solution in the meantime, please let us know.

Anoj Kunes

Hi Matt, thanks for this amazing post. Have you ever tackled on the schema stitching and merging for remote schemas using graphql spqr and how would you create that bean along with graphql braid library or java nadel library for microservices?

Matt Raible

No, I have not.

Tony B

Never been willing to take the risk to jump to GraphQL for external API use. Looks nice for internal stuff tho. The security seems like an afterthought to me; especially in Java.

It is hard enough dealing with number 8 on the OWASP Top Ten, without adding an extra layer of JSON deserialization, constructor calls, and immature libraries to the mix. Does look like fun tho. :slight_smile:…