question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate client code from introspection query response

See original GitHub issue

Is 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:

  1. the maven plugin does a HTTP request to the GraphQL API sending an introspection query.
  2. 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lmartellottocommented, Jul 23, 2020

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 a graphql.language.Document object, exactly the same object you need in GraphQLDocumentParser 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:

    private static Document readDocument(List<String> schemaPaths) throws IOException {
        try (MultiSourceReader reader = createMultiSourceReader(schemaPaths)) {
            TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
            int intValueOfChar;
            String targetString = "";
            while ((intValueOfChar = reader.read()) != -1) {
                targetString += (char) intValueOfChar;
            }
            reader.close();
            
            HashMap<String,Object> map = new ObjectMapper().readValue(targetString, typeRef);
            return new IntrospectionResultToSchema().createSchemaDefinition(map);
           // return GRAPHQL_PARSER.parseDocument(reader);
        }
    }

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) :

            return new IntrospectionResultToSchema().createSchemaDefinition(map);

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

1reaction
lmartellottocommented, Aug 22, 2020

Yes cool! I will take a look tomorrow! Sorry for the delay, holidays … 😊

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code generation - Apollo GraphQL Docs
To generate models, Apollo iOS requires two input sources: A GraphQL schema. The GraphQL schema is a list of all of the types...
Read more >
Remote GraphQL Schema Introspection Codegen - YouTube
Automatically introspect your stitched GraphQL schemas endpoints, and use the ... requests to remote schemas with the GraphQL Code Generator.
Read more >
Introspection - GraphQL
A query language for your API — GraphQL provides a complete description of the data in your API, gives clients the power to...
Read more >
API (GraphQL) - Client code generation - AWS Amplify Docs
The amplify codegen statements command generates GraphQL statements(queries, mutation and subscription) based on your GraphQL schema. This command downloads ...
Read more >
urql-introspection plugin query does not match the ... - GitHub
import the schema into urql cacheExchange; add the devtoolsExchange in urql client; load the app in a browser; Result: app will load, then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found