9

I want to dive into graphQL using Java. I want to query an existing graphQL Service. From what I have found so far are ways to generate the graphQL schemafiles either in JSON or IDL from your POJO classes, but not the other way round....? From what I have understood so far, I would have to create the java classes myself that "represent" the objects I would receive from a query. But the service I want to use has tons of endpoints and queries and the need of writing each pojo class myself sounds like I have missed something... I assume there must be a way to generate stubs like I am used to from REST API frameworks using swagger or yaml files? So... how can I generate the pojo classes automagically given only the schemafile? I have read the description of the schema-first approach at graphql-java but they also assume to write the pojo-classes by oneself.

thank you

Yves030
  • 181
  • 2
  • 3
  • 6
  • What I've found so far, HTH: (1) [a similar SO question (with links in answer)](https://stackoverflow.com/q/49087643/3127111); (2) [Nodes, GraphQL for the JVM](https://americanexpress.io/graphql-for-the-jvm/); (3) [Nodes on GitHub](https://github.com/americanexpress/nodes); (4) [LiveGQL, subscriptions on WebSockets](https://github.com/billybichon/liveGQL); (5) [Awesome list of GraphQL & Relay](https://github.com/chentsulin/awesome-graphql) – watery Aug 29 '18 at 12:18

1 Answers1

1

You can use a plugin for your build tool to generate POJOs for GraphQL types/inputs/interfaces/enums/etc and interfaces for queries/mutations/subscriptions. All based on your GraphQL schema.

Link to the project: https://github.com/kobylynskyi/graphql-java-codegen

It has the following plugins for your build tool:

Bogdan Kobylynskyi
  • 1,150
  • 1
  • 12
  • 34
  • Hi, I tried the gradle plugin. One drawback I see for nested queries, suppose there is a product object with a nested Cost object, and a query for get product by id. The generated stub is for methdo productById(), which will always query the Cost object irrespective of cost being requested. Can we do away with this tight coupling? – Aashish Katta May 19 '20 at 06:41
  • Sure. You can create an issue in the following repo with the suggested approach: https://github.com/kobylynskyi/graphql-java-codegen – Bogdan Kobylynskyi May 19 '20 at 11:05