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 SDL face**, read back out of the tokens: D-1's `interface` and
//! `implements`, and D-4's `__typename`.

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

/// The face alone, tight — the block is a MACRO invocation, whose body no formatter reaches.
fn emitted(model: &crate::interface::InterfaceModel) -> String {
  tight(model.sdl_face())
}

/// The fixture, stored in one database so the whole wire surface is written.
fn stored() -> crate::interface::InterfaceModel {
  let mut model = media();
  model.persist = vec![InterfaceDatabase {
    kind: DatabaseKind::Sqlite,
    aliases: AliasOptions::default(),
  }];
  model
}

/// **D-1: THE FACE IS A GraphQL `interface`**, and the type it is written on is the sum a page's
/// rows hydrate into — so there is no second output type for a client to learn.
#[test]
fn the_output_sum_is_published_as_an_interface() {
  let model = stored();
  let emitted = emitted(&model);
  let name = default_output_name(&model.name);

  assert!(
    emitted.contains(&format!(
      "impl::ingraph::__private::async_graphql::InterfaceTypefor{name}"
    )),
    "{emitted}"
  );
  assert!(
    emitted.contains("registry::MetaTypeId::Interface"),
    "{emitted}"
  );
  assert!(emitted.contains("MetaType::Interface"), "{emitted}");
  assert!(
    emitted.contains("Cow::Borrowed(\"Media\")"),
    "the published name is the interface's own:\n{emitted}"
  );
}

/// **AND THE MEMBERS GAIN `implements`** — which async-graphql builds from the REGISTRY's table
/// rather than from anything on the object's own side, so registering it here is the whole clause.
#[test]
fn every_member_is_told_it_implements_the_interface() {
  let emitted = emitted(&stored());

  assert_eq!(
    emitted.matches("Registry::add_implements").count(),
    2,
    "one per member:\n{emitted}"
  );
  assert_eq!(
    emitted.matches("IndexSet::insert").count(),
    2,
    "and one possible type per member:\n{emitted}"
  );
  assert!(
    emitted.contains("<Videoas::ingraph::__private::transform::Transformer>::Graphql"),
    "the member's OUTPUT is reached as a family projection, never as a name this crate \
     builds:\n{emitted}"
  );
}

/// **D-4: `__typename` IS THE PROVENANCE**, pinned per arm by delegating to the member's own answer
/// — so a member renamed through `output(rename = "…")` moves it and this face follows.
#[test]
fn the_typename_is_delegated_per_arm() {
  let emitted = emitted(&stored());

  assert!(emitted.contains("fnintrospection_type_name"), "{emitted}");
  assert_eq!(
    emitted
      .matches("OutputType::introspection_type_name(__ingraph_member)")
      .count(),
    2,
    "one arm per member, each answering with that member's own published name:\n{emitted}"
  );
}

/// **AND NO `kind` FIELD IS MINTED BESIDE IT** — a duplicate of `__typename` is schema bloat that
/// tooling cannot see, which is what the ruling refused.
#[test]
fn no_discriminant_field_is_published() {
  let emitted = emitted(&stored());

  assert!(!emitted.contains("String::from(\"kind\")"), "{emitted}");
}

/// **THE FIELDS ARE THE STORED CONTRACT**, at the type the MEMBERS publish them at — the same
/// crossing their own output faces return, so the interface declares what its members answer.
#[test]
fn the_published_fields_are_the_stored_contract() {
  let emitted = emitted(&stored());

  assert!(emitted.contains("String::from(\"title\")"), "{emitted}");
  assert!(emitted.contains("String::from(\"mask\")"), "{emitted}");
  assert!(
    emitted.contains("ToGraphqlOutput>::Output<'static>"),
    "the field's TYPE is the member's own crossing:\n{emitted}"
  );
  assert!(
    !emitted.contains("String::from(\"displayLabel\")"),
    "the COMPUTED tier is not on a member's object, so an interface declaring it would promise an \
     `implements` no member satisfies:\n{emitted}"
  );
}

/// **RESOLUTION DELEGATES TO THE MEMBER**, which is what `ContainerType` documents an interface to
/// do — and what makes an inline fragment on a member reach that member's PRIVATE fields.
#[test]
fn the_container_delegates_to_the_member_that_answered() {
  let emitted = emitted(&stored());

  assert_eq!(
    emitted
      .matches("ContainerType::collect_all_fields(__ingraph_member")
      .count(),
    2,
    "{emitted}"
  );
  assert_eq!(
    emitted
      .matches("ContainerType::resolve_field(__ingraph_member")
      .count(),
    2,
    "{emitted}"
  );
}

/// The face rides the BINDING GATE, and an interface with no stored column has none at all: a
/// GraphQL interface with no field is not a type.
#[test]
fn the_face_is_gated_and_conditional() {
  assert!(
    emitted(&stored()).contains("::ingraph::__private::gate::if_async_graphql!"),
    "the face names async-graphql at every line",
  );

  let mut bare = media();
  bare.persist = Vec::new();
  for field in &mut bare.fields {
    field.pins = Vec::new();
  }
  assert!(emitted(&bare).is_empty());
}