forked from facebook/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_artifacts.rs
81 lines (77 loc) · 2.83 KB
/
generate_artifacts.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* 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 super::apply_transforms::Programs;
use crate::config::ConfigProject;
use crate::errors::BuildProjectError;
use graphql_ir::FragmentDefinition;
use graphql_text_printer::print_full_operation;
use interner::StringKey;
use persist_query::persist;
use signedsource::{sign_file, SIGNING_TOKEN};
/// Represents a generated output artifact.
pub struct Artifact {
pub name: StringKey,
pub content: String,
}
pub async fn generate_artifacts(
project_config: &ConfigProject,
programs: &Programs<'_>,
) -> Result<Vec<Artifact>, BuildProjectError> {
let mut artifacts = Vec::new();
for node in programs.normalization.operations() {
let name = node.name.item;
let print_operation_node = programs
.operation_text
.operation(name)
.expect("a query text operation should be generated for this operation");
let text = print_full_operation(&programs.operation_text, print_operation_node);
let id = if let Some(ref persist_config) = project_config.persist {
persist(&text, &persist_config.url, &persist_config.params)
.await
.map_err(BuildProjectError::PersistError)?
} else {
"null".to_string()
};
let reader_operation = programs
.reader
.operation(name)
.expect("a reader fragment should be generated for this operation");
let operation_fragment = FragmentDefinition {
name: reader_operation.name,
variable_definitions: reader_operation.variable_definitions.clone(),
selections: reader_operation.selections.clone(),
used_global_variables: Default::default(),
directives: reader_operation.directives.clone(),
type_condition: reader_operation.type_,
};
artifacts.push(Artifact {
name,
content: sign_file(&format!(
"// {}\n\nconst request = {};\n\nconst text = `{}`;\n\nconst id = '{}';\n",
SIGNING_TOKEN,
relay_codegen::print_request_deduped(
programs.normalization.schema(),
node,
&operation_fragment,
),
text,
id,
)),
});
}
for node in programs.reader.fragments() {
artifacts.push(Artifact {
name: node.name.item,
content: sign_file(&format!(
"// {}\n\nconst fragment = {};\n",
SIGNING_TOKEN,
relay_codegen::print_fragment_deduped(programs.normalization.schema(), node),
)),
});
}
Ok(artifacts)
}