SlideShare a Scribd company logo
1 of 50
Download to read offline
GraphQL	in	Android
Thao	Huynh	Quang	
Trusting	Social
Restful	Service
Restful	Service
- Using	JSON	Format.
Restful	Service
- Using	JSON	Format.	
- Human	readable	and	editable.
Restful	Service
- Using	JSON	Format.	
- Human	readable	and	editable.	
- There	are	many	libraries	supporting	Restful	Service
Restful	Service
- Using	JSON	Format.	
- Human	readable	and	editable.	
- There	are	many	libraries	supporting	Restful	Service	
-		There	are	many	tools	supporting	Restful	Service.
Issue	1:	Multiple	round	trips
Requirements:	
- Get	all	the	feeds	
- Display	Jirst	comment	at	every	feed
Issue	1:	Multiple	round	trips
Requirements:	
- Get	all	the	feeds	
- Display	Jirst	comment	at	every	feed
Solution	1:	
- get_all_feeds(user_name,	offset,	limit)	
- get_comments_by_feed_id(feed_id,	total_comment)
Issue	1:	Multiple	round	trips
Requirements:	
- Get	all	the	feeds	
- Display	Jirst	comment	at	every	feed
Solution	1:	
- get_all_feeds(user_name,	offset,	limit)	
- get_comments_by_feed_id(feed_id,	total_comment)	
10	items/page:	11	requests.	(N+1	query	problem)
Issue	1:	Multiple	round	trips
Solution	2:	
- get_all_feeds(user_name,	offset,	limit)	
Return	Jirst	comment	for	every	feed	item.
Issue	1:	Multiple	round	trips
Solution	2:	
- get_all_feeds(user_name,	offset,	limit)	
Return	Jirst	comment	for	every	feed	item.
New	requirements:	
- Get	all	the	feeds	
- Display	Jirst	comment	at	every	feed	on	web.	
- Display	no	comment	on	mobile	device.
Issue	1:	Multiple	round	trips
Solution	2:	
- get_all_feeds(user_name,	offset,	limit)	
Return	Jirst	comment	for	every	feed	item.
New	requirements:	
- Get	all	the	feeds	
- Display	Jirst	comment	at	every	feed	on	web.	
- Display	no	comment	on	mobile	device.
Solution:	
- get_feeds_for_mobile	and	get_feeds_for_web	
- get_feeds(user_name,	offset,	limit,	total_comment)
Issue	1:	Multiple	round	trips
Problem:		REST	resource	that	is	too	tightly-couple	to	client	UI
Issue	2:	Keeping	backward	compatible
New	requirements:	
- Get	all	the	feeds	
- Remove	total	views.		It	is	so	distract	for	users.
Old	requirements:	
- Get	all	the	feeds	
- Display	total	views	at	every	feed
Issue	2:	Keeping	backward	compatible
- Solution	1:	remove	total_views:	old	client	will	be	broken.	
- Solution	2:	keep	total_views:	bloating	api			
- Solution	3:	making	new	api.	Increasing	maintain	cost.		
…
Issues	3:	Over	fetching	of	data
- Mobile:		feed	doesn’t	need	to	show	the	created	date.	
- Web:	feed	need	to	show	the	created	date.
Problems
- Client	can	get	only	data	which	server	has	deJined.	
- Many	different	types	of	client	(Android,	iOS,	Web	…)	
- Client	changes	overtime.
Problem
Can	client	query	directly	to	server	?
Problem
Can	client	query	directly	to	server	?
GraphQL
Introduction
GraphQL	is	an	open-source	data	query	and	manipulation	language	for	APIs,	
and	a	runtime	for	fulJilling	queries	with	existing	data.	
GraphQL	was	developed	internally	by	Facebookin	2012	before	being	publicly	
released	in	2015.
Introduction
Introduction
Speci?ication…NOT	Implementation	
GraphQL	is	a	query	language,	it	is	a	way	to	get	data	from	an	API	to	your	application	hence.
Introduction
Speci?ication…NOT	Implementation	
GraphQL	is	a	query	language,	it	is	a	way	to	get	data	from	an	API	to	your	application	hence.	
Cooperation	between	server	side	and	client	side	
Server	must	deJines	all	necessary	APIs	
Client	should	have	supported	GraphQL	for	querying.
Introduction
Speci?ication…NOT	Implementation	
GraphQL	is	a	query	language,	it	is	a	way	to	get	data	from	an	API	to	your	application	hence.	
Cooperation	between	server	side	and	client	side	
Server	must	deJines	all	necessary	APIs	
Client	should	have	supported	GraphQL	for	querying.	
Multiple	platforms	
Android,	iOS,	Web
GraphQL	Language
Query	Data
Query	Data
Filter	&	Paging
Relationship
GraphQL	vs	Restful
Companies
Companies
Restful	over	GraphQL
- Tooling	
- Caching	
- Query	performance	
- Limited	languages	
- …
Step-by-Step	
Android
Library
Bad	news:	Facebook	does	not	open	source	Android	GraphQL	client
Library
Bad	news:	Facebook	does	not	open	source	Android	GraphQL	client	
Good	news:	Apollo	Android	Client	
Developed	by	Shopify,	New	York	Times	and	AirBnB
Library
Bad	news:	Facebook	does	not	open	source	Android	GraphQL	client	
Good	news:	Apollo	Android	Client	
Developed	by	Shopify,	New	York	Times	and	AirBnB
Step	1:	Setup	Apollo	dependencies
Just	follow	on	https://github.com/apollographql/apollo-android
Step	2:		DeJine	graphQL	query
Import	schema	
DeJine	GraphQL	query	
Compile
Step	2:		DeJine	graphQL	query
FeedQuery feedQuery = FeedQuery.builder()
.limit(10)
.type(FeedType.HOT)
.build()
Step	3:		Consume
private static final String BASE_URL = "<your_server>/graphql";
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
ApolloClient apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
Step	3:		Consume
apolloClient.query(feedQuery).enqueue(new
ApolloCall.Callback<FeedQuery.Data>() {
@Override public void onResponse(@NotNull Response<FeedQuery.Data>
dataResponse) {
});
}
@Override public void onFailure(@NotNull Throwable t) {
}
});
References
Repos:	https://github.com/hqthao/graphql_demo
References
https://github.com/apollographql/apollo-android	
https://graphql.org/learn/	
https://github.com/graphql/graphql-spec
https://www.howtographql.com/
Q&A

More Related Content

More from Thao Huynh Quang

2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdfThao Huynh Quang
 
Consensus and Raft Algorithm in Distributed System
Consensus and  Raft Algorithm in Distributed SystemConsensus and  Raft Algorithm in Distributed System
Consensus and Raft Algorithm in Distributed SystemThao Huynh Quang
 
Consensus and Raft algorithm (Vietnamese version)
Consensus and Raft algorithm (Vietnamese version)Consensus and Raft algorithm (Vietnamese version)
Consensus and Raft algorithm (Vietnamese version)Thao Huynh Quang
 
Kotlin Introduction with Android applications
Kotlin Introduction with Android applicationsKotlin Introduction with Android applications
Kotlin Introduction with Android applicationsThao Huynh Quang
 
Git Introduction with illustrations
Git Introduction with illustrationsGit Introduction with illustrations
Git Introduction with illustrationsThao Huynh Quang
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence libraryThao Huynh Quang
 
Kubernetes and service mesh application
Kubernetes  and service mesh applicationKubernetes  and service mesh application
Kubernetes and service mesh applicationThao Huynh Quang
 
Kafka: All an engineer needs to know
Kafka: All an engineer needs to knowKafka: All an engineer needs to know
Kafka: All an engineer needs to knowThao Huynh Quang
 
Concurrency pattern in Kotlin
Concurrency pattern in KotlinConcurrency pattern in Kotlin
Concurrency pattern in KotlinThao Huynh Quang
 
Observability and its application
Observability and its applicationObservability and its application
Observability and its applicationThao Huynh Quang
 
Android Reverse Engineering
Android Reverse EngineeringAndroid Reverse Engineering
Android Reverse EngineeringThao Huynh Quang
 

More from Thao Huynh Quang (16)

2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf
 
Consensus and Raft Algorithm in Distributed System
Consensus and  Raft Algorithm in Distributed SystemConsensus and  Raft Algorithm in Distributed System
Consensus and Raft Algorithm in Distributed System
 
Consensus and Raft algorithm (Vietnamese version)
Consensus and Raft algorithm (Vietnamese version)Consensus and Raft algorithm (Vietnamese version)
Consensus and Raft algorithm (Vietnamese version)
 
Kotlin Introduction with Android applications
Kotlin Introduction with Android applicationsKotlin Introduction with Android applications
Kotlin Introduction with Android applications
 
Git Introduction with illustrations
Git Introduction with illustrationsGit Introduction with illustrations
Git Introduction with illustrations
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence library
 
Android Performance Tips
Android Performance TipsAndroid Performance Tips
Android Performance Tips
 
Kubernetes and service mesh application
Kubernetes  and service mesh applicationKubernetes  and service mesh application
Kubernetes and service mesh application
 
Kafka: All an engineer needs to know
Kafka: All an engineer needs to knowKafka: All an engineer needs to know
Kafka: All an engineer needs to know
 
Blockchain introduction
Blockchain introductionBlockchain introduction
Blockchain introduction
 
Concurrency pattern in Kotlin
Concurrency pattern in KotlinConcurrency pattern in Kotlin
Concurrency pattern in Kotlin
 
Observability and its application
Observability and its applicationObservability and its application
Observability and its application
 
Android GRPC
Android GRPCAndroid GRPC
Android GRPC
 
Android Reverse Engineering
Android Reverse EngineeringAndroid Reverse Engineering
Android Reverse Engineering
 
nosql
nosqlnosql
nosql
 
android deep linking
android deep linkingandroid deep linking
android deep linking
 

GraphQL in Android