Is it possible to change the default schema directory in Apollo Kotlin

I am trying to use Apollo Kotlin as GraphQL client for GraphQL queries.
Apollo Kotlin requires a schema in your module’s src/main/graphql directory.
All our Schemas are present in a separate git repo (say MyGraphQLSchema). To avoid duplication can apollo-kotlin read schemas from a different location or package ?

Hi :wave:

You can use schemaFile for this.

apollo {
  service("service") {
    schemaFile.set(file("shared/graphql/schema.graphqls"))
  }
}
1 Like

Thank you so much. it is indeed helpful. I have 2 follow up questions.

  1. Is it possible to give a directory to specify a set of Schema files (instead specifying each file individually). I tried schemaFiles.setFrom, but when i give a directory , gradle build fails with error > java.io.FileNotFoundException: /{My_Directory_Path}/ (Is a directory)

  2. Is there a similar way to specify operations (.graphql files) too from a different directory. Currently I add .graphql files only to the default directory src/main/graphql

For 1. I’m pretty sure there is some Gradle APIs to locate all given files inside a folder like fileTree:

apollo {
  service("service") {
    packageName.set("com.example")
    schemaFiles.from(fileTree("/").apply { include("**/*.graphqls") })
  }
}

For 2., you can use srcDir:

apollo {
  service("service") {
    packageName.set("com.example")
    srcDir(file("path/to/your/directory"))
  }
}

All that being said, note that it’s quite unusual to have multiple schema files for a single service. Usually there is a 1:1 mapping between a service and a schema file. And if your schema files are next to your operation files, you can just use srcDir and the Gradle plugin will locate the schema from their .graphqls extension

1 Like

This helped and solved my requirement. Thank you so much for the prompt and accurate answer.