SlideShare a Scribd company logo
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

What's hot

An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
Aniruddh Bhilvare
 
An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager WSO2
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
Reinvently
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
Adeel Riaz
 
Full stack development
Full stack developmentFull stack development
Full stack development
Pavlo Iuriichuk
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
nationalmobileapps
 
Report in Java programming and SQL
Report in Java programming and SQLReport in Java programming and SQL
Report in Java programming and SQL
vikram mahendra
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
Ashok Pundit
 
Secure your app with keycloak
Secure your app with keycloakSecure your app with keycloak
Secure your app with keycloak
Guy Marom
 
REST API
REST APIREST API
REST API
Tofazzal Ahmed
 
Building beautiful apps with Google flutter
Building beautiful apps with Google flutterBuilding beautiful apps with Google flutter
Building beautiful apps with Google flutter
Ahmed Abu Eldahab
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Sambhu Lakshmanan
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
Google flutter the easy and practical way IEEE Alazhar
Google flutter the easy and practical way IEEE AlazharGoogle flutter the easy and practical way IEEE Alazhar
Google flutter the easy and practical way IEEE Alazhar
Ahmed Abu Eldahab
 
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler WebinarKeycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
marcuschristie
 
React Native
React NativeReact Native
Git the fast version control system
Git the fast version control systemGit the fast version control system
Git the fast version control system
Jeroen Rosenberg
 
Dealing with Merge Conflicts in Git
Dealing with Merge Conflicts in GitDealing with Merge Conflicts in Git
Dealing with Merge Conflicts in Git
gittower
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
guy_davis
 
Best Practice-React
Best Practice-ReactBest Practice-React
Best Practice-ReactYang Yang
 

What's hot (20)

An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Full stack development
Full stack developmentFull stack development
Full stack development
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
Report in Java programming and SQL
Report in Java programming and SQLReport in Java programming and SQL
Report in Java programming and SQL
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
Secure your app with keycloak
Secure your app with keycloakSecure your app with keycloak
Secure your app with keycloak
 
REST API
REST APIREST API
REST API
 
Building beautiful apps with Google flutter
Building beautiful apps with Google flutterBuilding beautiful apps with Google flutter
Building beautiful apps with Google flutter
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Google flutter the easy and practical way IEEE Alazhar
Google flutter the easy and practical way IEEE AlazharGoogle flutter the easy and practical way IEEE Alazhar
Google flutter the easy and practical way IEEE Alazhar
 
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler WebinarKeycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
 
React Native
React NativeReact Native
React Native
 
Git the fast version control system
Git the fast version control systemGit the fast version control system
Git the fast version control system
 
Dealing with Merge Conflicts in Git
Dealing with Merge Conflicts in GitDealing with Merge Conflicts in Git
Dealing with Merge Conflicts in Git
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Best Practice-React
Best Practice-ReactBest Practice-React
Best Practice-React
 

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.pdf
Thao 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 System
Thao 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 applications
Thao Huynh Quang
 
Git Introduction with illustrations
Git Introduction with illustrationsGit Introduction with illustrations
Git Introduction with illustrations
Thao Huynh Quang
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence library
Thao Huynh Quang
 
Android Performance Tips
Android Performance TipsAndroid Performance Tips
Android Performance Tips
Thao Huynh Quang
 
Kubernetes and service mesh application
Kubernetes  and service mesh applicationKubernetes  and service mesh application
Kubernetes and service mesh application
Thao 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 know
Thao Huynh Quang
 
Blockchain introduction
Blockchain introductionBlockchain introduction
Blockchain introduction
Thao Huynh Quang
 
Concurrency pattern in Kotlin
Concurrency pattern in KotlinConcurrency pattern in Kotlin
Concurrency pattern in Kotlin
Thao Huynh Quang
 
Observability and its application
Observability and its applicationObservability and its application
Observability and its application
Thao Huynh Quang
 
Android GRPC
Android GRPCAndroid GRPC
Android GRPC
Thao Huynh Quang
 
Android Reverse Engineering
Android Reverse EngineeringAndroid Reverse Engineering
Android Reverse Engineering
Thao 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