GraphQLJava Client: Generating Models on the Fly for GraphQL Schema

Hello Experts,
I have been exploring the Apollo GraphQL Android Client and its really good to see the functionality working. I explored the Java Docs Class Hierarchy (apollo-runtime 2.5.11 API) as well.

I have a requirement in which i want to generate the Model at runtime. Elaborating it with an example. Lets say we have a Schema

query LaunchDetails($id:ID!) {
    launch(id: $id) {
        id
        site
        mission {
            name
            missionPatch(size:LARGE)
        }
    }
}

which we take from the user at runtime and want to generate the Model and then invoke the corresponding Query.

Do we have a direct API for this functionality? If not then what are the alternatives for doing this?

Thank You in Advance!!

Hi :wave:

This is not really possible. All the code generation has to happen at compile time or you loose the ability to check the types with the Kotlin compiler.
You can execute queries at runtime sending strings and parsing responses manually but that will loose a big part of the type safety guarantees apollo Kotlin provides

Thank You @mbonnin for the reasonable explanation, currently i am using Gradle Build to compile and generate classes. is there any simple command which will generate these classes with the GraphQL files placed in some folder?

You can use ./gradlew generateApolloSources to generate all the models

1 Like

@mbonnin : A quick question out of the way though, i can see that .

/gradlew generateApolloSources

or

./gradlew build

actually generate these classes, i am trying in the maven based application, how do i generate the sources in maven based application.
Is there a command for it or if i need to create a target, one other way is using a Java Program and invoking gradle utility, was wondering if it could be done through mvn as well.

i tried using

mvn clean install

and

mvn compile

but it doesn’t seems to be generating the models for the corresponding graphql files.

I’m not super well versed in the maven plugin. Are you using this maven plugin? I would expect mvn install to generate them but maybe I’m missing something. Did you add the plugin to your pom.xml as described in the doc? Can you share a small project somewhere?

@mbonnin : Sure let me extract the project as standalone and share it with you. Looking at the docs, it seems like its using Apollo3. I am trying to use Apollo Android Plugin 2.5.11 viz

<dependency>
            <groupId>com.apollographql.apollo</groupId>
            <artifactId>apollo-runtime</artifactId>
            <version>2.5.11</version>
</dependency>

Also i am initially tryin it out myself, but it seems like the path to place .graphql files seems to be hardcoded to src/main/graphql/SERVICE_NAME

give me some time, i will try to extract the project as standalone and share it with you but wanted to check on the concerns above.

Thank You!!

1 Like

Right, you might want to use version 4.x of the maven plugin for Apollo 2.x: Release v4.0.5 · aoudiamoncef/apollo-client-maven-plugin · GitHub

Hi @mbonnin I tried using the version 4.x of the plugin, but unfortunately hitting the following issue

Failed to execute goal com.github.aoudiamoncef:apollo-client-maven-plugin:4.0.5:generate (default) on project GraphQLClient: Execution default of goal com.github.aoudiamonc
ef:apollo-client-maven-plugin:4.0.5:generate failed.: NullPointerException → [Help 1]

My code is shared @ https://git.corp.adobe.com/ashudsha/graphql-client

I want to use to generate the Models through Maven utility also i want to understand if there is a limitation of putting all the .graphql files in src/main/graphql/ directory? Or this can be configured as well?

Thank You.

Hi :wave:

My code is shared @ https://git.corp.adobe.com/ashudsha/graphql-client

I can’t access this unfortunately

want to understand if there is a limitation of putting all the .graphql files in src/main/graphql/ directory

Not sure about this. For questions about apollo-client-maven-plugin, I’d suggest to open a new issue on that repo.

Sure Thank You, i will open a issue and add the code as well so as to get the required details.

Thanks Again!! Appreciate it.

So i have opened an issue apollo-client-maven-plugin:4.0.5:generate failed.: NullPointerException while generating Sources using Plugin · Issue #60 · aoudiamoncef/apollo-client-maven-plugin · GitHub for the plugin here. I have also shared the code in the public repository GitHub - ashudeep-sharma/graphql-client . I hope this will be accessible to users.

I have also shared the code in the public repository GitHub - ashudeep-sharma/graphql-client . I hope this will be accessible to users.

Yes, this works, thanks! I’ll look into it!

1 Like

FWIW, I could get the plugin to generate code with these changes. There are other issues after that but I think that should be a good start to get your GraphQL code running.

Yes @mbonnin this seems to work now after making the changes suggested by you.Thank You!!
Also want to check on how does other thing like downloading schema from server. I was using the following gradle command

.\gradlew downloadApolloSchema --endpoint="https://apollo-fullstack-tutorial.herokuapp.com/graphql" --schema="<Local_Schema_File_Path>"
Is there any API or introspection query as in what are the alternatives? If there is a doc link to it, please do share it.

You got an answer in the maven plugin issue. Let’s follow up there (if needed)!

Sure Thanks for sharing all the details and Resolutions :slightly_smiling_face: