Java move generated classes to src

I was wondering if its possible to have the generated classes from the schema to go into my src directory for java, its impossible to debug and test my project from my IDE currently because the build dir isn’t compiled with the project, so my IDE thinks the file doesnt exist
image

I was wondering if its possible to have the generated classes from the schema to go into my src directory

It might be possible to set the outputDir gradle property but I’m not 100% sure and on mobile right now. It’s usually better to keep generated files in the build folder.

its impossible to debug and test my project from my IDE

That sounds like the real bug. Generated sources should definitely ve debuggable. Do you have a reproducer project I could look into?

It might be possible to set the outputDir gradle property but I’m not 100% sure and on mobile right now.

Turns out it is possible. You can do something like this:

apollo {
  packageName.set("com.example")
  outputDir.set(layout.projectDirectory.dir("src/java/generated"))
}

I would advise against this though as it’s more work to keep the generated files in sync with the queries and duplicates information in your repository

Hm, I’ll try it out and see how it works, but it is really annoying to not be able to run the project inside my IDE because the files aren’t generated in the working directory.

I forgot, but when generating the sources, I always get this error

image

it is really annoying to not be able to run the project inside my IDE because the files aren’t generated in the working directory

There’s definitely something wrong here. Plenty of folks are able to run the project inside the IDE. Can you share a bit more about your setup? Maybe share your Gradle build script if you can?

I forgot, but when generating the sources, I always get this error

You’ve hit this issue: https://youtrack.jetbrains.com/issue/KTIJ-21905/False-positive-is-not-within-its-bound-caused-by-Java-project-using-a-Kotlin-library. It shouldn’t impact building though. So far I’ve only seen is as an IDE issue. It’ll be fixed with Kotlin 1.8

This is my build gradle(kts), I added the outputdir like you said, but ive also tried using it without it.

plugins {
    id ("org.jetbrains.kotlin.jvm").version("1.6.21")
    id ("java")
    id("com.apollographql.apollo3").version("3.6.0")
}

group "dev.jacrispys"
version "Alpha-0.0.1"


repositories {
    mavenCentral()
    mavenCentral()
}



dependencies {
    implementation ("org.jetbrains.kotlin:kotlin-stdlib")
    implementation ("org.junit.jupiter:junit-jupiter-api:5.9.0")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.9.0")
    implementation ("org.json:json:20220320")
    implementation ("org.slf4j:slf4j-log4j12:2.0.1")
    implementation ("org.apache.logging.log4j:log4j-to-slf4j:2.19.0")
    implementation("com.apollographql.apollo3:apollo-runtime:3.6.0")
    implementation("com.apollographql.apollo3:apollo-rx3-support:3.6.0")


}

apollo {
    packageName.set("dev.jacrispys.generated")
    generateKotlinModels.set(false)
    outputDir.set(layout.projectDirectory.dir("src/test/java/generated"))
}

Looks like you’re generating java but also adding the Kotlin plugin. Either remove the Kotlin plugin or generate Kotlin models.
I’d recommend the second option if your project has kotlin support, it’ll integrate better.
Let me know how that works!

perfect I got it working! now I just need to supress those errors that intellij is sending!

oh one last thing, is there a way to auto generate the queries on java? It’s a bit of a pain to rewrite all the queries when they already exist in the schema, just not formed yet…

You can’t really form queries from the schema as they would have to query all the fields which would be way too much for most schemas out there. In simple cases maybe but we found out that this stops working really soon so it wasn’t worth adding

Ah, thats my problem, my schema is like 15k+ lines so a FULL single query would take HOURS to type out, I’ll have to figure out which variables I want to omit and which to keep.

(I am writing a java API for a graphql/WS client)

1 Like