Skip to content

Commit

Permalink
fix: Incorrect Field Path Specification in @addfield (#2895)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Sep 25, 2024
1 parent 551b50e commit 49d925e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/core/config/from_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ where
.fuse(Cache::from_directives(directives.iter()))
.fuse(to_fields(fields))
.fuse(Protected::from_directives(directives.iter()))
.map(|(resolver, cache, fields, protected)| {
.fuse(to_add_fields_from_directives(directives))
.map(|(resolver, cache, fields, protected, added_fields)| {
let doc = description.to_owned().map(|pos| pos.node);
let implements = implements.iter().map(|pos| pos.node.to_string()).collect();
let added_fields = to_add_fields_from_directives(directives);
config::Type {
fields,
added_fields,
Expand Down Expand Up @@ -410,19 +410,16 @@ fn to_enum(enum_type: EnumType, doc: Option<String>) -> Valid<Enum, String> {

fn to_add_fields_from_directives(
directives: &[Positioned<ConstDirective>],
) -> Vec<config::AddField> {
directives
.iter()
.filter_map(|directive| {
if directive.node.name.node == config::AddField::directive_name() {
config::AddField::from_directive(&directive.node)
.to_result()
.ok()
} else {
None
}
})
.collect::<Vec<_>>()
) -> Valid<Vec<config::AddField>, String> {
Valid::from_iter(
directives
.iter()
.filter(|v| v.node.name.node == config::AddField::directive_name()),
|directive| {
let val = config::AddField::from_directive(&directive.node).to_result();
Valid::from(val)
},
)
}

trait HasName {
Expand Down
15 changes: 15 additions & 0 deletions tests/core/snapshots/test-invalid-add-field.md_error.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/core/spec.rs
expression: errors
---
[
{
"message": "Parsing failed because of invalid type: string \"{{.value.user.username}}\", expected a sequence",
"trace": [
"PostUser",
"@addField",
"path"
],
"description": null
}
]
31 changes: 31 additions & 0 deletions tests/execution/test-invalid-add-field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
error: true
---

# Test invalid add fields

```graphql @config
schema @server(port: 8000) @upstream(baseURL: "http://jsonplaceholder.typicode.com") {
query: Query
}

type Query {
postuser: [PostUser] @http(path: "/posts")
}

type PostUser @addField(name: "username", path: "{{.value.user.username}}") {
id: Int! @modify(name: "postId")
title: String!
userId: Int!
user: User @http(baseURL: "https://jsonplaceholder.typicode.com", path: "/users/{{.value.userId}}")
}

type User {
id: Int!
name: String!
username: String!
email: String!
phone: String
website: String
}
```

1 comment on commit 49d925e

@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 11.76ms 4.83ms 129.05ms 91.58%
Req/Sec 2.16k 243.96 2.93k 87.25%

258670 requests in 30.02s, 1.30GB read

Requests/sec: 8615.51

Transfer/sec: 44.22MB

Please sign in to comment.