eh3rrera / graphql-java-spring-boot-example

Sample GraphQL server implemented with graphql-java and Spring Boot
MIT License
211 stars 151 forks source link

Unable to debug your code in IDE #3

Closed themanojshukla closed 4 years ago

themanojshukla commented 6 years ago

I downloaded this repository, and run it as mentioned in README.md file. The run was successful, and executed few GraphQL queries too. But when tried to run the application in debug mode (with and without debug points) it fails, and error says..

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at com.example.DemoGraphQL.DemoGraphQlApplication.main(DemoGraphQlApplication.java:27) . . . . . . . Caused by: graphql.AssertException: Duplicated definition for field 'findAllBooks' in type 'Query' at graphql.schema.GraphQLObjectType.buildDefinitionMap(GraphQLObjectType.java:67) ~[graphql-java-6.0.jar:na] at graphql.schema.GraphQLObjectType.<init>(GraphQLObjectType.java:54) ~[graphql-java-6.0.jar:na] at graphql.schema.GraphQLObjectType$Builder.build(GraphQLObjectType.java:213) ~[graphql-java-6.0.jar:na]

So I debugged it at point where it checks for duplicates (i.e. at graphql.schema.GraphQLObjectType.buildDefinitionMap(GraphQLObjectType.java:67)) and there found duplicate entries in fieldDefinition and as per the code it should actually block the execution and should throw an Exception, which it does BUT this is happening only in debug mode, not at the time of running the app directly. No idea, Why??

Info: IDE used - IntelliJ

eh3rrera commented 6 years ago

Yes, someone mentioned this problem in one of the comments after the tutorial was published. Unfortunately, the site was redesigned and the comments were removed.

Here's the thread on Disqus: https://disqus.com/home/discussion/pluralsighttutorials/building_a_graphql_server_with_spring_boot/#comment-3752375744

Mitchell Meyer • 3 months ago I followed the tutorial and everything works great. However when I try and debug the application I get "Duplicated definition for field 'findAllBooks' in type 'Query'". Did you run into this problem as well? Thanks in advance

Esteban Herrera Mitchell Meyer • 3 months ago You're right, when I run the server in debug mode, an exception with that message is thrown at startup. I don't know why but the schema parser duplicates the definitions when the Book schema extends the Query type. If you remove the extends type part in book.grahpqls, the Query type will be overwritten and the author queries will be lost, so the solution is to put everything in one file and have only one Query and Mutation type with both book and author methods. It's probably a bug, I'm not sure. I'll try to find information about it. Thanks a lot!

I spent a couple of hours searching for a solution but didn't find one, except for the above answer. I don't know what's causing this.

I'll leave this issue open in case someone could help us.

themanojshukla commented 6 years ago

Hey,.... I got it working in debug mode.

I switched to Java 8, changed pom file properties to version 8. and then built the project and now run it in debug mode, it works perfectly fine..!!

Now, the point is, what magic happening in Java 9 and Debug combination.?

eh3rrera commented 6 years ago

Great, thanks!

Yes, it's weird. Maybe the version of graphql-java I used wasn't totally compatible with Java 9. This happened with Spring Boot 2. At the time I built this application, the final version of Spring Boot 2 was not ready and therefore, it was not supported.

Maybe it would be worth to upgrade the libraries to the latest versions (maybe Java 10 also) to see if this still happens. I'll try to test it this week.

Thanks!