-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gRPC with connect-rpc (#3197)
Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
a789775
commit 51ebc1f
Showing
8 changed files
with
308 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use tailcall_valid::Valid; | ||
|
||
use crate::core::config::{Config, Grpc, Http, Resolver, ResolverSet}; | ||
use crate::core::Transform; | ||
|
||
pub struct ConnectRPC; | ||
|
||
impl Transform for ConnectRPC { | ||
type Value = Config; | ||
type Error = String; | ||
|
||
fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> { | ||
for type_ in config.types.values_mut() { | ||
for field_ in type_.fields.values_mut() { | ||
let new_resolvers = field_ | ||
.resolvers | ||
.0 | ||
.iter() | ||
.map(|resolver| match resolver { | ||
Resolver::Grpc(grpc) => Resolver::Http(Http::from(grpc.clone())), | ||
other => other.clone(), | ||
}) | ||
.collect(); | ||
|
||
field_.resolvers = ResolverSet(new_resolvers); | ||
} | ||
} | ||
|
||
Valid::succeed(config) | ||
} | ||
} | ||
|
||
impl From<Grpc> for Http { | ||
fn from(grpc: Grpc) -> Self { | ||
let url = grpc.url; | ||
let body = grpc.body.or_else(|| { | ||
// if body isn't present while transforming the resolver, we need to provide an | ||
// empty object. | ||
Some(serde_json::Value::Object(serde_json::Map::new())) | ||
}); | ||
|
||
// remove the last | ||
// method: package.service.method | ||
// remove the method from the end. | ||
let parts = grpc.method.split(".").collect::<Vec<_>>(); | ||
let method = parts[..parts.len() - 1].join(".").to_string(); | ||
let endpoint = parts[parts.len() - 1].to_string(); | ||
|
||
let new_url = format!("{}/{}/{}", url, method, endpoint); | ||
let headers = grpc.headers; | ||
let batch_key = grpc.batch_key; | ||
let dedupe = grpc.dedupe; | ||
let select = grpc.select; | ||
let on_response_body = grpc.on_response_body; | ||
|
||
Self { | ||
url: new_url, | ||
body: body.map(|b| b.to_string()), | ||
method: crate::core::http::Method::POST, | ||
headers, | ||
batch_key, | ||
dedupe, | ||
select, | ||
on_response_body, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::{json, Value}; | ||
|
||
use super::*; | ||
use crate::core::config::KeyValue; | ||
|
||
#[test] | ||
fn test_grpc_to_http_basic_conversion() { | ||
let grpc = Grpc { | ||
url: "http://localhost:8080".to_string(), | ||
method: "package.service.method".to_string(), | ||
body: Some(json!({"key": "value"})), | ||
headers: Default::default(), | ||
batch_key: Default::default(), | ||
dedupe: Default::default(), | ||
select: Default::default(), | ||
on_response_body: Default::default(), | ||
}; | ||
|
||
let http = Http::from(grpc); | ||
|
||
assert_eq!(http.url, "http://localhost:8080/package.service/method"); | ||
assert_eq!(http.method, crate::core::http::Method::POST); | ||
assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_grpc_to_http_empty_body() { | ||
let grpc = Grpc { | ||
url: "http://localhost:8080".to_string(), | ||
method: "package.service.method".to_string(), | ||
body: Default::default(), | ||
headers: Default::default(), | ||
batch_key: Default::default(), | ||
dedupe: Default::default(), | ||
select: Default::default(), | ||
on_response_body: Default::default(), | ||
}; | ||
|
||
let http = Http::from(grpc); | ||
|
||
assert_eq!(http.body, Some("{}".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_grpc_to_http_with_headers() { | ||
let grpc = Grpc { | ||
url: "http://localhost:8080".to_string(), | ||
method: "a.b.c".to_string(), | ||
body: None, | ||
headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }], | ||
batch_key: Default::default(), | ||
dedupe: Default::default(), | ||
select: Default::default(), | ||
on_response_body: Default::default(), | ||
}; | ||
|
||
let http = Http::from(grpc); | ||
|
||
assert_eq!(http.url, "http://localhost:8080/a.b/c"); | ||
assert_eq!( | ||
http.headers | ||
.iter() | ||
.find(|h| h.key == "X-Foo") | ||
.unwrap() | ||
.value, | ||
"bar".to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_grpc_to_http_all_fields() { | ||
let grpc = Grpc { | ||
url: "http://localhost:8080".to_string(), | ||
method: "package.service.method".to_string(), | ||
body: Some(json!({"key": "value"})), | ||
headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }], | ||
batch_key: vec!["batch_key_value".to_string()], | ||
dedupe: Some(true), | ||
select: Some(Value::String("select_value".to_string())), | ||
on_response_body: Some("on_response_body_value".to_string()), | ||
}; | ||
|
||
let http = Http::from(grpc); | ||
|
||
assert_eq!(http.url, "http://localhost:8080/package.service/method"); | ||
assert_eq!(http.method, crate::core::http::Method::POST); | ||
assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string())); | ||
assert_eq!( | ||
http.headers | ||
.iter() | ||
.find(|h| h.key == "X-Foo") | ||
.unwrap() | ||
.value, | ||
"bar".to_string() | ||
); | ||
assert_eq!(http.batch_key, vec!["batch_key_value".to_string()]); | ||
assert_eq!(http.dedupe, Some(true)); | ||
assert_eq!(http.select, Some(Value::String("select_value".to_string()))); | ||
assert_eq!( | ||
http.on_response_body, | ||
Some("on_response_body_value".to_string()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod comments_builder; | ||
pub mod connect_rpc; | ||
pub mod path_builder; | ||
pub mod path_field; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
```json @config | ||
{ | ||
"inputs": [ | ||
{ | ||
"curl": { | ||
"src": "http://jsonplaceholder.typicode.com/users", | ||
"fieldName": "users" | ||
} | ||
}, | ||
{ | ||
"proto": { | ||
"src": "tailcall-fixtures/fixtures/protobuf/news.proto", | ||
"url": "http://localhost:50051", | ||
"connectRPC": true | ||
} | ||
} | ||
], | ||
"preset": { | ||
"mergeType": 1.0, | ||
"inferTypeNames": true, | ||
"treeShake": true | ||
}, | ||
"output": { | ||
"path": "./output.graphql", | ||
"format": "graphQL" | ||
}, | ||
"schema": { | ||
"query": "Query" | ||
} | ||
} | ||
``` |
81 changes: 81 additions & 0 deletions
81
...li_spec__test__generator_spec__tests__cli__fixtures__generator__proto-connect-rpc.md.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
source: tests/cli/gen.rs | ||
expression: config.to_sdl() | ||
--- | ||
schema @server @upstream { | ||
query: Query | ||
} | ||
|
||
input GEN__news__MultipleNewsId @addField(name: "ids", path: ["ids", "id"]) { | ||
ids: [Id]@omit | ||
} | ||
|
||
input GEN__news__NewsInput { | ||
body: String | ||
id: Int | ||
postImage: String | ||
status: Status | ||
title: String | ||
} | ||
|
||
input Id { | ||
id: Int | ||
} | ||
|
||
enum Status { | ||
DELETED | ||
DRAFT | ||
PUBLISHED | ||
} | ||
|
||
type Address { | ||
city: String | ||
geo: Geo | ||
street: String | ||
suite: String | ||
zipcode: String | ||
} | ||
|
||
type Company { | ||
bs: String | ||
catchPhrase: String | ||
name: String | ||
} | ||
|
||
type GEN__news__NewsList { | ||
news: [News] | ||
} | ||
|
||
type Geo { | ||
lat: String | ||
lng: String | ||
} | ||
|
||
type News { | ||
body: String | ||
id: Int | ||
postImage: String | ||
status: Status | ||
title: String | ||
} | ||
|
||
type Query { | ||
GEN__news__NewsService__AddNews(news: GEN__news__NewsInput!): News @http(url: "http://localhost:50051/news.NewsService/AddNews", body: "\"{{.args.news}}\"", method: "POST") | ||
GEN__news__NewsService__DeleteNews(newsId: Id!): Empty @http(url: "http://localhost:50051/news.NewsService/DeleteNews", body: "\"{{.args.newsId}}\"", method: "POST") | ||
GEN__news__NewsService__EditNews(news: GEN__news__NewsInput!): News @http(url: "http://localhost:50051/news.NewsService/EditNews", body: "\"{{.args.news}}\"", method: "POST") | ||
GEN__news__NewsService__GetAllNews: GEN__news__NewsList @http(url: "http://localhost:50051/news.NewsService/GetAllNews", body: "{}", method: "POST") | ||
GEN__news__NewsService__GetMultipleNews(multipleNewsId: GEN__news__MultipleNewsId!): GEN__news__NewsList @http(url: "http://localhost:50051/news.NewsService/GetMultipleNews", body: "\"{{.args.multipleNewsId}}\"", method: "POST") | ||
GEN__news__NewsService__GetNews(newsId: Id!): News @http(url: "http://localhost:50051/news.NewsService/GetNews", body: "\"{{.args.newsId}}\"", method: "POST") | ||
users: [User] @http(url: "http://jsonplaceholder.typicode.com/users") | ||
} | ||
|
||
type User { | ||
address: Address | ||
company: Company | ||
email: String | ||
id: Int | ||
name: String | ||
phone: String | ||
username: String | ||
website: String | ||
} |
51ebc1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running 30s test @ http://localhost:8000/graphql
4 threads and 100 connections
765974 requests in 30.01s, 3.84GB read
Requests/sec: 25524.16
Transfer/sec: 131.01MB