diff --git a/compiler/crates/relay-codegen/src/build_ast.rs b/compiler/crates/relay-codegen/src/build_ast.rs index a93bd779375d1..0f0036e6b6bfb 100644 --- a/compiler/crates/relay-codegen/src/build_ast.rs +++ b/compiler/crates/relay-codegen/src/build_ast.rs @@ -1868,24 +1868,21 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> { metadata_items.sort_unstable_by_key(|entry| entry.key); // Construct metadata object - let metadata_prop = ObjectEntry { - key: CODEGEN_CONSTANTS.metadata, - value: Primitive::Key(self.object(metadata_items)), - }; - let name_prop = ObjectEntry { - key: CODEGEN_CONSTANTS.name, - value: Primitive::String(request_parameters.name), - }; - let operation_kind_prop = ObjectEntry { - key: CODEGEN_CONSTANTS.operation_kind, - value: Primitive::String(match request_parameters.operation_kind { - OperationKind::Query => CODEGEN_CONSTANTS.query, - OperationKind::Mutation => CODEGEN_CONSTANTS.mutation, - OperationKind::Subscription => CODEGEN_CONSTANTS.subscription, - }), - }; + let mut params_object = vec![]; - let id_prop = ObjectEntry { + if let Some(ref text) = &request_parameters.text { + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.cache_id, + value: Primitive::RawString(md5(&text)), + }); + } else if request_parameters.id.is_none() { + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.cache_id, + value: Primitive::RawString(md5(operation.name.item.0.lookup())), + }); + } + + params_object.push(ObjectEntry { key: CODEGEN_CONSTANTS.id, value: match request_parameters.id { Some(QueryID::Persisted { id, .. }) => Primitive::RawString(id.clone()), @@ -1897,50 +1894,31 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> { } None => Primitive::Null, }, - }; + }); + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.metadata, + value: Primitive::Key(self.object(metadata_items)), + }); + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.name, + value: Primitive::String(request_parameters.name), + }); + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.operation_kind, + value: Primitive::String(match request_parameters.operation_kind { + OperationKind::Query => CODEGEN_CONSTANTS.query, + OperationKind::Mutation => CODEGEN_CONSTANTS.mutation, + OperationKind::Subscription => CODEGEN_CONSTANTS.subscription, + }), + }); - let mut params_object = if let Some(text) = request_parameters.text { - vec![ - ObjectEntry { - key: CODEGEN_CONSTANTS.cache_id, - value: Primitive::RawString(md5(&text)), - }, - id_prop, - metadata_prop, - name_prop, - operation_kind_prop, - ObjectEntry { - key: CODEGEN_CONSTANTS.text, - value: Primitive::RawString(text), - }, - ] - } else if request_parameters.id.is_some() { - vec![ - id_prop, - metadata_prop, - name_prop, - operation_kind_prop, - ObjectEntry { - key: CODEGEN_CONSTANTS.text, - value: Primitive::Null, - }, - ] - } else { - vec![ - ObjectEntry { - key: CODEGEN_CONSTANTS.cache_id, - value: Primitive::RawString(md5(operation.name.item.0.lookup())), - }, - id_prop, - metadata_prop, - name_prop, - operation_kind_prop, - ObjectEntry { - key: CODEGEN_CONSTANTS.text, - value: Primitive::Null, - }, - ] - }; + params_object.push(ObjectEntry { + key: CODEGEN_CONSTANTS.text, + value: match request_parameters.text { + Some(text) => Primitive::RawString(text), + None => Primitive::Null, + }, + }); let provided_variables = if top_level_statements .contains(CODEGEN_CONSTANTS.provided_variables_definition.lookup()) diff --git a/compiler/crates/relay-compiler/src/artifact_content/content.rs b/compiler/crates/relay-compiler/src/artifact_content/content.rs index 31882f84b938d..36f76ddba6d50 100644 --- a/compiler/crates/relay-compiler/src/artifact_content/content.rs +++ b/compiler/crates/relay-compiler/src/artifact_content/content.rs @@ -176,11 +176,20 @@ pub fn generate_operation( fragment_locations: &FragmentLocations, ) -> Result, FmtError> { let mut request_parameters = build_request_params(normalization_operation); + if id_and_text_hash.is_some() { request_parameters.id = id_and_text_hash; + if project_config + .persist + .as_ref() + .map_or(false, |config| config.include_query_text()) + { + request_parameters.text = text.clone(); + } } else { request_parameters.text = text.clone(); - }; + } + let operation_fragment = FragmentDefinition { name: reader_operation.name.map(|x| FragmentDefinitionName(x.0)), variable_definitions: reader_operation.variable_definitions.clone(), diff --git a/compiler/crates/relay-config/src/project_config.rs b/compiler/crates/relay-config/src/project_config.rs index d76dcbce01ab3..91b71e26af1cd 100644 --- a/compiler/crates/relay-config/src/project_config.rs +++ b/compiler/crates/relay-config/src/project_config.rs @@ -43,7 +43,7 @@ type FnvIndexMap = IndexMap; pub type ProjectName = StringKey; #[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct RemotePersistConfig { /// URL to send a POST request to to persist. pub url: String, @@ -62,6 +62,9 @@ pub struct RemotePersistConfig { deserialize_with = "deserialize_semaphore_permits" )] pub semaphore_permits: Option, + + #[serde(default)] + pub include_query_text: bool, } fn deserialize_semaphore_permits<'de, D>(d: D) -> Result, D::Error> @@ -98,6 +101,9 @@ pub struct LocalPersistConfig { #[serde(default)] pub algorithm: LocalPersistAlgorithm, + + #[serde(default)] + pub include_query_text: bool, } #[derive(Debug, Serialize, Clone)] @@ -107,6 +113,15 @@ pub enum PersistConfig { Local(LocalPersistConfig), } +impl PersistConfig { + pub fn include_query_text(&self) -> bool { + match self { + PersistConfig::Remote(remote_config) => remote_config.include_query_text, + PersistConfig::Local(local_config) => local_config.include_query_text, + } + } +} + impl<'de> Deserialize<'de> for PersistConfig { fn deserialize>(deserializer: D) -> std::result::Result { let value = Value::deserialize(deserializer)?; diff --git a/packages/relay-runtime/util/RelayConcreteNode.js b/packages/relay-runtime/util/RelayConcreteNode.js index a0fd9532a75d5..776851f3d72a7 100644 --- a/packages/relay-runtime/util/RelayConcreteNode.js +++ b/packages/relay-runtime/util/RelayConcreteNode.js @@ -43,14 +43,14 @@ export type ProvidedVariablesType = {+[key: string]: {get(): mixed}}; /** * Contains the parameters required for executing a GraphQL request. - * The operation can either be provided as a persisted `id` or `text`. If given - * in `text` format, a `cacheID` as a hash of the text should be set to be used - * for local caching. + * The operation can either be provided as a persisted `id` or `text` or both. + * If `text` format is provided, a `cacheID` as a hash of the text should be set + * to be used for local caching. */ export type RequestParameters = | { +id: string, - +text: null, + +text: string | null, // common fields +name: string, +operationKind: 'mutation' | 'query' | 'subscription',