ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **B-27 — the interface's ROOT CITIZENSHIP**, read back out of the tokens: the unit, the seat it
//! is bound at, and the two conditions each is written under.

use crate::{
  interface::{
    default_filter_name, default_input_name, default_order_by_name, default_output_name,
    tests::{media, rendered},
  },
  model::{DatabaseKind, default_queries_name},
};

/// The fixture with its `persist(…)` selection written — a projection's databases being the block
/// JOINED with every database a field pinned a type at, which is why the pins are cleared first: a
/// selection this function did not name would otherwise arrive through `media()`'s own pins.
fn stored(kinds: &[DatabaseKind]) -> crate::interface::InterfaceModel {
  let mut model = unstored();
  model.persist = kinds
    .iter()
    .map(|kind| crate::interface::InterfaceDatabase {
      kind: *kind,
      aliases: crate::model::AliasOptions::default(),
    })
    .collect();
  model
}

/// The fixture about NO database at all: no selection block, and no field pinning a type anywhere.
fn unstored() -> crate::interface::InterfaceModel {
  let mut model = media();
  model.persist = Vec::new();
  for field in &mut model.fields {
    field.pins = Vec::new();
  }
  model
}

/// **THE UNIT IS THE RECORD's SHAPE**, which is D-3 in tokens: a `Default` unit type, named by the
/// same convention, so the root that folds it needs no second mechanism.
#[test]
fn the_interface_emits_a_query_unit_of_the_records_own_shape() {
  let model = stored(&[DatabaseKind::Sqlite]);
  let emitted = rendered(&model);
  let name = default_queries_name(&model.name);

  assert!(
    emitted.contains(&format!("pub struct {name};")),
    "{emitted}"
  );
  assert!(emitted.contains("Default"), "{emitted}");
  assert_eq!(name.to_string(), "MediaQueries");
}

/// **THE SEAT IS `Graphql::Query`**, bound on the INTERFACE itself — one projection where a record's
/// is two, because `Transformer: Identifiable` and an interface's id is a sum no member's value
/// holds one of.
#[test]
fn the_binding_seats_the_unit_at_graphql_query() {
  let model = stored(&[DatabaseKind::Sqlite]);
  let emitted = rendered(&model);

  assert!(
    emitted.contains("impl ::ingraph::__private::transform::Graphql for Media"),
    "{emitted}"
  );
  assert!(emitted.contains("type Query = MediaQueries;"), "{emitted}");
  assert!(
    !emitted.contains("transform::Transformer for Media"),
    "an interface implements no `Transformer`:\n{emitted}"
  );
  assert!(
    !emitted.contains("transform::Identifiable for Media"),
    "nor the supertrait whose `id_ref` it could not write:\n{emitted}"
  );
}

/// **THE TWO FACE HALVES ARE THE EMITTED ONES**, so the filter and the order a client writes are
/// reached through the same projections a record's are.
#[test]
fn the_face_halves_name_the_argument_object_and_the_output_sum() {
  let model = stored(&[DatabaseKind::Sqlite]);
  let emitted = rendered(&model);
  let (input, output) = (
    default_input_name(&model.name),
    default_output_name(&model.name),
  );

  assert!(
    emitted.contains(&format!(
      "impl ::ingraph::__private::transform::GraphqlInput for {input}"
    )),
    "{emitted}"
  );
  assert!(
    emitted.contains(&format!(
      "type Filter = {};",
      default_filter_name(&model.name)
    )),
    "{emitted}"
  );
  assert!(
    emitted.contains(&format!(
      "type OrderBy = {};",
      default_order_by_name(&model.name)
    )),
    "{emitted}"
  );
  assert!(
    emitted.contains(&format!(
      "impl ::ingraph::__private::transform::GraphqlOutput for {output}"
    )),
    "{emitted}"
  );
  assert!(
    emitted.contains("type Key = ();"),
    "the wire ADDRESS is the fusion seam's, and the empty product is the framework's own absent \
     form:\n{emitted}"
  );
  assert!(
    emitted.contains(&format!("type Input = {input};")),
    "{emitted}"
  );
  assert!(
    emitted.contains(&format!("type Output = {output};")),
    "{emitted}"
  );
}

/// **AN INTERFACE THAT PROJECTS NOTHING BINDS NOTHING**, and a graph that mounts it is `E0277` at
/// the projection carrying `Graphql`'s own sentence — the same answer a record with no wire face
/// gives.
#[test]
fn an_interface_about_no_sql_database_has_no_binding_and_no_unit() {
  let emitted = rendered(&unstored());

  assert!(
    !emitted.contains("transform::Graphql for Media"),
    "{emitted}"
  );
  assert!(!emitted.contains("struct MediaQueries"), "{emitted}");
}

/// **THE ROOT FIELDS ARE SERVED FROM ONE DATABASE** — the first the declaration named, so a client
/// never reads a field that varies with a deployment's storage choice.
#[test]
fn the_root_fields_are_served_from_the_first_declared_database() {
  let model = stored(&[DatabaseKind::Postgres, DatabaseKind::Sqlite]);

  assert_eq!(
    model.wire_query_database(),
    Some(DatabaseKind::Sqlite),
    "`databases()` walks `DatabaseKind::ALL`, so 'first' is that order and not the block's",
  );
}

/// The unit joins the COLLISION LIST exactly where it is written, so a declaring crate that owns a
/// `<I>Queries` of its own is refused — and one whose interface has no page is not refused for a
/// clash that cannot happen.
#[test]
fn the_unit_is_declared_into_the_scope_only_where_it_is_written() {
  let named = |model: &crate::interface::InterfaceModel| {
    model
      .emitted_types()
      .into_iter()
      .any(|declared| declared.name == "MediaQueries")
  };

  assert!(named(&stored(&[DatabaseKind::Sqlite])));
  assert!(!named(&unstored()));
}