Skip to main content

Module integrations

Module integrations 

Source
Expand description

What a Filters mirror’s generated code integrates with (serialization, API documentation, validation, …), each opted into per struct with a flag of #[filters(...)] named after it.

An integration only adds to the mirror’s generated types: derives and attributes, field attributes, extra items, and the sort direction and page types it needs. The mirror’s code generation names no integration, and no integration relies on another’s attributes: each states the wire format itself (e.g. snake case sort variants).

The integrations a mirror may opt into are the conventions’ (crate::conventions::Conventions::integrations): the built-in ones (BUILT_IN: serde, utoipa, validator, schemars, garde) by default, to which a proc-macro crate adds its own, or which it replaces (e.g. by another serializer).

§Examples

An integration deriving a GraphQL input type, with its own sort direction (named by path from the using crate, e.g. a type deriving the GraphQL enum and converting both ways with queryweave::SortDirectionModel):

use queryweave_codegen::integrations::{Context, Integration, TypeAdditions};
use quote::quote;

struct Graphql;

impl Integration for Graphql {
    fn name(&self) -> &'static str {
        "graphql"
    }

    fn sort_direction(&self, _context: &Context<'_>) -> Option<proc_macro2::TokenStream> {
        Some(quote!(crate::graphql::SortDirection))
    }

    fn sort_field(&self, _context: &Context<'_>) -> TypeAdditions {
        TypeAdditions {
            derives: vec![quote!(async_graphql::Enum)],
            attributes: quote!(#[graphql(rename_items = "snake_case")]),
        }
    }
}

// Registered next to the built-in ones, in a crate's conventions:
let integrations: Vec<&'static dyn Integration> = queryweave_codegen::integrations::BUILT_IN
    .into_iter()
    .chain([&Graphql as &'static dyn Integration])
    .collect();
assert_eq!(integrations.len(), queryweave_codegen::integrations::BUILT_IN.len() + 1);

Structs§

Context
What an integration knows of the mirror it adds to.
NonNullableContext
What an integration knows of the merge patch DTO deriving ValidateNonNullable it adds to.
TypeAdditions
What an integration adds to one generated type.

Constants§

BUILT_IN
The built-in integrations, the default of crate::conventions::Conventions::integrations.

Traits§

Integration
An integration of a mirror’s generated code with another crate.