4

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.

https://github.com/michaeljq/graphQlMCVE

enter image description here

Michael.
  • 980
  • 12
  • 16
  • Why are you trying to edit or reference `build/generated` source code? – OneCricketeer May 23 '17 at 17:58
  • I'm not trying to edit it, I'm trying to access the generated class. As in import the class. – Michael. May 23 '17 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) – OneCricketeer May 23 '17 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 your `src/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. – CommonsWare May 23 '17 at 18:02
  • @CommonsWare BTW, https://github.com/michaeljq/graphQlMCVE/tree/master/app/src/main/graphql – OneCricketeer May 23 '17 at 18:06
  • @cricket_007: Yeah, I noticed that shortly after posting the comment, then was busy writing an answer... Thanks! – CommonsWare May 23 '17 at 18:07

2 Answers2

16

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I did this but I still don't see the generated `.java` files in my project. I do see them in the `build/generated/source/apollo/mypackage` directory though. I still can't reference them from IDEA. What can be the problem? – Adam Arold Mar 17 '18 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 '18 at 15:22
  • I think it is better to copy them over to the `src/main/java` folder, or not? – Adam Arold Mar 17 '18 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 '18 at 16:47
0

Here is a screenshot to understand the solution faster: Project structure

Andrii Kovalchuk
  • 4,351
  • 2
  • 36
  • 31
  • 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](https://imgur.com/AC0IKim) worked for me – ansh sachdeva Oct 11 '20 at 09: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 – Ashutosh Singh Feb 16 '22 at 11:41