SlideShare a Scribd company logo
1 of 70
Download to read offline
Implementing OpenAPI and
GraphQL Services with gRPC
Tim Burks (timburks@google.com, @timburks)
1. API technologies: OpenAPI, GraphQL, gRPC
2. Why implement your API with gRPC?
3. Implementing OpenAPI services with gRPC
4. Implementing GraphQL services with gRPC
Topics
timburks@google.com
@timburks
Electronic Design Automation
Mobile Apps
APIs
Hello!
Projects at Google
gnostic
(OpenAPI Compiler)
gRPC-Swift
GAPICs for
Google APIs
1
API Technologies:
OpenAPI, GraphQL,
gRPC
APIs are Interfaces
for Application
Programmers.
Programmers write code.
Five Questions about API Systems
1. What problem is this API system addressing?
2. How are APIs called?
3. How are APIs built?
4. What’s great about this?
5. What’s challenging about this?
What problem is OpenAPI addressing?
“The OpenAPI Specification, originally known
as the Swagger Specification, is a
specification for machine-readable interface
files for describing, producing, consuming, and
visualizing RESTful web services.”
Wikipedia
How an OpenAPI service is called:
You might call it from your browser.
In your applications, you call it from code.
openapi-generator
autorest
APIMatic
SwagGen (Swift)
Paperclip (Rust)
How an OpenAPI service is built:
🤞 Code-first
Handwritten implementations.
Handwritten API descriptions.
⚙ Code-first (better)
Handwritten implementations.
API descriptions generated
from code.
📐 Spec-first
Handwritten API descriptions.
Implementations generated
from API descriptions.
?🤖
What’s great about OpenAPI:
● Standards and tools make it easy to get
started.
○ HTTP
○ JSON/YAML
● Very popular - large community.
What’s challenging about OpenAPI:
REST?
“Unfortunately, many of the APIs that claim to be RESTful
layer a lot of proprietary concepts on top of HTTP.
Essentially, they invent their own API and use HTTP as a
lower-level transport layer, rather than using HTTP directly as
it was designed. In fact, there’s so much variability in the way
that people use the term REST in the context of APIs that it’s
difficult to know what they mean by it unless you know them
well.”
Martin Nally
What problem is GraphQL addressing?
“GraphQL is a query language for APIs and a
runtime for fulfilling those queries with your
existing data. GraphQL provides a complete
and understandable description of the data in
your API, gives clients the power to ask for
exactly what they need and nothing more,
makes it easier to evolve APIs over time, and
enables powerful developer tools.”
graphql.org
How a GraphQL service is called:
“With express-graphql, you can just send an
HTTP POST request to the endpoint you
mounted your GraphQL server on, passing the
GraphQL query as the query field in a JSON
payload.”
https://graphql.org/graphql-js/graphql-clients/
Also GraphiQL!
How a GraphQL service is built:
● Code-first, schema extracted from code
● Schema-first - code generated from SDL
● Generated from underlying APIs
○ Federated from other GraphQL APIs
○ OpenAPI-to-GraphQL
○ rejoiner
What’s great about GraphQL:
● Simple client-side interface
● Optimizes for client performance
● Layered on familiar technology
● Sharable BFF
What’s challenging about GraphQL:
● Complex implementation
● Performance challenges
● Versioning?
● Schema design
Public GraphQL APIs are rare.
What problem is gRPC addressing?
“gRPC is a modern open source high
performance RPC framework that can run in
any environment. It can efficiently connect
services in and across data centers with
pluggable support for load balancing, tracing,
health checking and authentication. It is also
applicable in last mile of distributed computing
to connect devices, mobile applications and
browsers to backend services.”
grpc.io
How a gRPC service is called:
ALWAYS specification-first.
Clients are generated from API
descriptions written in the Protocol Buffer
language.
How a gRPC service is built:
ALWAYS specification-first.
Service code is generated. Typically this
generated code declares an interface that is
implemented in language-native data
structures and patterns.
What’s great about gRPC:
So much is built-in.
High performance.
Cross-language compatibility.
Potential for very nice client integration.
What’s challenging about gRPC:
Some learning and tooling is required!
2
Why implement
your API with
gRPC?
APIs have a native language.
Protocol Buffers
Protocol Buffers is a language-neutral, platform-neutral, extensible
mechanism for serializing structured data.
“Protocol Buffers” means several things
1. A serialization mechanism
2. An interface description language
3. A methodology
Protocol Buffer Serialization
A message is just a stream of bytes
[field_number<<3 + wire_type] [length if necessary][data]...
$ hexdump /tmp/request.bin
0000000 0a 05 68 65 6c 6c 6f
0a is “0000 1010”, so
field_number = 1 and wire_type = 2
Protocol Buffer IDL
message EchoMessage {
string text = 1;
// other fields...
}
Protocol Buffer Methodology
% protoc echo.proto --go_out=.
# This runs the Protocol Buffer Compiler
# (https://github.com/protocolbuffers/protobuf/releases)
#
# with a plugin called protoc-gen-go
#
# The plugin generates a Go source file that implements
# the data structures defined in echo.proto and code
# for reading and writing them as serialized bytes.
Try it!
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
echo "./generated"
)
func main() {
document := echo.EchoMessage{Text:"hello"}
bytes, _ := proto.Marshal(&document)
fmt.Printf("%+vn", bytes)
}
gRPC scales to multiple languages.
Java
Service
Python
Service
Go
Service
C++
Service
gRPC
Server
gRPC
Stub
gRPC
Stub
gRPC
Stub
gRPC
Stub
gRPC
Server
gRPC
Server
gRPC
Server
gRPC
Stub
gRPC scales to distributed systems.
Load
Balancer
Users
SQL Database
Cluster
NoSQL Database
Cluster
ETL Pipeline
Front End Business
Logic
Back End Business
Logic
Data Warehouse
Invoicing
Transcoding
Illustration: Sandeep Dinesh, Google, Developer Advocate
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Illustration: Sandeep Dinesh, Google, Developer Advocate
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
Load Balancer
Users
SQL Database Cluster
NoSQL Database Cluster
ETL Pipeline
Front End Business Logic
Back End Business Logic
Data Warehouse
Back End Business Logic
Back End Business Logic
API
API
gRPC is open and extensible.
gRPC has a style guide.
Generated API Clients
What do you get?
Trivial Getting-Started Experience
GAPIC client:
Channel channel =
NettyChannelBuilder.forAddress(API_ENTRY, API_PORT).build();
List<ClientInterceptor> interceptors = new ArrayList<>();
GoogleCredentials credentials =
GoogleCredentials.getApplicationDefault();
interceptors.add(
ChannelFactory.credentialInterceptor(credentials));
LoggingService stub =
LoggingServiceGrpc.newBlockingStub(channel, interceptors);
gRPC:
LoggingClient client = LoggingClient.create();
Flattening, Automatic Retry
GAPIC client:
LoggingService stub = ...;
// Note: this only does a simple retry, exponential backoff
// is more complex
DeleteLogRequest request = DeleteLogRequest.newBuilder()
.setLogName(LOG_NAME)
.build();
for (int i = 0; i < MAX_RETRY; i++) {
try {
stub.deleteLog(request);
} catch (RpcException e) {
if (i == MAX_RETRY) throw e;
}
}
gRPC:
LoggingClient client = LoggingClient.create();
client.deleteLog(LOG_NAME);
Natural Pagination
GAPIC client:
LoggingService stub = ...;
ListLogsRequest request = ListLogsRequest.newBuilder()
.setParentName("projects/" + PROJECT_ID)
.build();
while (true) {
ListLogsResponse response = stub.listLogs(request);
for (Log log : response.getLogsList()) {
System.out.printf("Log: %s%n", log.getDisplayName());
}
String nextPageToken = response.getNextPageToken();
if (nextPageToken != null) {
request = ListLogsRequest.newBuilder()
.setPageToken(nextPageToken).build();
} else {
break;
}
}
gRPC:
LoggingClient client = LoggingClient.create();
ParentNameOneOf parent =
ParentNameOneOf.from(ProjectName.create(PROJECT_ID));
for (Log log : client.listLogs(PARENT).iterateAll()) {
System.out.printf("Log: %s%n", log.getDisplayName());
}
Long-Running Operations
SpeechClient speechClient = SpeechClient.create();
OperationFuture<LongRunningRecognizeResponse> recognizeOperation =
speechClient.longRunningRecognizeAsync(config, audio);
…
LongRunningRecognizeResponse response = recognizeOperation.get();
● Java: OperationFuture
○ Polls the service until the LRO is done
○ Provides metadata as the LRO is in progress
○ Provides the final result
Resource Name Types
GAPIC client:
PublisherService stub = ...;
CreateTopicRequest request = CreateTopicRequest.newBuilder()
.setTopic("projects/my-project/topics/my-topic")
.build();
Topic response = stub.createTopic(request);
gRPC:
TopicAdminClient topicAdminClient = TopicAdminClient.create();
TopicName name = TopicName.create("my-project", "my-topic");
Topic response = topicAdminClient.createTopic(name);
Generated Sample Code
GAPIC Project Architecture (at github.com/googleapis)
gapic-generator-python
Wanted: Swift, Rust, Dart...
gapic-generator
(java, php, python, go,
csharp, node, ruby)
gapic-generator-ruby
gapic-generator-go
gapic-generator-typescript
gapic-generator-csharp
One API
description
(Annotated
.proto files)
Many
language-
specific
client
libraries
“Showcase” API verification
protoc-gen-go_cli
A protoc plugin that generates
CLIs in Go for APIs described
with protocol buffers.
gRPC Fallback
● Go proxy
● Description and handwritten examples
● Node client (gax-nodejs)
gRPC Reflection
● Protocol Discussion
● Go implementation (example)
● Specification (protos)
Manage gRPC Services with Cloud Endpoints
Cloud Endpoints Architecture
GCE, GKE, Kubernetes or App Engine
Flexible
Environment Instance
GCE, GKE, Kubernetes or App Engine Flexible
Environment Instance (or off-GCP)
Extensible Service
Proxy Container
API Container
Google Service
Management API
User
Code
api
calls
gcloud
Config
deployment
Google Cloud
Console
Google Service
Control API
config Runtime
check & report
Load
balanci
ng
Stackdriver
Metrics &
logs
Metrics &
logs
visualized
3
Implementing
OpenAPI Services
with gRPC
Build a gRPC service, get a free REST API!
Originally defined in google/api/http.proto
Now documented in AIP-127: HTTP and gRPC Transcoding
service Display {
rpc CreateKiosk(Kiosk) returns (Kiosk) {
option (google.api.http) = {post:"/v1/kiosks"};
}
rpc GetKiosk(GetKioskRequest) returns (Kiosk) {
option (google.api.http) =
{get: "/v1/kiosks/{id}" };
}
rpc DeleteKiosk(DeleteKioskRequest)
returns (google.protobuf.Empty) {
option (google.api.http) =
{ delete: "/v1/signs/{id}" };
}
[...]
}
Add REST annotations to your service
gRPC definitions with HTTP annotations
service Display {
rpc CreateKiosk(Kiosk) returns (Kiosk) {
}
rpc GetKiosk(GetKioskRequest) returns(Kiosk) {
}
rpc DeleteKiosk(DeleteKioskRequest)
returns (google.protobuf.Empty) {
}
[...]
}
gRPC definitions
# Call the CreateKiosk method
$ curl -d '{"name":"HTTP Kiosk", "size": { width: 1080, height: 720 } }' 
localhost:8082/v1/kiosks?key=AIzaSy[...]bBo
# Call the GetKiosk method
$ curl localhost:8082/v1/kiosks/1?key=AIzaSy[...]bBo
Now you can use HTTP+JSON!
Schema-first OpenAPI
gnostic-grpc
gnostic-grpc
● 6 useful things I learned from GSoC
● How to build a REST API with gRPC and get
the best of two worlds
● gnostic-grpc (end-to-end example)
● Envoy gRPC-JSON transcoding
4
Implementing
GraphQL Services
with gRPC
Code-first or Schema-first?
● Evolving the Graph (Jon Wong, Coursera)
● Hard-Learned GraphQL Lessons: Based on
a True Story (Natalie Qabazard & Aditi
Garg, Trulia)
● The Problems of "Schema-First" GraphQL
Server Development (Nikolas Burk, Prisma)
Code-first GraphQL with Rejoiner
Medallion (Rejoiner demo by Danny Baggett)
Sample query (Rejoiner + Medallion)
{
events {
id
name
olympics {
year
season
}
olympians {
id
gender
last
first
}
}
}
Sample query (Rejoiner + Medallion)
{
getEvents(input: {year: 2018, season: "WINTER"}) {
id
name
}
}
Let’s hand-wrap a gRPC service (graphql-showcase)
Impressions / Project Ideas
gRPC has a lot of API information in the .proto files and we
can extend that with annotations.
There are some gaps between GraphQL assumptions and
gRPC practices that we would want to smooth with
configuration or conventions.
We could generate a GraphQL interface directly from gRPC
reflection.
Can we define a standard for gRPC-GraphQL transcoding?
resolver
in-mem
response
schema
GraphQL server
on Apigee
Hosted Target
Google Apigee and GraphQL
app
Edge
● AuthZ
● security
● aggregate analytics
Integrated Dev Portal
● GraphQL playgroundapp developer
AX plug-in
batch AX
app
GQL query
response
GQL query
response
Stackdriver
● query-level analytics
on-board,
use APIs
Implementing OpenAPI and GraphQL services with gRPC

More Related Content

What's hot

Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 

What's hot (20)

Features of java 02
Features of java 02Features of java 02
Features of java 02
 
Git and GitHub
Git and GitHubGit and GitHub
Git and GitHub
 
젠킨스 설치 및 설정
젠킨스 설치 및 설정젠킨스 설치 및 설정
젠킨스 설치 및 설정
 
Rust
RustRust
Rust
 
WorkShop: Introducción a GIT
WorkShop: Introducción a GITWorkShop: Introducción a GIT
WorkShop: Introducción a GIT
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
git and github
git and githubgit and github
git and github
 
GoLang Introduction
GoLang IntroductionGoLang Introduction
GoLang Introduction
 
Clean code
Clean codeClean code
Clean code
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
Clean code
Clean codeClean code
Clean code
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
 
Git and Github
Git and GithubGit and Github
Git and Github
 
Gradle
GradleGradle
Gradle
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Github
GithubGithub
Github
 
Introduction git
Introduction gitIntroduction git
Introduction git
 
Golang
GolangGolang
Golang
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 

Similar to Implementing OpenAPI and GraphQL services with gRPC

Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code Generation
Tim Burks
 

Similar to Implementing OpenAPI and GraphQL services with gRPC (20)

apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
 
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
 
GraphQL across the stack: How everything fits together
GraphQL across the stack: How everything fits togetherGraphQL across the stack: How everything fits together
GraphQL across the stack: How everything fits together
 
Build your next REST API with gRPC
Build your next REST API with gRPCBuild your next REST API with gRPC
Build your next REST API with gRPC
 
Usable APIs at Scale
Usable APIs at ScaleUsable APIs at Scale
Usable APIs at Scale
 
Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHP
 
How easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performanceHow easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performance
 
API Management for GraphQL
API Management for GraphQLAPI Management for GraphQL
API Management for GraphQL
 
Graphql presentation
Graphql presentationGraphql presentation
Graphql presentation
 
apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...
apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...
apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...
 
Your API on Steroids
Your API on Steroids Your API on Steroids
Your API on Steroids
 
APIdays Helsinki 2019 - GraphQL API Management with Amit P. Acharya, IBM
APIdays Helsinki 2019 - GraphQL API Management with Amit P. Acharya, IBMAPIdays Helsinki 2019 - GraphQL API Management with Amit P. Acharya, IBM
APIdays Helsinki 2019 - GraphQL API Management with Amit P. Acharya, IBM
 
Kochi Mulesoft Meetup #6
Kochi Mulesoft Meetup #6Kochi Mulesoft Meetup #6
Kochi Mulesoft Meetup #6
 
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
 
LF_APIStrat17_OpenAPI and gRPC Side-by-Side
LF_APIStrat17_OpenAPI and gRPC Side-by-SideLF_APIStrat17_OpenAPI and gRPC Side-by-Side
LF_APIStrat17_OpenAPI and gRPC Side-by-Side
 
Connecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL EndpointsConnecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL Endpoints
 
GraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsGraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer tools
 
Exposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIsExposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIs
 
Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code Generation
 

More from Tim Burks

More from Tim Burks (11)

Governing APIs at Scale
Governing APIs at ScaleGoverning APIs at Scale
Governing APIs at Scale
 
Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swift
 
Taming Cloud APIs with Swift
Taming Cloud APIs with SwiftTaming Cloud APIs with Swift
Taming Cloud APIs with Swift
 
Fast and Reliable Swift APIs with gRPC
Fast and Reliable Swift APIs with gRPCFast and Reliable Swift APIs with gRPC
Fast and Reliable Swift APIs with gRPC
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPC
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at Google
 
CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
 
Interpreting Objective C
Interpreting Objective CInterpreting Objective C
Interpreting Objective C
 
Deep Geek Diving into the iPhone OS and Frameworks
Deep Geek Diving into the iPhone OS and FrameworksDeep Geek Diving into the iPhone OS and Frameworks
Deep Geek Diving into the iPhone OS and Frameworks
 
Building Open Radar
Building Open RadarBuilding Open Radar
Building Open Radar
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

Implementing OpenAPI and GraphQL services with gRPC

  • 1. Implementing OpenAPI and GraphQL Services with gRPC Tim Burks (timburks@google.com, @timburks)
  • 2. 1. API technologies: OpenAPI, GraphQL, gRPC 2. Why implement your API with gRPC? 3. Implementing OpenAPI services with gRPC 4. Implementing GraphQL services with gRPC Topics
  • 4. Projects at Google gnostic (OpenAPI Compiler) gRPC-Swift GAPICs for Google APIs
  • 6. APIs are Interfaces for Application Programmers.
  • 8. Five Questions about API Systems 1. What problem is this API system addressing? 2. How are APIs called? 3. How are APIs built? 4. What’s great about this? 5. What’s challenging about this?
  • 9. What problem is OpenAPI addressing? “The OpenAPI Specification, originally known as the Swagger Specification, is a specification for machine-readable interface files for describing, producing, consuming, and visualizing RESTful web services.” Wikipedia
  • 10. How an OpenAPI service is called: You might call it from your browser. In your applications, you call it from code. openapi-generator autorest APIMatic SwagGen (Swift) Paperclip (Rust)
  • 11. How an OpenAPI service is built: 🤞 Code-first Handwritten implementations. Handwritten API descriptions. ⚙ Code-first (better) Handwritten implementations. API descriptions generated from code. 📐 Spec-first Handwritten API descriptions. Implementations generated from API descriptions. ?🤖
  • 12. What’s great about OpenAPI: ● Standards and tools make it easy to get started. ○ HTTP ○ JSON/YAML ● Very popular - large community.
  • 13. What’s challenging about OpenAPI: REST? “Unfortunately, many of the APIs that claim to be RESTful layer a lot of proprietary concepts on top of HTTP. Essentially, they invent their own API and use HTTP as a lower-level transport layer, rather than using HTTP directly as it was designed. In fact, there’s so much variability in the way that people use the term REST in the context of APIs that it’s difficult to know what they mean by it unless you know them well.” Martin Nally
  • 14. What problem is GraphQL addressing? “GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.” graphql.org
  • 15. How a GraphQL service is called: “With express-graphql, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the query field in a JSON payload.” https://graphql.org/graphql-js/graphql-clients/ Also GraphiQL!
  • 16. How a GraphQL service is built: ● Code-first, schema extracted from code ● Schema-first - code generated from SDL ● Generated from underlying APIs ○ Federated from other GraphQL APIs ○ OpenAPI-to-GraphQL ○ rejoiner
  • 17. What’s great about GraphQL: ● Simple client-side interface ● Optimizes for client performance ● Layered on familiar technology ● Sharable BFF
  • 18. What’s challenging about GraphQL: ● Complex implementation ● Performance challenges ● Versioning? ● Schema design Public GraphQL APIs are rare.
  • 19. What problem is gRPC addressing? “gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.” grpc.io
  • 20. How a gRPC service is called: ALWAYS specification-first. Clients are generated from API descriptions written in the Protocol Buffer language.
  • 21. How a gRPC service is built: ALWAYS specification-first. Service code is generated. Typically this generated code declares an interface that is implemented in language-native data structures and patterns.
  • 22. What’s great about gRPC: So much is built-in. High performance. Cross-language compatibility. Potential for very nice client integration.
  • 23. What’s challenging about gRPC: Some learning and tooling is required!
  • 25. APIs have a native language. Protocol Buffers
  • 26. Protocol Buffers is a language-neutral, platform-neutral, extensible mechanism for serializing structured data.
  • 27. “Protocol Buffers” means several things 1. A serialization mechanism 2. An interface description language 3. A methodology
  • 28. Protocol Buffer Serialization A message is just a stream of bytes [field_number<<3 + wire_type] [length if necessary][data]... $ hexdump /tmp/request.bin 0000000 0a 05 68 65 6c 6c 6f 0a is “0000 1010”, so field_number = 1 and wire_type = 2
  • 29. Protocol Buffer IDL message EchoMessage { string text = 1; // other fields... }
  • 30. Protocol Buffer Methodology % protoc echo.proto --go_out=. # This runs the Protocol Buffer Compiler # (https://github.com/protocolbuffers/protobuf/releases) # # with a plugin called protoc-gen-go # # The plugin generates a Go source file that implements # the data structures defined in echo.proto and code # for reading and writing them as serialized bytes.
  • 31. Try it! package main import ( "fmt" "github.com/golang/protobuf/proto" echo "./generated" ) func main() { document := echo.EchoMessage{Text:"hello"} bytes, _ := proto.Marshal(&document) fmt.Printf("%+vn", bytes) }
  • 32. gRPC scales to multiple languages. Java Service Python Service Go Service C++ Service gRPC Server gRPC Stub gRPC Stub gRPC Stub gRPC Stub gRPC Server gRPC Server gRPC Server gRPC Stub
  • 33.
  • 34. gRPC scales to distributed systems.
  • 35. Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Invoicing Transcoding Illustration: Sandeep Dinesh, Google, Developer Advocate
  • 36. Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Illustration: Sandeep Dinesh, Google, Developer Advocate
  • 37. Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic Load Balancer Users SQL Database Cluster NoSQL Database Cluster ETL Pipeline Front End Business Logic Back End Business Logic Data Warehouse Back End Business Logic Back End Business Logic API API
  • 38. gRPC is open and extensible.
  • 39. gRPC has a style guide.
  • 41. Trivial Getting-Started Experience GAPIC client: Channel channel = NettyChannelBuilder.forAddress(API_ENTRY, API_PORT).build(); List<ClientInterceptor> interceptors = new ArrayList<>(); GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); interceptors.add( ChannelFactory.credentialInterceptor(credentials)); LoggingService stub = LoggingServiceGrpc.newBlockingStub(channel, interceptors); gRPC: LoggingClient client = LoggingClient.create();
  • 42. Flattening, Automatic Retry GAPIC client: LoggingService stub = ...; // Note: this only does a simple retry, exponential backoff // is more complex DeleteLogRequest request = DeleteLogRequest.newBuilder() .setLogName(LOG_NAME) .build(); for (int i = 0; i < MAX_RETRY; i++) { try { stub.deleteLog(request); } catch (RpcException e) { if (i == MAX_RETRY) throw e; } } gRPC: LoggingClient client = LoggingClient.create(); client.deleteLog(LOG_NAME);
  • 43. Natural Pagination GAPIC client: LoggingService stub = ...; ListLogsRequest request = ListLogsRequest.newBuilder() .setParentName("projects/" + PROJECT_ID) .build(); while (true) { ListLogsResponse response = stub.listLogs(request); for (Log log : response.getLogsList()) { System.out.printf("Log: %s%n", log.getDisplayName()); } String nextPageToken = response.getNextPageToken(); if (nextPageToken != null) { request = ListLogsRequest.newBuilder() .setPageToken(nextPageToken).build(); } else { break; } } gRPC: LoggingClient client = LoggingClient.create(); ParentNameOneOf parent = ParentNameOneOf.from(ProjectName.create(PROJECT_ID)); for (Log log : client.listLogs(PARENT).iterateAll()) { System.out.printf("Log: %s%n", log.getDisplayName()); }
  • 44. Long-Running Operations SpeechClient speechClient = SpeechClient.create(); OperationFuture<LongRunningRecognizeResponse> recognizeOperation = speechClient.longRunningRecognizeAsync(config, audio); … LongRunningRecognizeResponse response = recognizeOperation.get(); ● Java: OperationFuture ○ Polls the service until the LRO is done ○ Provides metadata as the LRO is in progress ○ Provides the final result
  • 45. Resource Name Types GAPIC client: PublisherService stub = ...; CreateTopicRequest request = CreateTopicRequest.newBuilder() .setTopic("projects/my-project/topics/my-topic") .build(); Topic response = stub.createTopic(request); gRPC: TopicAdminClient topicAdminClient = TopicAdminClient.create(); TopicName name = TopicName.create("my-project", "my-topic"); Topic response = topicAdminClient.createTopic(name);
  • 47. GAPIC Project Architecture (at github.com/googleapis) gapic-generator-python Wanted: Swift, Rust, Dart... gapic-generator (java, php, python, go, csharp, node, ruby) gapic-generator-ruby gapic-generator-go gapic-generator-typescript gapic-generator-csharp One API description (Annotated .proto files) Many language- specific client libraries
  • 48.
  • 50. protoc-gen-go_cli A protoc plugin that generates CLIs in Go for APIs described with protocol buffers.
  • 51. gRPC Fallback ● Go proxy ● Description and handwritten examples ● Node client (gax-nodejs)
  • 52. gRPC Reflection ● Protocol Discussion ● Go implementation (example) ● Specification (protos)
  • 53. Manage gRPC Services with Cloud Endpoints
  • 54. Cloud Endpoints Architecture GCE, GKE, Kubernetes or App Engine Flexible Environment Instance GCE, GKE, Kubernetes or App Engine Flexible Environment Instance (or off-GCP) Extensible Service Proxy Container API Container Google Service Management API User Code api calls gcloud Config deployment Google Cloud Console Google Service Control API config Runtime check & report Load balanci ng Stackdriver Metrics & logs Metrics & logs visualized
  • 56. Build a gRPC service, get a free REST API! Originally defined in google/api/http.proto Now documented in AIP-127: HTTP and gRPC Transcoding
  • 57. service Display { rpc CreateKiosk(Kiosk) returns (Kiosk) { option (google.api.http) = {post:"/v1/kiosks"}; } rpc GetKiosk(GetKioskRequest) returns (Kiosk) { option (google.api.http) = {get: "/v1/kiosks/{id}" }; } rpc DeleteKiosk(DeleteKioskRequest) returns (google.protobuf.Empty) { option (google.api.http) = { delete: "/v1/signs/{id}" }; } [...] } Add REST annotations to your service gRPC definitions with HTTP annotations service Display { rpc CreateKiosk(Kiosk) returns (Kiosk) { } rpc GetKiosk(GetKioskRequest) returns(Kiosk) { } rpc DeleteKiosk(DeleteKioskRequest) returns (google.protobuf.Empty) { } [...] } gRPC definitions
  • 58. # Call the CreateKiosk method $ curl -d '{"name":"HTTP Kiosk", "size": { width: 1080, height: 720 } }' localhost:8082/v1/kiosks?key=AIzaSy[...]bBo # Call the GetKiosk method $ curl localhost:8082/v1/kiosks/1?key=AIzaSy[...]bBo Now you can use HTTP+JSON!
  • 60. gnostic-grpc ● 6 useful things I learned from GSoC ● How to build a REST API with gRPC and get the best of two worlds ● gnostic-grpc (end-to-end example) ● Envoy gRPC-JSON transcoding
  • 62. Code-first or Schema-first? ● Evolving the Graph (Jon Wong, Coursera) ● Hard-Learned GraphQL Lessons: Based on a True Story (Natalie Qabazard & Aditi Garg, Trulia) ● The Problems of "Schema-First" GraphQL Server Development (Nikolas Burk, Prisma)
  • 64. Medallion (Rejoiner demo by Danny Baggett)
  • 65. Sample query (Rejoiner + Medallion) { events { id name olympics { year season } olympians { id gender last first } } }
  • 66. Sample query (Rejoiner + Medallion) { getEvents(input: {year: 2018, season: "WINTER"}) { id name } }
  • 67. Let’s hand-wrap a gRPC service (graphql-showcase)
  • 68. Impressions / Project Ideas gRPC has a lot of API information in the .proto files and we can extend that with annotations. There are some gaps between GraphQL assumptions and gRPC practices that we would want to smooth with configuration or conventions. We could generate a GraphQL interface directly from gRPC reflection. Can we define a standard for gRPC-GraphQL transcoding?
  • 69. resolver in-mem response schema GraphQL server on Apigee Hosted Target Google Apigee and GraphQL app Edge ● AuthZ ● security ● aggregate analytics Integrated Dev Portal ● GraphQL playgroundapp developer AX plug-in batch AX app GQL query response GQL query response Stackdriver ● query-level analytics on-board, use APIs