ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **B-27 D-6 — the NAMING TOWER over an interface**, and D-3's list leg reading it.
//!
//! Beside [`super::tests`] rather than inside it, which is this workspace's shape for an axis that
//! arrives with its own grammar: the roster a reader walks stays the one it was.

use crate::{
  interface::{
    InterfaceDatabase, InterfaceModel,
    tests::{media, tight},
  },
  model::{AliasOptions, DatabaseKind},
};

/// The fixture, stored in one database so the leg and both page names are written.
fn stored() -> InterfaceModel {
  let mut model = media();
  model.persist = vec![InterfaceDatabase {
    kind: DatabaseKind::Sqlite,
    aliases: AliasOptions::default(),
  }];
  model
}

/// This module's own emission — the unit, its binding and its face — tight, the face being a nested
/// MACRO invocation whose body no formatter reaches.
///
/// Scoped to the three items rather than the whole expansion, and the ABSENCE claims are why: an
/// interface's key sum documents itself against the entity binding's `Lineage` member, so a claim
/// that this LEG has no ancestry has to be asked of the leg.
fn emitted(model: &InterfaceModel) -> String {
  tight(model.queries())
}

/// **THE BASE IS THE PLURALISER's**, and `Media` is the ruling's own worked example: the answer
/// equals the singular, which a RECORD is refused for and an interface is not.
#[test]
fn the_derived_plural_is_the_base_and_a_degenerate_one_is_legal() {
  let model = stored();

  assert_eq!(model.published_plural(), "Media");

  let emitted = emitted(&model);
  assert!(emitted.contains("#[graphql(name=\"media\")]"), "{emitted}");
  assert!(emitted.contains("\"MediaConnection\""), "{emitted}");
  assert!(emitted.contains("\"MediaEdge\""), "{emitted}");
}

/// **ONE WORD MOVES THREE NAMES** — the user's *一起跟*: the list field, the connection and the edge
/// all read one resolution, so a declaration that corrects English once is not asked to again.
#[test]
fn the_declared_plural_moves_the_field_and_both_page_names() {
  let mut model = stored();
  model.plural = Some("corpora".to_owned());
  let emitted = emitted(&model);

  assert!(
    emitted.contains("#[graphql(name=\"corpora\")]"),
    "{emitted}"
  );
  assert!(emitted.contains("\"CorporaConnection\""), "{emitted}");
  assert!(emitted.contains("\"CorporaEdge\""), "{emitted}");
  assert!(!emitted.contains("\"MediaConnection\""), "{emitted}");
}

/// **THE PLURAL IS READ AT ITS INITIAL, BOTH WAYS** — `"Corpora"` and `"corpora"` reach ONE field
/// and ONE pair of page names, whichever way a declaration wrote the word.
#[test]
fn the_word_is_raised_and_lowered_rather_than_re_cased() {
  let mut model = stored();
  model.plural = Some("Corpora".to_owned());
  let emitted = emitted(&model);

  assert!(
    emitted.contains("#[graphql(name=\"corpora\")]"),
    "{emitted}"
  );
  assert!(emitted.contains("\"CorporaConnection\""), "{emitted}");
}

/// **THE TOP TIER COVERS THE MIDDLE, AND EACH NAME INDEPENDENTLY** — a declaration may move the page
/// and leave the edge on the tower's base.
#[test]
fn a_renamed_page_covers_the_plural_built_word_alone() {
  let mut model = stored();
  model.plural = Some("corpora".to_owned());
  model.connection = Some("CorpusPage".to_owned());
  let emitted = emitted(&model);

  assert!(emitted.contains("\"CorpusPage\""), "{emitted}");
  assert!(
    emitted.contains("\"CorporaEdge\""),
    "the edge stays on the tier below:\n{emitted}"
  );
  assert!(
    emitted.contains("#[graphql(name=\"corpora\")]"),
    "and so does the field:\n{emitted}"
  );
}

/// **D-3 — ONE LEG, AND THE ABSENT ONE IS A RULING.** The list is emitted; there is no by-id
/// resolver and no `id:` argument for a client to send.
#[test]
fn the_unit_carries_the_list_and_no_get_leg() {
  let emitted = emitted(&stored());

  assert!(emitted.contains("__async_graphql_media_list"), "{emitted}");
  assert!(
    !emitted.contains("#[graphql(name=\"id\")]"),
    "an interface's address is a SUM, so there is no id a client could send:\n{emitted}"
  );
  assert!(
    !emitted.contains("Lineage"),
    "and no ancestry, an interface owning no table for one to be a path through:\n{emitted}"
  );
}

/// **THE LEG's ARGUMENTS ARE THE RULED ONES, `where` LAST** — the widening law rather than a
/// preference: the SDL renders arguments in declaration order.
#[test]
fn the_legs_arguments_are_the_relay_five_with_the_predicate_last() {
  let emitted = emitted(&stored());
  let at = |word: &str| {
    emitted
      .find(word)
      .unwrap_or_else(|| panic!("{word} is written:\n{emitted}"))
  };

  assert!(at("#[graphql(name=\"orderBy\")]") < at("#[graphql(name=\"first\")]"));
  assert!(at("#[graphql(name=\"first\")]") < at("#[graphql(name=\"last\")]"));
  assert!(at("#[graphql(name=\"after\")]") < at("#[graphql(name=\"before\")]"));
  assert!(at("#[graphql(name=\"before\")]") < at("#[graphql(name=\"where\")]"));
}

/// **THE LEG RIDES BOTH GATES** — async-graphql's, because `#[Object]` is its macro, and the
/// DATABASE's, because the engine call names one dialect's row, cursor and finder.
#[test]
fn the_face_rides_the_binding_gate_and_the_databases() {
  let emitted = emitted(&stored());

  assert!(
    emitted.contains("::ingraph::__private::gate::if_async_graphql!"),
    "{emitted}"
  );
  assert!(
    emitted.contains("::ingraph::__private::gate::if_sqlite!"),
    "{emitted}"
  );
}