Reputation: 5418
My current project has switched to GraphQL API's and I wish to automate the generation of model objects that match both Query/Mutations requests/responses.
All I require is the model classes, I do not want to use tools such as Apollo at runtime in my Application.
I require model classes to be either Java or Kotlin.
I found this https://www.graphql-java-kickstart.com/tools/schema-definition/
however this appears to require me to create the model classes my self...
based on this statement "GraphQL Java Tools will expect to be given three classes that map to the GraphQL types: Query, Book, and Author. The Data classes for Book and Author are simple:
"
What am I missing?
When I attempt use Apollo-cli to download my schema I get this error
~ - $ npx apollo-cli download-schema $https://my.graphql.end.point/graphql --output schema.json
Error while fetching introspection query result: only absolute urls are supported
Surely this is an basic requirement when employing GraphQL
Upvotes: 4
Views: 3631
Reputation: 1888
So if I understand you correctly what you are trying to do is to a) download and locally create the schema from an existing graphql endpoint and b) create java model objects from this schema.
To download the schema you can use the graphql-cli
. First install via npm install -g graphql-cli
and run graphql init
to setup your .graphqlconfig
. Finally run graphql get-schema
to download the schema from the defined endpoint.
Next you want to leverage a Java code generator that takes the GraphQL schema and creates:
- Interfaces for GraphQL queries, mutations and subscriptions
- Interfaces for GraphQL unions
- POJO classes for GraphQL types
- Enum classes for each GraphQL enum
There are various options depending on your setup / preferences (e.g. gradle vs maven):
I recommend you to check out the first option, since it looks very well documented and also provides full flexibility after generating the desired helpers:
graphql-java-generator generates the boilerplate code, and lets you concentrate on what’s specific to your use case. Then, the running code doesn’t depend on any dependencies from graphql-java-generator. So you can get rid of graphql-java-generator at any time: just put the generated code in your SCM, and that’s it.
When in client mode, you can query the server with just one line of code.
For instance :
Human human = queryType.human("{id name appearsIn homePlanet friends{name}}", "180");
In this mode, the plugin generates:
One java class for the Query object, One java class for the Mutation object (if any), One POJO for each standard object of the GraphQL object, All the necessary runtime is actually attached as source code into your project: the generated code is stand-alone. So, your project, when it runs, doesn’t depend on any external dependency from graphql-java-generator.
Upvotes: 5