-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): load config from tailcall subgraph
- Loading branch information
Showing
11 changed files
with
240 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -917,6 +917,7 @@ enum LinkType { | |
Htpasswd | ||
Jwks | ||
Grpc | ||
SubGraph | ||
} | ||
|
||
enum HttpVersion { | ||
|
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 |
---|---|---|
|
@@ -832,7 +832,8 @@ | |
"Operation", | ||
"Htpasswd", | ||
"Jwks", | ||
"Grpc" | ||
"Grpc", | ||
"SubGraph" | ||
] | ||
}, | ||
"Method": { | ||
|
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 @@ | ||
pub mod subgraph_reader; |
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,42 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use url::Url; | ||
|
||
use crate::core::{ | ||
config::{Config, ConfigModule}, | ||
resource_reader::{Cached, ResourceReader}, | ||
valid::Validator, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct SubGraphReader { | ||
reader: ResourceReader<Cached>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct AdminQuery { | ||
config: String, | ||
} | ||
|
||
impl SubGraphReader { | ||
pub fn new(reader: ResourceReader<Cached>) -> Self { | ||
Self { reader } | ||
} | ||
|
||
pub async fn fetch(&self, src: &str) -> anyhow::Result<ConfigModule> { | ||
let url = Url::parse(&format!("{src}/graphql"))?; | ||
|
||
let mut request = reqwest::Request::new(reqwest::Method::POST, url); | ||
|
||
let _ = request | ||
.body_mut() | ||
.insert(reqwest::Body::from(r#"{"query": "{ config }"}"#)); | ||
|
||
let file_read = self.reader.read_file(request).await?; | ||
let admin_query: AdminQuery = serde_json::from_str(&file_read.content)?; | ||
|
||
let config = Config::from_sdl(&admin_query.config).to_result()?; | ||
let config_module = ConfigModule::from(config); | ||
|
||
Ok(config_module) | ||
} | ||
} |
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,67 @@ | ||
--- | ||
source: tests/core/spec.rs | ||
expression: formatted | ||
--- | ||
scalar Bytes | ||
|
||
scalar Date | ||
|
||
scalar DateTime | ||
|
||
scalar Email | ||
|
||
scalar Empty | ||
|
||
scalar Int128 | ||
|
||
scalar Int16 | ||
|
||
scalar Int32 | ||
|
||
scalar Int64 | ||
|
||
scalar Int8 | ||
|
||
scalar JSON | ||
|
||
scalar PhoneNumber | ||
|
||
type Post { | ||
body: String! | ||
id: Int! | ||
title: String! | ||
user: User | ||
userId: Int! | ||
} | ||
|
||
type Query { | ||
posts: [Post] | ||
user(id: Int!): User | ||
users: [User] | ||
version: String | ||
} | ||
|
||
scalar UInt128 | ||
|
||
scalar UInt16 | ||
|
||
scalar UInt32 | ||
|
||
scalar UInt64 | ||
|
||
scalar UInt8 | ||
|
||
scalar Url | ||
|
||
type User { | ||
email: String! | ||
id: Int! | ||
name: String! | ||
phone: String | ||
username: String! | ||
website: String | ||
} | ||
|
||
schema { | ||
query: Query | ||
} |
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,35 @@ | ||
--- | ||
source: tests/core/spec.rs | ||
expression: formatter | ||
--- | ||
schema | ||
@server(port: 8000) | ||
@upstream(baseURL: "http://jsonplaceholder.typicode.com", batch: {delay: 100, headers: []}, httpCache: 42) | ||
@link(src: "http://localhost:4000", meta: {name: "Users"}, type: SubGraph) | ||
@link(src: "http://localhost:5000", meta: {name: "Posts"}, type: SubGraph) { | ||
query: Query | ||
} | ||
|
||
type Post { | ||
body: String! | ||
id: Int! | ||
title: String! | ||
user: User @http(path: "/users/{{.value.userId}}") | ||
userId: Int! | ||
} | ||
|
||
type Query { | ||
posts: [Post] @http(path: "/posts") | ||
user(id: Int!): User @http(path: "/users/{{.args.id}}") | ||
users: [User] @http(path: "/users") | ||
version: String @expr(body: "test") | ||
} | ||
|
||
type User { | ||
email: String! | ||
id: Int! | ||
name: String! | ||
phone: String | ||
username: String! | ||
website: 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
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,74 @@ | ||
# Tailcall Federation router | ||
|
||
```graphql @config | ||
schema | ||
@link(src: "http://localhost:4000", type: SubGraph, meta: {name: "Users"}) | ||
@link(src: "http://localhost:5000", type: SubGraph, meta: {name: "Posts"}) | ||
{ | ||
query: Query | ||
} | ||
|
||
type Query { | ||
version: String @expr(body: "test") | ||
} | ||
``` | ||
|
||
```yml @mock | ||
- request: | ||
method: POST | ||
url: http://localhost:4000/graphql | ||
textBody: {"query": "{ config }"} | ||
response: | ||
status: 200 | ||
body: | ||
config: | | ||
schema | ||
@server(port: 8000) | ||
@upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42, batch: {delay: 100}) { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
users: [User] @http(path: "/users") | ||
user(id: Int!): User @http(path: "/users/{{.args.id}}") | ||
} | ||
|
||
type User { | ||
id: Int! | ||
name: String! | ||
username: String! | ||
email: String! | ||
phone: String | ||
website: String | ||
} | ||
|
||
type Post { | ||
userId: Int! | ||
user: User @http(path: "/users/{{.value.userId}}") | ||
} | ||
|
||
- request: | ||
method: POST | ||
url: http://localhost:5000/graphql | ||
textBody: {"query": "{ config }"} | ||
response: | ||
status: 200 | ||
body: | ||
config: | | ||
schema | ||
@server(port: 8000) | ||
@upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42, batch: {delay: 100}) { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
posts: [Post] @http(path: "/posts") | ||
} | ||
|
||
type Post { | ||
id: Int! | ||
userId: Int! | ||
title: String! | ||
body: String! | ||
} | ||
``` |