# GraphQLJava Client: Generating Models on the Fly for GraphQL Schema

**URL:** <https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705>\
**Category:** Client SDKs\
**Tags:** client, mobile, kotlin\
**Created:** [June 10, 2022, 5:32am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705 "2022-06-10T05:32:12Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 10, 2022, 5:32am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/1 "2022-06-10T05:32:12Z")

</div>

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)](https://javadoc.io/static/com.apollographql.apollo/apollo-runtime/2.5.11/overview-tree.html) 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

```auto
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!!

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 10, 2022, 6:21am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/2 "2022-06-10T06:21:58Z")

</div>

Hi 👋

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

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 10, 2022, 6:54am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/3 "2022-06-10T06:54:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 10, 2022, 7:09am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/4 "2022-06-10T07:09:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 13, 2022, 1:25pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/5 "2022-06-13T13:25:52Z")

</div>

> [@mbonnin](#):
>
> `generateApolloSources`

@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.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 13, 2022, 1:41pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/6 "2022-06-13T13:41:39Z")

</div>

I’m not super well versed in the maven plugin. Are you using [this maven plugin](https://github.com/aoudiamoncef/apollo-client-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](https://github.com/aoudiamoncef/apollo-client-maven-plugin#getting-started)? Can you share a small project somewhere?

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 13, 2022, 5:52pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/7 "2022-06-13T17:52:12Z")

</div>

@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

```auto
<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!!

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 13, 2022, 6:07pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/8 "2022-06-13T18:07:07Z")

</div>

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](https://github.com/aoudiamoncef/apollo-client-maven-plugin/releases/tag/v4.0.5)

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 14, 2022, 8:05am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/9 "2022-06-14T08:05:58Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 14, 2022, 8:24am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/10 "2022-06-14T08:24:38Z")

</div>

Hi 👋

> My code is shared @ [https://git.corp.adobe.com/ashudsha/graphql-client](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](https://github.com/aoudiamoncef/apollo-client-maven-plugin/issues/new).

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 14, 2022, 8:33am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/11 "2022-06-14T08:33:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 14, 2022, 11:32am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/12 "2022-06-14T11:32:37Z")

</div>

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](https://github.com/aoudiamoncef/apollo-client-maven-plugin/issues/60) for the plugin here. I have also shared the code in the public repository [GitHub - ashudeep-sharma/graphql-client](https://github.com/ashudeep-sharma/graphql-client) . I hope this will be accessible to users.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 14, 2022, 11:44am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/13 "2022-06-14T11:44:18Z")

</div>

> I have also shared the code in the public repository [GitHub - ashudeep-sharma/graphql-client](https://github.com/ashudeep-sharma/graphql-client) . I hope this will be accessible to users.

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

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 14, 2022, 3:17pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/14 "2022-06-14T15:17:25Z")

</div>

FWIW, I could get the plugin to generate code with [these changes](https://github.com/ashudeep-sharma/graphql-client/pull/1). There are other issues after that but I think that should be a good start to get your GraphQL code running.

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 14, 2022, 5:48pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/15 "2022-06-14T17:48:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [June 15, 2022, 8:02am UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/16 "2022-06-15T08:02:41Z")

</div>

You got an answer [in the maven plugin issue](https://github.com/aoudiamoncef/apollo-client-maven-plugin/issues/60#issuecomment-1156049932). Let’s follow up there (if needed)!

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [June 15, 2022, 3:32pm UTC](https://community.apollographql.com/t/graphqljava-client-generating-models-on-the-fly-for-graphql-schema/3705/17 "2022-06-15T15:32:56Z")

</div>

Sure Thanks for sharing all the details and Resolutions 🙂
