Skip to content

Commit

Permalink
Support for client-only queries
Browse files Browse the repository at this point in the history
Summary:
This diff removes restriction on client-only generation in Relay.

- This validation is removed from skip_unreachable_nodes and skip_client_extensions.
- The FlowTypes indicate the possibility of the text being `null`.

This change allows the building of complete client-only Relay applications, that may rely on Live Resolvers or imperative updaters APIs to write data to the Relay store.

Reviewed By: captbaritone

Differential Revision: D36890340

fbshipit-source-id: d52942f68aa1497d88d8d7326b3ddb24b3078bdc
  • Loading branch information
alunyov authored and facebook-github-bot committed Jun 7, 2022
1 parent 31b6767 commit 1244359
Show file tree
Hide file tree
Showing 190 changed files with 821 additions and 200 deletions.
15 changes: 15 additions & 0 deletions compiler/crates/relay-codegen/src/build_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,23 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
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.lookup())),
},
id_prop,
metadata_prop,
name_prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ return {
]
},
"params": {
"cacheID": "ac6b4c4fdff87d8c6ce7c26f6e334226",
"id": null,
"metadata": {
"connection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ query QueryWithMetadata {
]
},
"params": {
"cacheID": "2e5967148a8303de3c58059c0eaa87c6",
"id": null,
"metadata": {
"metadataKey": "Hello world!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ pub fn generate_operation(
reader_operation: &OperationDefinition,
typegen_operation: &OperationDefinition,
source_hash: String,
text: &str,
text: &Option<String>,
id_and_text_hash: &Option<QueryID>,
skip_types: bool,
) -> Result<Vec<u8>, FmtError> {
let mut request_parameters = build_request_params(normalization_operation);
if id_and_text_hash.is_some() {
request_parameters.id = id_and_text_hash;
} else {
request_parameters.text = Some(text.into());
request_parameters.text = text.clone();
};
let operation_fragment = FragmentDefinition {
name: reader_operation.name,
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/artifact_content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum ArtifactContent {
reader_operation: Arc<OperationDefinition>,
typegen_operation: Arc<OperationDefinition>,
source_hash: String,
text: String,
text: Option<String>,
id_and_text_hash: Option<QueryID>,
},
UpdatableQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ fn generate_normalization_artifact(
source_hash: String,
source_file: SourceLocationKey,
) -> Artifact {
let text = operation_printer.print(operations.expect_operation_text());
let text = operations
.operation_text
.map(|operation| operation_printer.print(operation));
Artifact {
source_definition_names: vec![source_definition_name],
path: project_config.path_for_artifact(source_file, operations.normalization.name.item),
Expand Down Expand Up @@ -238,15 +240,6 @@ struct OperationGroup<'a> {
}

impl<'a> OperationGroup<'a> {
fn expect_operation_text(&self) -> &OperationDefinition {
self.operation_text.unwrap_or_else(|| {
panic!(
"Expected to have a operation_text operation for `{}`",
self.normalization.name.item
)
})
}

fn expect_reader(&self) -> Arc<OperationDefinition> {
Arc::clone(self.reader.unwrap_or_else(|| {
panic!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn persist_operations(
{
*id_and_text_hash = Some(QueryID::External(virtual_id_file_name));
None
} else {
} else if let Some(text) = text {
let text_hash = md5(text);
let artifact_path = root_dir.join(&artifact.path);
let extracted_persist_id = if config.repersist_operations {
Expand All @@ -70,6 +70,8 @@ pub async fn persist_operations(
})
})
}
} else {
None
}
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorFragmentSpreadConditi
]
},
"params": {
"cacheID": "9206eaaffad1e12f6e7727cd01cd417c",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorFragmentSpreadConditional_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorFragmentSpread_NodeFr
]
},
"params": {
"cacheID": "60e9b9f0d89c6dfcec940caf231e5f2c",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorFragmentSpread_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ query abstractTypeRefinementDontFlattenTypeDiscriminatorInlineFragmentConditiona
]
},
"params": {
"cacheID": "2e9adb24082d7b97e5ba9fdda362d573",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorInlineFragmentConditional_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ query abstractTypeRefinementDontFlattenTypeDiscriminatorInlineFragment_AbstractT
]
},
"params": {
"cacheID": "5412f6e444d6ae99909f15f3f7d9f76b",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorInlineFragment_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpreadW
]
},
"params": {
"cacheID": "aa874916213500bac85f55bac70ef1a1",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpreadWithinInlineFragmentDifferentFields_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpreadW
]
},
"params": {
"cacheID": "8e92705f7705cc41780b1d42d90ab733",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpreadWithinInlineFragment_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpread_
]
},
"params": {
"cacheID": "b18f324ebb51d89a4e7974512a17ef56",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorNestedFragmentSpread_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fragment abstractTypeRefinementDontFlattenTypeDiscriminatorNestedInlineFragment_
]
},
"params": {
"cacheID": "e3c542e4d182a128425d6c1e29b19ca4",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontFlattenTypeDiscriminatorNestedInlineFragment_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fragment abstractTypeRefinementDontSkipTypeDiscriminatorFragmentSpread_NodeFragm
]
},
"params": {
"cacheID": "46e852a1d0a587f408f3cb577806af0b",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontSkipTypeDiscriminatorFragmentSpread_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fragment abstractTypeRefinementDontSkipTypeDiscriminatorInlineFragment_ActorFrag
]
},
"params": {
"cacheID": "7f1d413029d4f6717a12f1141f0f9c61",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontSkipTypeDiscriminatorInlineFragment_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fragment abstractTypeRefinementDontSkipTypeDiscriminatorWhenIdenticalSelections_
]
},
"params": {
"cacheID": "5fc3aca129e962f11991489716725fce",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementDontSkipTypeDiscriminatorWhenIdenticalSelections_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ mutation abstractTypeRefinementNoUnnecessaryTypeDiscriminatorUnderConditionIncor
]
},
"params": {
"cacheID": "100754c2e67f31b602606199788fcc24",
"id": null,
"metadata": {},
"name": "abstractTypeRefinementNoUnnecessaryTypeDiscriminatorUnderConditionIncorrect_ChangeNameMutation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fragment abstractTypeRefinement_ActorFragment on Actor {
]
},
"params": {
"cacheID": "feacbf4c38e59aa276558a9018654a7e",
"id": null,
"metadata": {},
"name": "abstractTypeRefinement_AbstractTypeRefinementQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fragment actorChangeSimpleQueryUserFragment on User {
]
},
"params": {
"cacheID": "5b421dc4587c0c01cd781123e133ab7d",
"id": null,
"metadata": {},
"name": "actorChangeSimpleQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ query aliasSameAsName_TestQuery {
]
},
"params": {
"cacheID": "161630d699024acc651e4f5313857aa8",
"id": null,
"metadata": {},
"name": "aliasSameAsName_TestQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mutation appendEdgeOnInterfaceMutation(
]
},
"params": {
"cacheID": "d204e2183324fbc33e98e848f63cdb5f",
"id": null,
"metadata": {},
"name": "appendEdgeOnInterfaceMutation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mutation appendEdge_CommentCreateMutation(
]
},
"params": {
"cacheID": "9c204231949c489f550d47403e373798",
"id": null,
"metadata": {},
"name": "appendEdge_CommentCreateMutation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ mutation appendNodeLiteralEdgeTypeNameCommentCreateMutation(
]
},
"params": {
"cacheID": "d2d9e98d8b7fd733fbf453da2a57c3f6",
"id": null,
"metadata": {},
"name": "appendNodeLiteralEdgeTypeNameCommentCreateMutation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ mutation appendNodeCommentCreateMutation(
]
},
"params": {
"cacheID": "7a4f84769f34b7daed587a41f27698cc",
"id": null,
"metadata": {},
"name": "appendNodeCommentCreateMutation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fragment autoFilledArgumentOnDeferDeferredFragment on User {
]
},
"params": {
"cacheID": "355bb3a3f3326a3962f689e2169b329c",
"id": null,
"metadata": {},
"name": "autoFilledArgumentOnDeferQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ extend type Mutation {
]
},
"params": {
"cacheID": "cdf5b479621acd117378029eb0cea266",
"id": null,
"metadata": {},
"name": "clientFieldsOnRoots_FooMutation",
Expand Down Expand Up @@ -270,6 +271,7 @@ mutation clientFieldsOnRoots_FooMutation(
]
},
"params": {
"cacheID": "0944177ad57ab8d9b6abd67a33085d46",
"id": null,
"metadata": {},
"name": "clientFieldsOnRoots_FooQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type ClientType {
]
},
"params": {
"cacheID": "bd2cc350c06e3c2acf412f1bee7f1f6d",
"id": null,
"metadata": {},
"name": "clientFragmentSpreadsInQuery_FooQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ interface ClientNamed {
]
},
"params": {
"cacheID": "0fdcace16a82aaf469e14bd8f7d6e5bd",
"id": null,
"metadata": {},
"name": "clientInlineFragmentsInQuery_FooQuery",
Expand Down
Loading

0 comments on commit 1244359

Please sign in to comment.