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