What Are The Drawbacks Of REST APIs? • Responses may not have enough data • Responses may have too much data • Responses are not controlled by the client @AdamMc331 #AndroidMakers 7
What Are The Drawbacks Of REST APIs? • Responses may not have enough data • Responses may have too much data • Responses are not controlled by the client • Any changes to responses have to go through another developer @AdamMc331 #AndroidMakers 7
Only Get What You Ask For // Query { pokemon(name: "bulbasaur") { name } } // Response { "data": { "pokemon": { "name": "Bulbasaur" } } } @AdamMc331 #AndroidMakers 10
What Are The Benefits Of GraphQL APIs? • Responses have just enough data • Responses are controlled by the client • Any changes to responses can be done by the client @AdamMc331 #AndroidMakers 12
Introspection Query 4 • A query that asks for meta data about a GraphQL Schema 4 https://graphqlmastery.com/blog/graphql-introspection-and-introspection-queries @AdamMc331 #AndroidMakers 22
Introspection Query 4 • A query that asks for meta data about a GraphQL Schema • Will return all of the possible queries and mutations 4 https://graphqlmastery.com/blog/graphql-introspection-and-introspection-queries @AdamMc331 #AndroidMakers 22
Introspection Query 4 • A query that asks for meta data about a GraphQL Schema • Will return all of the possible queries and mutations • Will return all of the models in a graph 4 https://graphqlmastery.com/blog/graphql-introspection-and-introspection-queries @AdamMc331 #AndroidMakers 22
Introspection Query 4 • A query that asks for meta data about a GraphQL Schema • Will return all of the possible queries and mutations • Will return all of the models in a graph • Foot note has a deep dive into these queries 4 https://graphqlmastery.com/blog/graphql-introspection-and-introspection-queries @AdamMc331 #AndroidMakers 22
Creating An Apollo Client val okHttpClient = OkHttpClient.Builder() .build() val apolloClient = ApolloClient.builder() .serverUrl("https://graphql-pokemon.now.sh/") .okHttpClient(okHttpClient) .build() @AdamMc331 #AndroidMakers 35
Use Kotlin Models // build.gradle or build.gradle.kts apollo { generateKotlinModels.set(true) // or false for Java models } @AdamMc331 #AndroidMakers 38
Query Code Is More Modern // repository.kt // No builder pattern val query = PokemonQuery(first = DEFAULT_LIMIT) // Use fields not methods val name = apolloPokemon.name val typeNames = apolloPokemon.types val image = apolloPokemon.image @AdamMc331 #AndroidMakers 39
A Common Problem /** * This class has a direct dependency on a REST API. * It returns a data class we defined. * Not the data class generated by Apollo. * This makes swapping them difficult. */ class PokemonListViewModel( private val restApi: PokemonAPI ) : ViewModel() { @AdamMc331 #AndroidMakers 44
Allows You To A/B Test Your New API private fun getRepository(): PokemonRepository { if (isInGraphQLTestGroup) { return ApolloService() } else { return RetrofitService() } } @AdamMc331 #AndroidMakers 47
Why A/B Test A New API? • You can monitor performance of a page using different APIs • This is a big change, you should ship with confidence @AdamMc331 #AndroidMakers 48
Why A/B Test A New API? • You can monitor performance of a page using different APIs • This is a big change, you should ship with confidence • You won't need to do this for every page, but is helpful for your first page to use GraphQL @AdamMc331 #AndroidMakers 48