Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expr): enhance expr with JQ functionality #3191

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

karatakis
Copy link
Contributor

@karatakis karatakis commented Dec 3, 2024

Summary:

  • Enable jq syntax inside the mustache of @expr directives and select operations.
  • Report template execution (runtime) errors on response
  • Optimized handling of mustache: if it is read-only we use the faster Mustache implementation, otherwise we use Jq

Broken changes:

  • On the select argument of @http, ... directives we cannot prepend the .args selector. So the users have to manually add it if they use Jq features only.

From:

@http(..., select: {id: "{{.id | tostring }}")

Becomes

@http(..., select: {id: "{{.args.id | tostring }}")

Example:

schema
  @server(port: 8000) {
  query: Query
}

type Query {
  users: [User] @http(url: "http://jsonplaceholder.typicode.com/users")
}

type User {
  id: Int!
  posts: [Post] @http(url: "http://jsonplaceholder.typicode.com/users/{{.value.id}}/posts")
}

type Post {
  id: Int!
  title: String!
  body: String!
  bodyParts: BodyParts! @expr(body: "{{ .value.body | split(\" \") | {first: .[0], second: .[1]} }}")
}

type BodyParts {
  first: String!
  second: String!
}

Build & Testing:

  • I ran cargo test successfully.
  • I have run ./lint.sh --mode=fix to fix all linting issues raised by ./lint.sh --mode=check.

Checklist:

  • I have added relevant unit & integration tests.
  • I have updated the documentation accordingly.
  • I have performed a self-review of my code.
  • PR follows the naming convention of <type>(<optional scope>): <title>

@github-actions github-actions bot added the type: feature Brand new functionality, features, pages, workflows, endpoints, etc. label Dec 3, 2024
@karatakis karatakis changed the title feat(mustache): enhance mustache with JQ funtionality feat(expr): enhance expr with JQ funtionality Dec 5, 2024
Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 56.08365% with 462 lines in your changes missing coverage. Please review.

Project coverage is 85.65%. Comparing base (0b05b83) to head (ed65705).

Files with missing lines Patch % Lines
src/core/mustache/jq_value.rs 21.51% 332 Missing ⚠️
src/core/mustache/jq_transform.rs 77.51% 105 Missing ⚠️
src/core/path.rs 29.41% 12 Missing ⚠️
src/core/mustache/render_value.rs 91.93% 5 Missing ⚠️
src/core/mustache/eval.rs 57.14% 3 Missing ⚠️
src/core/http/request_template.rs 0.00% 2 Missing ⚠️
src/core/blueprint/dynamic_value.rs 87.50% 1 Missing ⚠️
src/core/ir/error.rs 50.00% 1 Missing ⚠️
src/core/mustache/parse.rs 97.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3191      +/-   ##
==========================================
- Coverage   86.78%   85.65%   -1.13%     
==========================================
  Files         281      284       +3     
  Lines       28443    29421     +978     
==========================================
+ Hits        24683    25202     +519     
- Misses       3760     4219     +459     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@karatakis karatakis marked this pull request as ready for review December 5, 2024 13:45
@karatakis karatakis marked this pull request as draft December 5, 2024 16:01
@tusharmath tusharmath changed the title feat(expr): enhance expr with JQ funtionality feat(expr): enhance expr with JQ functionality Dec 9, 2024
@github-actions github-actions bot added the ci: benchmark Runs benchmarks label Dec 10, 2024
@karatakis karatakis marked this pull request as ready for review December 10, 2024 15:07
@karatakis
Copy link
Contributor Author

karatakis commented Dec 10, 2024

Benchmarks: before/after JQ Template

TC_LOG_LEVEL=error cargo run --release start main.graphql

schema @server(port: 8000, hostname: "0.0.0.0") {
  query: Query
}

type Query {
  args(text: String!): String! @expr(body: "{{.args.text}}")
}
request = function()
    local headers = {}
    headers["Content-Type"] = "application/json"

    local body = [[
    {
        "query": "query { args(text: \"Hello, World!\") }"
    }
    ]]

    return wrk.format("POST", "/graphql", headers, body)
end

Before

$ wrk -t12 -c400 -d30s -s bench.lua http://127.0.0.1:8000/graphql
Running 30s test @ http://127.0.0.1:8000/graphql
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.90ms  648.63us  10.81ms   75.83%
    Req/Sec    35.51k     3.33k   46.54k    67.11%
  12732725 requests in 30.04s, 1.67GB read
Requests/sec: 423842.17
Transfer/sec:     56.99MB

After

$ wrk -t12 -c400 -d30s -s bench.lua http://127.0.0.1:8000/graphql
Running 30s test @ http://127.0.0.1:8000/graphql
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.88ms  633.52us   9.94ms   76.60%
    Req/Sec    36.78k     3.90k   48.32k    67.31%
  13190038 requests in 30.05s, 1.73GB read
Requests/sec: 439007.72
Transfer/sec:     59.03MB

Copy link

github-actions bot commented Dec 10, 2024

🐰 Bencher Report

Branchfeat/jq-template-hybrid
Testbedbenchmarking-runner
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
nanoseconds (ns)
(Result Δ%)
Upper Boundary
nanoseconds (ns)
(Limit %)
from_json_bench📈 view plot
🚷 view threshold
6,259,800.00
(+0.80%)
6,804,157.68
(92.00%)
group_by📈 view plot
🚷 view threshold
512.06
(+5.06%)
541.12
(94.63%)
input/args.missing📈 view plot
🚷 view threshold
22.21
(-8.03%)
29.10
(76.34%)
input/args.nested.existing📈 view plot
🚷 view threshold
42.97
(-2.43%)
49.83
(86.23%)
input/args.nested.missing📈 view plot
🚷 view threshold
37.19
(-1.20%)
43.34
(85.82%)
input/args.root📈 view plot
🚷 view threshold
38.93
(-1.96%)
45.06
(86.41%)
input/headers.existing📈 view plot
🚷 view threshold
32.92
(+4.07%)
35.18
(93.56%)
input/headers.missing📈 view plot
🚷 view threshold
31.67
(+2.59%)
34.88
(90.80%)
input/value.missing📈 view plot
🚷 view threshold
22.39
(-3.55%)
25.84
(86.67%)
input/value.nested.existing📈 view plot
🚷 view threshold
42.26
(-1.11%)
47.68
(88.62%)
input/value.nested.missing📈 view plot
🚷 view threshold
36.00
(-0.52%)
40.98
(87.85%)
input/value.root📈 view plot
🚷 view threshold
38.74
(-1.24%)
44.21
(87.65%)
input/vars.existing📈 view plot
🚷 view threshold
7.75
(-4.80%)
9.55
(81.24%)
input/vars.missing📈 view plot
🚷 view threshold
8.38
(-19.60%)
13.46
(62.27%)
synth_nested📈 view plot
🚷 view threshold
101,080.00
(-17.47%)
158,101.41
(63.93%)
synth_nested_borrow📈 view plot
🚷 view threshold
37,887.00
(-44.67%)
110,752.73
(34.21%)
test_batched_body📈 view plot
🚷 view threshold
2,002.90
(+5.79%)
2,069.55
(96.78%)
test_batched_body #2📈 view plot
🚷 view threshold
1,562,700.00
(+2.75%)
1,663,623.46
(93.93%)
test_data_loader📈 view plot
🚷 view threshold
395,050.00
(-0.85%)
432,878.85
(91.26%)
test_handle_request📈 view plot
🚷 view threshold
128,040.00
(-1.56%)
143,351.25
(89.32%)
test_handle_request_jit📈 view plot
🚷 view threshold
53,005.00
(-43.48%)
156,028.65
(33.97%)
test_http_execute_method📈 view plot
🚷 view threshold
14,935.00
(+2.94%)
16,055.79
(93.02%)
with_mustache_expressions📈 view plot
🚷 view threshold
1,029.70
(-6.45%)
1,242.38
(82.88%)
with_mustache_literal📈 view plot
🚷 view threshold
616.42
(-12.21%)
803.79
(76.69%)
🐰 View full continuous benchmarking report in Bencher

src/core/path.rs Outdated Show resolved Hide resolved
src/core/runtime.rs Outdated Show resolved Hide resolved
@tusharmath tusharmath marked this pull request as draft December 10, 2024 20:27
@karatakis karatakis marked this pull request as ready for review December 12, 2024 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci: benchmark Runs benchmarks type: feature Brand new functionality, features, pages, workflows, endpoints, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants