Skip to content

Retrofit converter which uses annotations to inject .graphql query or mutation files into a request body, along with any variables.

License

Notifications You must be signed in to change notification settings

AniTrend/retrofit-graphql

Repository files navigation

Retrofit Converter - With GraphQL Support     Codacy Badge   android-unit-test

Seeing how we already have a really powerful type-safe HTTP client for Android and Java Retrofit why not use it and extend it's functionality, this project is a retrofit converter which uses annotations to inject .graphql query or mutation file contents into a request body along with any GraphQL variables.

Why This Project Exists?

Many might wonder why this exists when an android GraphQL library like Apollo exists. Unfortunately Apollo for Android still lacks some basic but important features/functionality which led to the following questions about General Design Questions Regarding Apollo, Polymorphic Type Handling and Non Shared Types. Don't get me wrong Apollo is not inferior any way, it has amazing features such as:

  • Code Generation (Classes and Data Types)
  • Custom Scalar Types
  • Cached Responses

But since model classes are automatically generated for you, the developer loses some flexibility, such as use of generics, abstraction and inheritance. Also Android Performance best practice suggests that developers should use StringDef and IntDef over enums and here's why, with the exception of kotlin especially when using R8.

Strangely there are tons of simple examples all over Medium using apollo graphql for Android, but none of them address these issues because most of them just construct a simple single resource request demo application. These look just fine at first glance until you start working with multiple data types and apollo starts generating classes for every fragment and query even if the data models are the same, or share similar properties.

See a list of changes from here


How Everything Works

Getting Started

  • Add the JitPack repository to your build file
allprojects {
    repositories {
        ...
        maven { url 'https://www.jitpack.io' }
    }
}
  • Add the dependency
dependencies {
    implementation 'com.github.anitrend:retrofit-graphql:{latest_version}'
}
  • Optional R8 / ProGuard Rules

If you are using R8 the shrinking and obfuscation rules are included automatically.

ProGuard users must manually copy the options from this file.

You might also need retrofit rules and it's dependencies (OkHttp and Okio)

Examples

Define, your .graphql files for your GraphQL queries, fragments, and mutations e.t.c.

If you use insomnia you can use this tool to generate your workspaces into directories and files insomnia-graphql-generator. After you can simply place the generated content into your assets folder e.g.:

N.B. You might find this too useful too JetBrains JS GraphQL - Plugin

For more instructions on how to setup the sample app and other examples with file uploads, persisted queries, custom loggers, custom graphql files location (outside assets) please visit the projects wiki page

Screenshots