Skip to content

Commit

Permalink
Add a subcommand to dump definitions to file
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D49826503

fbshipit-source-id: 0d9da2d4e3b0f9a2c600daf0b94b0e8013131631
  • Loading branch information
Georgii Satosov authored and facebook-github-bot committed Oct 3, 2023
1 parent 55db0b0 commit 1ae14fe
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/crates/dependency-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ graphql-syntax = { path = "../graphql-syntax" }
relay-transforms = { path = "../relay-transforms" }
rustc-hash = "1.1.0"
schema = { path = "../schema" }
serde = { version = "1.0.185", features = ["derive", "rc"] }

[dev-dependencies]
common = { path = "../common" }
Expand Down
2 changes: 2 additions & 0 deletions compiler/crates/dependency-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

mod ast;
mod ir;
mod minimized_executable;

pub use ast::get_definition_references;
pub use ast::get_reachable_ast;
Expand All @@ -20,3 +21,4 @@ pub use ir::get_reachable_ir;
pub use ir::ExecutableDefinitionNameMap;
pub use ir::ExecutableDefinitionNameSet;
pub use ir::ExecutableDefinitionNameVec;
pub use minimized_executable::MinExecutableDefinition;
168 changes: 168 additions & 0 deletions compiler/crates/dependency-analyzer/src/minimized_executable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

use graphql_syntax::*;
use serde::Serialize;

#[derive(Serialize)]
pub enum MinExecutableDefinition {
Operation(MinOperationDefinition),
Fragment(MinFragmentDefinition),
}

impl MinExecutableDefinition {
pub fn from_executable_definition(def: ExecutableDefinition) -> Self {
match def {
ExecutableDefinition::Operation(operation) => MinExecutableDefinition::Operation(
MinOperationDefinition::from_operation_definition(operation),
),
ExecutableDefinition::Fragment(fragment) => MinExecutableDefinition::Fragment(
MinFragmentDefinition::from_fragment_definition(fragment),
),
}
}
}

#[derive(Serialize)]
pub struct MinOperationDefinition {
pub operation: String,
pub name: Option<String>,
pub selections: Vec<MinSelection>,
}

impl MinOperationDefinition {
fn from_operation_definition(op: OperationDefinition) -> Self {
MinOperationDefinition {
operation: op.operation_kind().to_string(),
name: op.name.as_ref().map(|n| n.value.to_string()),
selections: op
.selections
.items
.iter()
.map(MinSelection::from_selection)
.collect::<Vec<_>>(),
}
}
}

#[derive(Serialize)]
pub struct MinFragmentDefinition {
pub name: String,
pub type_: String,
pub selections: Vec<MinSelection>,
}

impl MinFragmentDefinition {
fn from_fragment_definition(fragment: FragmentDefinition) -> Self {
MinFragmentDefinition {
name: fragment.name.value.to_string(),
type_: fragment.type_condition.type_.value.to_string(),
selections: fragment
.selections
.items
.iter()
.map(MinSelection::from_selection)
.collect::<Vec<_>>(),
}
}
}

#[derive(Serialize)]
pub struct MinInlineFragment {
pub type_: Option<String>,
pub selections: Vec<MinSelection>,
}

impl MinInlineFragment {
fn from_inline_fragment(fragment: &InlineFragment) -> Self {
MinInlineFragment {
type_: fragment
.clone()
.type_condition
.map(|t| t.type_.value.to_string()),
selections: fragment
.selections
.items
.iter()
.map(MinSelection::from_selection)
.collect::<Vec<_>>(),
}
}
}

#[derive(Serialize)]
pub struct MinLinkedField {
pub name: String,
pub selections: Vec<MinSelection>,
}

impl MinLinkedField {
fn from_linked_field(field: &LinkedField) -> Self {
MinLinkedField {
name: field.name.value.to_string(),
selections: field
.selections
.items
.iter()
.map(MinSelection::from_selection)
.collect::<Vec<_>>(),
}
}
}

#[derive(Serialize)]
pub enum MinSelection {
FragmentSpread(MinFragmentSpread),
InlineFragment(MinInlineFragment),
LinkedField(MinLinkedField),
ScalarField(MinScalarField),
}

impl MinSelection {
fn from_selection(selection: &Selection) -> Self {
match selection {
Selection::FragmentSpread(fragment_spread) => MinSelection::FragmentSpread(
MinFragmentSpread::from_fragment_spread(fragment_spread),
),
Selection::InlineFragment(inline_fragment) => MinSelection::InlineFragment(
MinInlineFragment::from_inline_fragment(inline_fragment),
),
Selection::LinkedField(linked_field) => {
MinSelection::LinkedField(MinLinkedField::from_linked_field(linked_field))
}
Selection::ScalarField(scalar_field) => {
MinSelection::ScalarField(MinScalarField::from_scalar_field(scalar_field))
}
}
}
}

#[derive(Serialize)]
pub struct MinScalarField {
pub name: String,
}

impl MinScalarField {
fn from_scalar_field(field: &ScalarField) -> Self {
MinScalarField {
name: field.name.value.to_string(),
}
}
}

#[derive(Serialize)]
pub struct MinFragmentSpread {
pub name: String,
}

impl MinFragmentSpread {
fn from_fragment_spread(fragment_spread: &FragmentSpread) -> Self {
MinFragmentSpread {
name: fragment_spread.name.value.to_string(),
}
}
}

0 comments on commit 1ae14fe

Please sign in to comment.