Skip to content

Commit

Permalink
[FOUN-575] persisted queries with client names
Browse files Browse the repository at this point in the history
This depends on unreleased functionality of GraphOS.
  • Loading branch information
glasser committed Nov 14, 2024
1 parent d508196 commit 66438eb
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/rover-client/.schema/hash.id

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 132 additions & 5 deletions crates/rover-client/.schema/schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ pub struct ApolloPersistedQueryManifest {
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Ord, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct PersistedQueryOperation {
pub name: String,
pub r#type: PersistedQueryOperationType,
pub body: String,
pub id: String,
pub client_name: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Ord, PartialOrd)]
Expand Down Expand Up @@ -96,6 +98,7 @@ impl From<PersistedQueriesPublishInput> for QueryVariables {
name: operation.name,
body: operation.body,
id: operation.id,
client_name: operation.client_name,
type_: match operation.r#type {
PersistedQueryOperationType::Mutation => {
RoverClientOperationType::MUTATION
Expand Down Expand Up @@ -183,6 +186,10 @@ impl TryFrom<RelayPersistedQueryManifest> for ApolloPersistedQueryManifest {
r#type: operation_type,
body: body.to_string(),
id: id.to_string(),
// Relay format has no way to include client names in
// the manifest file; you can still use the
// `--for-client-name` flag.
client_name: None,
});
} else {
// `apollo-parser` may sometimes be able to detect an operation name
Expand Down Expand Up @@ -324,7 +331,8 @@ mod tests {
name: "NewsfeedQuery".to_string(),
r#type: PersistedQueryOperationType::Query,
id: id.to_string(),
body: body.to_string()
body: body.to_string(),
client_name: None,
}
);
}
Expand All @@ -344,7 +352,8 @@ mod tests {
name: "NewsfeedQuery".to_string(),
r#type: PersistedQueryOperationType::Query,
id: id.to_string(),
body: body.to_string()
body: body.to_string(),
client_name: None,
}
);
}
Expand All @@ -370,6 +379,7 @@ mod tests {
name: "NamedMutation".to_string(),
r#type: PersistedQueryOperationType::Mutation,
id: id_two.to_string(),
client_name: None,
body: body_two.to_string()
}
);
Expand All @@ -380,6 +390,7 @@ mod tests {
name: "NewsfeedQuery".to_string(),
r#type: PersistedQueryOperationType::Query,
id: id_one.to_string(),
client_name: None,
body: body_one.to_string()
}
);
Expand Down
15 changes: 14 additions & 1 deletion src/command/persisted_queries/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub struct Publish {
#[arg(long, value_enum, default_value_t = PersistedQueriesManifestFormat::Apollo)]
manifest_format: PersistedQueriesManifestFormat,

/// If provided, overrides the `clientName` field in all operations in
/// the manifest file.
#[arg(long)]
for_client_name: Option<String>,

#[clap(flatten)]
profile: ProfileOpt,
}
Expand All @@ -57,7 +62,7 @@ impl Publish {
format!("JSON in {manifest} did not match '--manifest-format {format}'")
};

let operation_manifest = match self.manifest_format {
let mut operation_manifest = match self.manifest_format {
PersistedQueriesManifestFormat::Apollo => {
serde_json::from_str::<ApolloPersistedQueryManifest>(&raw_manifest)
.with_context(|| invalid_json_err(&self.manifest, "apollo"))?
Expand All @@ -69,6 +74,14 @@ impl Publish {
}
};

// Override any client names provided in the manifest (which is the only way to
// provide client names for the Relay format).
if let Some(for_client_name) = &self.for_client_name {
for op in &mut operation_manifest.operations {
op.client_name = Some(for_client_name.to_string());
}
}

let (graph_id, list_id, list_name) = match (&self.graph.graph_ref, &self.graph_id, &self.list_id) {
(Some(graph_ref), None, None) => {
let persisted_query_list = resolve::run(ResolvePersistedQueryListInput { graph_ref: graph_ref.clone() }, &client).await?;
Expand Down

0 comments on commit 66438eb

Please sign in to comment.