Skip to content

Commit

Permalink
feat: update mustache syntax (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Apr 22, 2024
1 parent 13f6172 commit a6cea61
Show file tree
Hide file tree
Showing 144 changed files with 445 additions and 346 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ schema

type Query {
posts: [Post] @http(path: "/posts")
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -81,7 +81,7 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
```

Expand Down
2 changes: 1 addition & 1 deletion ci-benchmark/benchmark.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
6 changes: 3 additions & 3 deletions examples/auth.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ schema

type Query {
posts: [Post] @http(path: "/posts")
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type Mutation {
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User @protected {
Expand All @@ -30,5 +30,5 @@ type Post {
userId: Int!
title: String!
body: String! @protected
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
6 changes: 3 additions & 3 deletions examples/call.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type Post {
}

type Query {
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
firstUser: User @call(steps: [{query: "user", args: {id: 1}}])
postFromUser(userId: Int!): [Post] @http(path: "/posts?userId={{args.userId}}")
postFromUser(userId: Int!): [Post] @http(path: "/posts?userId={{.args.userId}}")
}

type User {
id: Int
name: String
posts: [Post] @call(steps: [{query: "postFromUser", args: {userId: "{{value.id}}"}}])
posts: [Post] @call(steps: [{query: "postFromUser", args: {userId: "{{.value.id}}"}}])
}
2 changes: 1 addition & 1 deletion examples/graphql-composition.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @graphQL(name: "user", args: [{key: "id", value: "{{value.userId}}"}])
user: User @graphQL(name: "user", args: [{key: "id", value: "{{.value.userId}}"}])
}

type User {
Expand Down
4 changes: 2 additions & 2 deletions examples/grpc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ schema

type Query {
news: NewsData! @grpc(method: "news.NewsService.GetAllNews")
newsById(news: NewsInput!): News! @grpc(method: "news.NewsService.GetNews", body: "{{args.news}}")
newsById(news: NewsInput!): News! @grpc(method: "news.NewsService.GetNews", body: "{{.args.news}}")
newsByIdBatch(news: NewsInput!): News!
@grpc(method: "news.NewsService.GetMultipleNews", body: "{{args.news}}", batchKey: ["news", "id"])
@grpc(method: "news.NewsService.GetMultipleNews", body: "{{.args.news}}", batchKey: ["news", "id"])
}

type News {
Expand Down
4 changes: 2 additions & 2 deletions examples/jsonplaceholder.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ schema
type Query {
posts: [Post] @http(path: "/posts")
users: [User] @http(path: "/users")
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -24,5 +24,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @call(steps: [{query: "user", args: {id: "{{value.userId}}"}}])
user: User @call(steps: [{query: "user", args: {id: "{{.value.userId}}"}}])
}
2 changes: 1 addition & 1 deletion examples/jsonplaceholder_batch.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users", query: [{key: "id", value: "{{value.userId}}"}], batchKey: ["id"])
user: User @http(path: "/users", query: [{key: "id", value: "{{.value.userId}}"}], batchKey: ["id"])
}
2 changes: 1 addition & 1 deletion examples/jsonplaceholder_http_2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
4 changes: 2 additions & 2 deletions examples/jsonplaceholder_script.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ schema

type Query {
posts: [Post] @http(path: "/posts")
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -24,5 +24,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
4 changes: 2 additions & 2 deletions examples/rest-api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ schema
type Query {
posts: [Post] @http(path: "/posts")
users: [User] @http(path: "/users")
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -25,5 +25,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @call(steps: [{query: "user", args: {id: "{{value.userId}}"}}])
user: User @call(steps: [{query: "user", args: {id: "{{.value.userId}}"}}])
}
6 changes: 3 additions & 3 deletions examples/telemetry-otlp.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ schema
url: "https://api.honeycomb.io:443"
# gather api key from https://ui.honeycomb.io and set it as env when running tailcall
headers: [
{key: "x-honeycomb-team", value: "{{env.HONEYCOMB_API_KEY}}"}
{key: "x-honeycomb-team", value: "{{.env.HONEYCOMB_API_KEY}}"}
{key: "x-honeycomb-dataset", value: "tailcall"}
]
}
Expand All @@ -21,7 +21,7 @@ schema

type Query {
posts: [Post] @http(path: "/posts") @cache(maxAge: 3000)
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
users: [User] @http(path: "/users")
news: NewsData! @grpc(baseURL: "http://localhost:50051", method: "news.NewsService.GetAllNews")
}
Expand All @@ -40,7 +40,7 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}

type News {
Expand Down
4 changes: 2 additions & 2 deletions examples/telemetry-prometheus.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ schema

type Query {
posts: [Post] @http(path: "/posts") @cache(maxAge: 5000)
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -24,5 +24,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
4 changes: 2 additions & 2 deletions examples/telemetry-stdout.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ schema

type Query {
posts: [Post] @http(path: "/posts") @cache(maxAge: 5000)
user(id: Int!): User @http(path: "/users/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User {
Expand All @@ -24,5 +24,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
18 changes: 12 additions & 6 deletions src/mustache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use nom::branch::alt;
use nom::bytes::complete::{tag, take_until};
use nom::character::complete::char;
Expand Down Expand Up @@ -92,9 +94,9 @@ impl Mustache {
}
}

impl ToString for Mustache {
fn to_string(&self) -> String {
match self {
impl Display for Mustache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Mustache(segments) => segments
.iter()
.map(|segment| match segment {
Expand All @@ -103,7 +105,8 @@ impl ToString for Mustache {
})
.collect::<Vec<String>>()
.join(""),
}
};
write!(f, "{}", str)
}
}

Expand All @@ -127,8 +130,11 @@ fn parse_expression(input: &str) -> IResult<&str, Segment> {
delimited(
tag("{{"),
map(
nom::multi::separated_list1(char('.'), parse_name),
Segment::Expression,
nom::sequence::tuple((
nom::combinator::opt(char('.')), // Optional leading dot
nom::multi::separated_list1(char('.'), parse_name),
)),
|(_, expr_parts)| Segment::Expression(expr_parts),
),
tag("}}"),
)(input)
Expand Down
8 changes: 4 additions & 4 deletions tailcall-query-plan/tests/config/user-posts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ schema
type Query {
posts: [Post] @http(path: "/posts")
users: [User] @http(path: "/users")
user(id: Int!): User @http(path: "/users/{{args.id}}")
post(id: Int!): Post @http(path: "/posts/{{args.id}}")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
post(id: Int!): Post @http(path: "/posts/{{.args.id}}")
}

type User {
Expand All @@ -17,13 +17,13 @@ type User {
username: String!
email: String!
phone: String
website: String @expr(body: "/users/website/{{value.username}}")
website: String @expr(body: "/users/website/{{.value.username}}")
}

type Post {
id: Int!
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}
6 changes: 5 additions & 1 deletion tests/execution/batching-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type Post {
body: String
userId: Int!
user: User
@http(path: "/users", query: [{key: "id", value: "{{value.userId}}"}, {key: "foo", value: "bar"}], batchKey: ["id"])
@http(
path: "/users"
query: [{key: "id", value: "{{.value.userId}}"}, {key: "foo", value: "bar"}]
batchKey: ["id"]
)
}

type User {
Expand Down
2 changes: 1 addition & 1 deletion tests/execution/batching-disabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"http": {
"path": "/users/{{args.id}}"
"path": "/users/{{.args.id}}"
},
"cache": null
}
Expand Down
6 changes: 5 additions & 1 deletion tests/execution/batching-group-by-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ type Post {
body: String
userId: Int!
user: User
@http(batchKey: ["id"], path: "/users", query: [{key: "id", value: "{{value.userId}}"}, {key: "foo", value: "bar"}])
@http(
batchKey: ["id"]
path: "/users"
query: [{key: "id", value: "{{.value.userId}}"}, {key: "foo", value: "bar"}]
)
}

type User {
Expand Down
6 changes: 5 additions & 1 deletion tests/execution/batching-group-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ type Post {
body: String
userId: Int!
user: User
@http(path: "/users", query: [{key: "id", value: "{{value.userId}}"}, {key: "foo", value: "bar"}], batchKey: ["id"])
@http(
path: "/users"
query: [{key: "id", value: "{{.value.userId}}"}, {key: "foo", value: "bar"}]
batchKey: ["id"]
)
}

type User {
Expand Down
2 changes: 1 addition & 1 deletion tests/execution/batching-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Post {
title: String
body: String
userId: Int!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(path: "/users/{{.value.userId}}")
}

type User {
Expand Down
2 changes: 1 addition & 1 deletion tests/execution/cache-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"query": [
{
"key": "id",
"value": "{{args.id}}"
"value": "{{.args.id}}"
}
],
"baseURL": "http://jsonplaceholder.typicode.com"
Expand Down
2 changes: 1 addition & 1 deletion tests/execution/caching-collision.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Foo {

type Bar {
id: String!
foo: Foo @http(path: "/foo?id={{value.id}}") @cache(maxAge: 300)
foo: Foo @http(path: "/foo?id={{.value.id}}") @cache(maxAge: 300)
}
```

Expand Down
4 changes: 2 additions & 2 deletions tests/execution/call-graphql-datasource.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ schema
type Query {
posts: [Post] @http(path: "/posts")
user(id: Int!): User
@graphQL(baseURL: "http://upstream/graphql", name: "user", args: [{key: "id", value: "{{args.id}}"}])
@graphQL(baseURL: "http://upstream/graphql", name: "user", args: [{key: "id", value: "{{.args.id}}"}])
}

type User {
Expand All @@ -27,7 +27,7 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @call(steps: [{query: "user", args: {id: "{{value.userId}}"}}])
user: User @call(steps: [{query: "user", args: {id: "{{.value.userId}}"}}])
}
```

Expand Down
Loading

1 comment on commit a6cea61

@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 7.54ms 3.50ms 80.17ms 72.62%
Req/Sec 3.36k 285.74 6.24k 87.27%

402417 requests in 30.10s, 2.02GB read

Requests/sec: 13370.96

Transfer/sec: 68.63MB

Please sign in to comment.