Skip to content

Latest commit

 

History

History
192 lines (150 loc) · 6.11 KB

plugin-configuration.mdx

File metadata and controls

192 lines (150 loc) · 6.11 KB
title
Gradle plugin configuration

Apollo Kotlin's default configuration works for the majority of use cases. If you're getting started, see the getting started guide for an overview of the default Gradle configuration.

This article describes configuration options for advanced use cases when using Gradle.

Using multiple GraphQL APIs

Apollo Kotlin supports communicating with multiple GraphQL endpoints with different schemas. To do so, create multiple services like so:

apollo {
  service("starwars") {
    srcDir("src/main/graphql/starwars")
    packageName.set("com.starwars")
  }
  service("githunt") {
    srcDir("src/main/graphql/githunt")
    packageName.set("com.githunt")
  }
}

Specifying the schema location

Specify the location of your schema file using the schemaFile property:

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

By default, Apollo Kotlin combines all files in your project that match the pattern schema.[graphqls|json|sdl].

Combining multiple schema files

Apollo Kotlin supports a collection of client directives, including @nonnull, @optional, and @typePolicy. These directives enable you to extend your server's base schema with client-specific types and fields.

If you expand your schema in a separate file, you can instruct Apollo Kotlin to construct its schema from a combination of multiple files, like so:

apollo {
  service("service") {
    schemaFiles.set(setOf(file("shared/graphql/schema.graphqls"), file("shared/graphql/extra.graphqls")))
  }
}

By default, Apollo Kotlin combines all files in your project that match the pattern schema.[graphqls|json|sdl].

Wiring generated source

By default, Apollo Kotlin adds generated source:

  • to the main sourceSet for JVM projects
  • to commonMain for multiplatform projects
  • to all non-test variants for Android projects

You can customize this behavior with the outputDirConnection property. For example, to wire a service to the test source set of a Kotlin JVM project:

apollo {
  service("service") {
    outputDirConnection {
      connectToKotlinSourceSet("test")
    }
  }
}

Android variants support

It is sometimes useful to have different operations or schemas depending on the variant of your Android project.

To do this, you can instruct the Gradle plugin to automatically configure a Service per variant, like so:

apollo {
  createAllAndroidVariantServices(sourceFolder = ".", nameSuffix = "") {
    // Configure the service here
    packageName.set("...")
  }
}
  • sourceFolder where to find the GraphQL relative to "src/$sourceSetName/graphql". Pass "." to look into "src/$sourceSetName/graphql".
  • nameSuffix a suffix to use for the service name. Leave blank to use the variant name as is.

Similarly to what the Android variant system does with source code, the GraphQL files are handled additively, and files in src/main/graphql are included in all services. This means your project could look like this (e.g. when certain operations must only exist in debug builds):

- main
    - graphql
        - schema.graphqls // Schema for all variants
        - operations.graphql // Operations shared by all variants
- debug
    - graphql
        - operations.graphql // Operations specific to the 'debug' build type

Or for instance like this (specific backend per flavor):

- main
- demo
    - graphql
        - schema.graphqls // Schema for the 'demo' flavor
        - operations.graphql // Operations specific to the 'demo' flavor
- full
    - graphql
        - schema.graphqls // Schema for the 'full' flavor
        - operations.graphql // Operations specific to the 'full' flavor

If you have many variants and don't need to configure an Apollo Service for each one of them, it may be simpler to declare the Services manually, for instance:

apollo {
  service("debug") {
    srcDir(file("src/debug/graphql/"))
    packageName.set("com.example")
    outputDirConnection {
      connectToAndroidSourceSet("debug")
    }
  }
  service("release") {
    srcDir(file("src/release/graphql/"))
    packageName.set("com.example")
    outputDirConnection {
      connectToAndroidSourceSet("release")
    }
  }
}

Downloading a schema

By default, the Gradle plugin registers a downloadApolloSchema task that you can use from the command line:

# --schema is interpreted relative to the project's root directory (can also be an absolute path). This example
# assumes the root project directory and an Android app in `app`
./gradlew downloadApolloSchema \
  --endpoint="https://your.domain/graphql/endpoint" \
  --schema="app/src/main/graphql/com/example/schema.graphqls"

If you're doing this often or want to automate the process from CI, you can configure an introspection {} block:

apollo {
  service("starwars") {
    packageName.set("com.starwars")

    // This will create a downloadStarwarsApolloSchemaFromIntrospection task
    introspection {
      endpointUrl.set("https://your.domain/graphql/endpoint")
      // The path is interpreted relative to the current project here, no need to prepend 'app'
      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
    }
  }
}

This will create a task named download<ServiceName>ApolloSchemaFromIntrospection (downloadServiceApolloSchemaFromIntrospection by default).

If you register your schema with Apollo Studio, use the registry block instead:

apollo {
  service("starwars") {
    packageName.set("com.starwars")

    // This will create a downloadStarwarsApolloSchemaFromRegistry task
    registry {
      key.set(System.getenv("APOLLO_KEY"))
      graph.set(System.geten("APOLLO_GRAPH"))
      // The path is interpreted relative to the current project here, no need to prepend 'app'
      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
    }
  }
}

This will create a task named download<ServiceName>ApolloSchemaFromRegistry (downloadServiceApolloSchemaFromRegistry by default).