I’ve generated the Apollo classes successfully and can see them in the build directory, however they’re not available on the classpath. Strangely the Enum that is generated is available but the classes themselves aren’t.
Running the sample project provided on Apollo’s Github does work but I cant see the difference between the configurations.
mcve below.
6
3 Answers
Right now, you have src/main/graphql/
containing GraphQL documents. That means that your Java classes will be generated with no Java package, which probably is not what you want.
Instead, create a package tree under src/main/graphql/
(e.g., src/main/graphql/apollotest/mq/apollotest/api/
). Move the GraphQL documents and schema.json
there. Clean the project, and you should find that your Apollo-generated classes are available to you in whatever Java package you chose (e.g., apollotest.mq.apollotest.api
in the above example).
4
-
I did this but I still don't see the generated
.java
files in my project. I do see them in thebuild/generated/source/apollo/mypackage
directory though. I still can't reference them from IDEA. What can be the problem?– Adam AroldMar 17, 2018 at 15:08
-
@AdamArold: I don't use IDEA. Somehow, you need to teach it that
build/generated/source/apollo/
has sources to be included in your project.– CommonsWare
Mar 17, 2018 at 15:22
-
I think it is better to copy them over to the
src/main/java
folder, or not?– Adam AroldMar 17, 2018 at 16:44
-
Well, they will get regenerated when you change your GraphQL schema, your GraphQL queries, or upgrade Apollo-Android. So, IMHO, it is better to teach IDEA to pull from the generated source directory. Otherwise, you will forget to manually copy them again.
– CommonsWare
Mar 17, 2018 at 16:47
2
-
this is an incorrect diagram and didn't worked for me. what commonsware said was to create subfolders similar to our current architecture hierarchy . this folder system worked for me
– ansh sachdevaOct 11, 2020 at 9:22
-
@Andrii-Kovalchuk : what if internal module(app in above img) is ant project and external prj(GraphQlAndroidInFull) is in gradle. How we should generate build folder inside internal module in this case. I am using apollo client 3.1.0
– ridersingh92Feb 16, 2022 at 11:41
In my case, I had forgotten to include the package name in app level build.gradle
file
apollo {
// instruct the compiler to generate Kotlin models
generateKotlinModels.set(true)
packageNamesFromFilePaths("com.example.your_package_name")
}
After this I didn’t need to create the subfolders inside graphql
folder.
Why are you trying to edit or reference
build/generated
source code?May 23, 2017 at 17:58
I'm not trying to edit it, I'm trying to access the generated class. As in import the class.
May 23, 2017 at 17:59
Your
app/build.gradle
isn't compiling that as a dependency. I don't think you can import plugins within your code (at least I've never tried)May 23, 2017 at 18:01
Um, that doesn't look right. There should be package directories inside of
build/generated/source/apollo/
. What is the contents of yoursrc/main/graphql/
directory? If it is just the GraphQL document file directly, create a set of subdirectories matching your desired Java package for the generated code, and move the GraphQL file into there.May 23, 2017 at 18:02
@CommonsWare BTW, github.com/michaeljq/graphQlMCVE/tree/master/app/src/main/…
May 23, 2017 at 18:06