-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add a JSON file query persister strategy to relay-compiler #3666
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use crate::OperationPersister; | ||
use async_trait::async_trait; | ||
use persist_query::PersistError; | ||
use serde::{Deserialize, Serialize}; | ||
use sha1::{Digest, Sha1}; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct FilePersistConfig { | ||
/// Path to a file to persist the document to. | ||
pub file_path: PathBuf, | ||
} | ||
|
||
pub struct FilePersister { | ||
file_path: PathBuf, | ||
} | ||
|
||
impl FilePersister { | ||
pub fn new(path: PathBuf, root_path: PathBuf) -> Self { | ||
// Append the incoming path to the project's root. | ||
let mut file_path = root_path.clone(); | ||
file_path.push(path); | ||
Self { file_path } | ||
} | ||
|
||
fn hash_operation(&self, operation_text: String) -> String { | ||
let mut hash = Sha1::new(); | ||
hash.input(&operation_text); | ||
hex::encode(hash.result()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl OperationPersister for FilePersister { | ||
async fn persist_artifact(&self, operation_text: String) -> Result<String, PersistError> { | ||
let mut map: HashMap<String, String> = if self.file_path.exists() { | ||
let existing_content = std::fs::read_to_string(&self.file_path); | ||
match existing_content { | ||
Ok(content) => serde_json::from_str(&content).unwrap(), | ||
Err(_e) => HashMap::new(), | ||
} | ||
} else { | ||
HashMap::new() | ||
}; | ||
|
||
let op_hash = self.hash_operation(operation_text.clone()); | ||
|
||
map.insert(op_hash.clone(), operation_text); | ||
let content = serde_json::to_string_pretty(&map).unwrap(); | ||
std::fs::write(&self.file_path, content).unwrap(); | ||
|
||
Ok(op_hash) | ||
} | ||
|
||
fn worker_count(&self) -> usize { | ||
0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,40 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use crate::{OperationPersister, PersistConfig}; | ||
use crate::OperationPersister; | ||
use async_trait::async_trait; | ||
use fnv::FnvBuildHasher; | ||
use indexmap::IndexMap; | ||
use persist_query::{persist, PersistError}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub struct RemotePersister; | ||
type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct RemotePersistConfig { | ||
/// URL to send a POST request to to persist. | ||
pub url: String, | ||
/// The document will be in a POST parameter `text`. This map can contain | ||
/// additional parameters to send. | ||
pub params: FnvIndexMap<String, String>, | ||
} | ||
|
||
pub struct RemotePersister { | ||
config: RemotePersistConfig, | ||
} | ||
|
||
impl RemotePersister { | ||
pub fn new(config: RemotePersistConfig) -> Self { | ||
Self { config } | ||
} | ||
} | ||
Comment on lines
+19
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
|
||
#[async_trait] | ||
impl OperationPersister for RemotePersister { | ||
async fn persist_artifact( | ||
&self, | ||
operation_text: String, | ||
persist_config: &PersistConfig, | ||
) -> Result<String, PersistError> { | ||
let params = &persist_config.params; | ||
let url = &persist_config.url; | ||
async fn persist_artifact(&self, operation_text: String) -> Result<String, PersistError> { | ||
let params = &self.config.params; | ||
let url = &self.config.url; | ||
persist(&operation_text, url, params).await | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this
serde
feature, the only problem with it: obscure error messages if both/mixed values are provided.