ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! THE SINGLE LEG'S TWO LANES, read back out of the tokens the leg writes.
//!
//! The leg rather than the whole face, for the reason `page_name_tests.rs` reads the naming
//! functions rather than the face: what this file is about is one emitter's body, and the face
//! around it is asserted where a face is — against a real schema.
//!
//! What the shape DOES is `ingraph/tests/wire_batched.rs`, which sends the documents
//! `wire_batching.rs` counted and counts them again through the same [`SqlSource`] tally: three
//! statements become one. Neither file stands in for the other. This one says the branch is written
//! and written unconditionally; that one says the branch saves the statements.
//!
//! [`SqlSource`]: ../../../ingraph/trait.SqlSource.html

use quote::ToTokens as _;

/// The single leg's tokens, with the spaces `TokenStream::to_string` puts between them removed —
/// so a type argument list can be matched as a reader would write it.
fn leg() -> String {
  let model = crate::tests::queried();
  let graphql = model.graphql.as_ref().expect("the fixture has a wire face");
  let database = model
    .queryable()
    .first()
    .copied()
    .expect("the fixture stores in a queryable backend");
  model
    .single_leg(graphql, database)
    .to_token_stream()
    .to_string()
    .replace(' ', "")
}

/// **THE TWO LANES ARE EMITTED**, and the branch is `data_opt` — the ruling's *不装loader 就慢一些呗*
/// in one token.
///
/// `data_opt` and not `data` is the whole of *absence degrades, never refuses*: a build that
/// installs no door takes the second arm, and the second arm is what every build did before this
/// round.
#[test]
fn the_single_leg_branches_on_an_optional_door() {
  let emitted = leg();

  assert!(
    emitted.contains("Context::data_opt::"),
    "the leg reads the batch door OPTIONALLY:\n{emitted}"
  );
  assert!(
    emitted.contains("graphql::BatchDoor"),
    "and by the framework's own door type, through the shelf:\n{emitted}"
  );
}

/// **THE DOOR IS KEYED ON THE ID AND THE ROW** — the two things the emission already knows, which is
/// what lets an application install one without either side spelling a word the other could get
/// wrong.
///
/// A row type is minted per record PER DATABASE, so the pair is exactly the granularity a batch has:
/// a second record's door is a second type, and no resolver can be answered out of another record's
/// batch.
#[test]
fn the_door_is_keyed_on_this_records_id_and_row() {
  let emitted = leg();

  assert!(
    emitted.contains("BatchDoor<Uuid7,VideoTrackSqlite>"),
    "the door names the declared id type and this backend's row:\n{emitted}"
  );
}

/// **THE ID TYPE IS CARRIED VERBATIM** — an alias stays an alias, as it does everywhere else in this
/// emission, so an application installing a door names the same word the declaration wrote.
#[test]
fn the_key_is_the_declarations_own_spelling() {
  let emitted = leg();

  assert!(
    emitted.contains("BatchDoor<Uuid7,"),
    "`Uuid7` rather than the `u128` it resolves to:\n{emitted}"
  );
}

/// **THE PER-ROW LANE IS UNCHANGED** — the read door, the id crossing, the gather, the first row.
///
/// A round that moved the batch in and quietly changed the slow lane would have made the
/// before-number in `wire_batching.rs` a measurement of something that no longer exists.
#[test]
fn the_absent_lane_still_reads_the_store_the_way_it_did() {
  let emitted = leg();

  for token in ["SqlDoor", "sql::rows::", "SqlDoor::pool", "slice::from_ref"] {
    assert!(
      emitted.contains(token),
      "the per-row lane still spells `{token}`:\n{emitted}"
    );
  }
}

/// **THE READ DOOR MOVED INSIDE THE ABSENT LANE** — a resolver answering out of a batch never asks
/// for a pool, so a schema that installs a batch door and no read door is a working configuration.
///
/// Read positionally: the `data_opt` lookup comes FIRST and the `data::<SqlDoor…>` lookup after it.
/// Before this round the read door was the leg's first statement, unconditionally.
#[test]
fn the_read_door_is_looked_up_after_the_batch_door() {
  let emitted = leg();
  let batch = emitted.find("data_opt::").expect("the batch lookup");
  let read = emitted.find("data::<").expect("the read door lookup");

  assert!(
    batch < read,
    "the batch door is read first, and the read door only where there is no batch:\n{emitted}"
  );
}

/// **THE ID CROSSING RIDES BOTH LANES** — written out in each arm rather than hoisted above the
/// match, which is what preserves the absent lane's refusal ORDER: a request with no read door AND
/// an unusable address still gets the door's refusal first, exactly as it did.
#[test]
fn both_lanes_cross_the_written_id() {
  let emitted = leg();

  assert_eq!(
    emitted
      .matches("ToGraphqlInput>::cross(__async_graphql_id)")
      .count(),
    2,
    "one crossing per lane, and neither hoisted above the branch:\n{emitted}"
  );
}

/// **THE BATCHED REFUSAL CROSSES THROUGH THE SAME DOOR THE PER-ROW ONE DOES** — `refused`, which
/// attaches whatever word the refusal itself names.
///
/// That is what makes the two lanes indistinguishable to a client: a store failure reaches it under
/// `STORE_REFUSED` whether or not an application installed a loader, because the neutral currency
/// carries the code rather than having one chosen for it at the wire.
#[test]
fn both_lanes_cross_their_refusals_the_same_way() {
  let emitted = leg();

  assert_eq!(
    emitted.matches("graphql::refused(").count(),
    2,
    "one per lane, through the one place a code is ever attached:\n{emitted}"
  );
  assert_eq!(
    emitted.matches("refused_address(").count(),
    2,
    "and one address refusal per lane, since each crosses its own id:\n{emitted}"
  );
}

/// **NOTHING IN THE LEG NAMES THE COALESCING ENGINE** — the ruling's *seam + install contract, not
/// an emission*.
///
/// This is what makes the `dataloader` feature independent: the door type is the framework's own and
/// exists in every build, so no expansion asks whether async-graphql's dataloader was compiled, and
/// no expansion picks a runtime for the spawner it would need.
#[test]
fn the_leg_names_no_engine_and_no_spawner() {
  let emitted = leg();

  for absent in ["DataLoader", "AsLoader", "spawn", "dataloader", "cfg("] {
    assert!(
      !emitted.contains(absent),
      "an expansion must not name `{absent}` — the engine is the application's:\n{emitted}"
    );
  }
}

/// **THE MISS IS STILL THE ENGINE'S OWN ANSWER** — both lanes hand the SAME `Option<Row>` to the one
/// `match` that raises it, so `null` for an absent id is decided in one place rather than twice.
#[test]
fn one_raise_serves_both_lanes() {
  let emitted = leg();

  assert_eq!(
    emitted.matches("let__ingraph_hit=match").count(),
    1,
    "the branch produces the hit, and the raise below it is written once:\n{emitted}"
  );
}