Skip to content

Commit

Permalink
add tests for js directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed May 24, 2024
1 parent c088e92 commit 10aa70b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/cli/javascript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod runtime;
pub use request_filter::RequestFilter;
pub use runtime::Runtime;

use crate::core::{blueprint, HttpIO};
use crate::core::{blueprint, HttpIO, WorkerIO};

pub fn init_http(
http: Arc<impl HttpIO>,
Expand All @@ -25,6 +25,13 @@ pub fn init_http(
Arc::new(RequestFilter::new(http, script_io))
}

pub fn init_worker_io<T, V>(script: blueprint::Script) -> Arc<dyn WorkerIO<T, V> + Send + Sync>
where
Runtime: WorkerIO<T, V>,
{
(Arc::new(Runtime::new(script))) as _
}

fn create_header_map(
headers: BTreeMap<String, String>,
) -> anyhow::Result<reqwest::header::HeaderMap> {
Expand Down
19 changes: 16 additions & 3 deletions tests/core/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,23 +272,36 @@ impl ExecutionSpec {
http_client: Arc<Http>,
) -> Arc<AppContext> {
let blueprint = Blueprint::try_from(config).unwrap();
let http = if let Some(script) = blueprint.server.script.clone() {
let script = blueprint.server.script.clone();
let http = if let Some(script) = script.clone() {
javascript::init_http(http_client, script)
} else {
http_client
};

let http2_only = http.clone();

let http_worker = if let Some(script) = script.clone() {
javascript::init_worker_io(script)
} else {
Arc::new(DefaultJsRuntime {})
};

let worker = if let Some(script) = script {
javascript::init_worker_io(script)
} else {
Arc::new(DefaultJsRuntime {})
};

let runtime = TargetRuntime {
http,
http2_only,
file: Arc::new(File::new(self.clone())),
env: Arc::new(Env::init(env)),
cache: Arc::new(InMemoryCache::new()),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker,
worker,
};

let endpoints = config
Expand Down
2 changes: 1 addition & 1 deletion tests/core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn create_runtime(
http_client.clone()
};

let http2 = if let Some(script) = script {
let http2 = if let Some(script) = script.clone() {
javascript::init_http(http_client.clone(), script)
} else {
http_client.clone()
Expand Down
17 changes: 17 additions & 0 deletions tests/core/snapshots/js-directive.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"hello": {
"name": "Leanne Graham"
}
}
}
}
28 changes: 28 additions & 0 deletions tests/core/snapshots/js-directive.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: tests/core/spec.rs
expression: formatted
---
scalar Date

scalar Email

scalar Empty

scalar JSON

scalar PhoneNumber

type Query {
hello: User!
}

scalar Url

type User {
id: Int!
name: String!
}

schema {
query: Query
}
16 changes: 16 additions & 0 deletions tests/core/snapshots/js-directive.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/core/spec.rs
expression: formatter
---
schema @server @upstream(baseURL: "https://jsonplaceholder.typicode.com") @link(src: "test.js", type: Script) {
query: Query
}

type Query {
hello: User! @http(path: "/users/1")
}

type User {
id: Int!
name: String! @js(name: "name")
}

1 comment on commit 10aa70b

@github-actions
Copy link

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

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.66ms 3.26ms 187.55ms 76.01%
Req/Sec 3.81k 192.98 4.37k 92.17%

454762 requests in 30.01s, 2.28GB read

Requests/sec: 15155.18

Transfer/sec: 77.79MB

Please sign in to comment.