Skip to content

Commit

Permalink
refactor: key values (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 authored Oct 5, 2023
1 parent 62a7432 commit 881c8ad
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/blueprint/from_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,13 @@ fn update_http(field: &config::Field, mut b_field: FieldDefinition, config: &Con
base_url.pop();
}
base_url.push_str(http.path.clone().as_str());
let query = http.query.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
let query: BTreeMap<String, String> = http.query.clone().into();
let query = query.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
let output_schema = to_json_schema_for_field(field, config);
let input_schema = to_json_schema_for_args(&field.args, config);
let mut header_map = HeaderMap::new();
for (k, v) in http.headers.iter() {
let headers: BTreeMap<String, String> = http.headers.clone().into();
for (k, v) in headers.iter() {
header_map.insert(
HeaderName::from_bytes(k.as_bytes()).map_err(|e| ValidationError::new(e.to_string()))?,
HeaderValue::from_str(v.as_str()).map_err(|e| ValidationError::new(e.to_string()))?,
Expand Down
23 changes: 21 additions & 2 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ pub struct Union {
pub doc: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
pub struct KeyValues(pub Vec<KeyValue>);

impl From<KeyValues> for BTreeMap<String, String> {
fn from(value: KeyValues) -> Self {
let mut map = BTreeMap::new();
for KeyValue { key, value } in value.0 {
map.insert(key, value);
}
map
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
pub struct KeyValue {
pub key: String,
pub value: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Http {
pub path: String,
Expand All @@ -246,7 +265,7 @@ pub struct Http {
pub method: Method,
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
pub query: BTreeMap<String, String>,
pub query: KeyValues,
pub input: Option<JsonSchema>,
pub output: Option<JsonSchema>,
pub body: Option<String>,
Expand All @@ -258,7 +277,7 @@ pub struct Http {
pub base_url: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
pub headers: BTreeMap<String, String>,
pub headers: KeyValues,
}

impl Http {
Expand Down
2 changes: 1 addition & 1 deletion tests/graphql/passed/test-resolve-with-vars.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Query {
user: [User]
@http(
path: "/users"
query: { id: "{{vars.id}}" }
query: [{ key: "id",value: "{{vars.id}}" }]
baseURL: "http://jsonplaceholder.typicode.com"
)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/graphql/passed/test-with-args.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Query {
user(id: Int): [User]
@http(
path: "/users"
query: { id: "{{args.id}}" }
query: [{key: "id",value: "{{args.id}}" }]
baseURL: "http://jsonplaceholder.typicode.com"
)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/graphql/test-http-headers.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ schema @server(baseURL: "http://localhost:4000") {
}

type Query {
foo: String @http(headers: {foo: "bar"}, path: "/foo")
foo: String @http(headers: [{key: "foo",value: "bar"}], path: "/foo")
}

#> client-sdl
Expand Down
2 changes: 1 addition & 1 deletion tests/graphql/test-http-tmpl.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema @server(baseURL: "http://jsonplacheholder.typicode.com") {

type Post {
id: Int
user: User @http(path: "/users", query: {id: "{{value.userId}}"})
user: User @http(path: "/users", query: [{key: "id",value: "{{value.userId}}"}])
userId: Int
}

Expand Down

0 comments on commit 881c8ad

Please sign in to comment.