How to GraphQL in Java

Alejandro Lazaro Gutierrez

io.leangen.graphql, com.graphql-java which one should i use?

Matt Raible

I recommend the one used in this post. It worked well for me.


Could you please share example for joins or multiple entities part of single query/mutation?

Matt Raible

You’ll probably have better luck asking this question on Stack Overflow. I don’t have an example handy.

Zverko Veselic

GraphQL is not SQL. With GraphQL you develop your API, not query your data layer. If you want to dump your DB to the web and query using SQL, then do that. No point in translating GraphQL to SQL and back.

Zverko Veselic

There is no schema document generated. It’s not needed. If you want it, you can inject GraphQLSchema and print it using graphql-java tools like SchemaPrinter. Or generate it from introspection.

Mike Ballesteros

Hi Karthikeya, how did you manage to capture “/graphql” in the interceptor? I´m struggling because when I perform a request to “/graphql” the interceptor is not able to catch it. Sorry if noob question, but I´d like to get a piece of advice. Thanks btw.


Hi Mike, I think the Interceptor is not registered with InterceptorRegistry. By registering our interceptor we can add lookup to a certain endpoints by specifying them in path patterns, as in our case it is “/graphql”. You should be implementing Spring WebMvcConfigurer for registering Interceptor.

InterceptorRegistryReference.addInterceptor(new CustomInterceptor()).addPathPatterns("/graphql");

I hope this helps and do let me know in more detail if above configuration does’nt work. Thank you!

Mike Ballesteros

Thanks, I´m gonna check that out. cheers!

Kushagra Bindal

Hi Matt,

Thanks for sharing such a useful tutorial. I have a very generic use case.

I am having a class Foo & Boo

public class Foo{
String a;
public class Boo extends Foo{
String b;

Boo will be my output object in .graphqls

I am able to get the response of Boo in this. But if I wanted to get the a also as the output of above query then how can we define the type and graphqls.

Please help in resolving this issue.

Matt Raible

Hello Kushagra,

I’m not a GraphQL expert, so I’m unsure how to do what you’re asking. I’d suggest asking your question on Stack Overflow and tagging it with “graphql”.

Himanjli Jain

Hi matt, thanku for such a compact yet so much understandable code.

I wanted to know that if I want to post the token along with my graphQL query, is there any way to do that?
In this code, you have generated the token and manually added it to the GUI. But I don’t have a GUI to involve. I have to acess data from AWS, for that I have to make a graphQL call. So how to pass headers and/or token along with the query?

Any help is appreciated, thank you.

Matt Raible

I’m guessing you want to do this in Java code? You might be able to use Spring WebClient for that. You might also check out Spring GraphQL, which was just released in the last month or so.