Generate client code from introspection query response
See original GitHub issueIs your feature request related to a problem? Please describe. Always same context: we use this project to test our GraphQL API with RestAssured. We use your maven plugin for generating client code from schema file, before compiling and running RestAssured tests.
From our pom.xml for example:
<!-- Generate GraphQL client classes from schema. -->
<plugin>
<groupId>io.github.kobylynskyi</groupId>
<artifactId>graphql-codegen-maven-plugin</artifactId>
<version>1.8.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<graphqlSchemaPaths>${project.basedir}/src/integration-test/resources/schema.graphqls</graphqlSchemaPaths>
<outputDir>${project.build.directory}/generated-test-sources/graphql</outputDir>
...
It works great! But we have to write these schema file(s) that we use to generate this code client.
Describe the solution you’d like Can we imagine:
- the maven plugin does a HTTP request to the GraphQL API sending an introspection query.
- the graphql-java-codegen lib is able to generate the client code from this introspection query response (instead of only GQL schema).
Of course, this will be an additionnal feature to the plugin. The plugin must always support the generation from schema file.
I think it will be relevant, specially for generating client code in order to really use the GraphQL API (and not only for testing). Because, server and client aren’t probably in the same project. For example, it can be done with Swagger in command line: https://github.com/swagger-api/swagger-codegen#to-generate-a-sample-client-library
However, I don’t know if it is easily possible, because the result of introspection query has absolutely not the same format of the schema file.
Before thinking about details, I want to know if you think that it is relevant and possible ?
Describe alternatives you’ve considered The first step (retrieving introspection query response from API) maybe can be done by another maven plugin in order to simplify the feature. For example: https://github.com/cjnygard/rest-maven-plugin (I don’t take time to test this plugin, but I’m almost sure we can find a maven plugin which makes the job).
Thanks for your time, Laura
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Hi @kobylynskyi, I have a great news about this feature. In testing another GraphQL library for schemas stitching (graphql-braid), I’ve found a simple solution to generate GraphQL code from introspection query!
Indeed, this library offers the possibility to read SDL (so, .graphqls schemas like your library) or introspection result query in order to generate their own stuff. And this is easily possible thanks to methods available from graphql-java library:
The magic method: https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/introspection/IntrospectionResultToSchema.java#L67
This method takes the result of introspection query (as a
Map<String, Object>
) and provides agraphql.language.Document
object, exactly the same object you need inGraphQLDocumentParser
to create your own stuff (https://github.com/kobylynskyi/graphql-java-codegen/blob/master/src/main/java/com/kobylynskyi/graphql/codegen/GraphQLDocumentParser.java#L113).I’ve quickly tested this method in your project, in order to generate client code. I just rewrote the readDocument() method:
And it works!! 😃 I have exactly the same generated code from SDL than the introspection query result!
The mostly important part is this line (which makes all the job) :
My other lines are just about creating a String from a Reader… (There is probably another way to do that, but nevermind for the moment).
So, my question is: do you agree with this way to do?
Of course, it needs to have a option in Maven/Gradle plugins to say if it’s a GraphQL SDL or introspection query result (by default, we can keep the SDL value, in order to not have breaking changes). And it needs more tests to be sure that all features are supported.
If you are ok, I can make a PR 🙂
Laura
Yes cool! I will take a look tomorrow! Sorry for the delay, holidays … 😊