Skip to content

Conversation

@ardatan
Copy link
Member

@ardatan ardatan commented Nov 24, 2025

How to attach configuration to a plugin

Had to introduce a seperate trait because of some dyn compatibility issues etc called RouterPluginWithConfig so next to RouterPlugin, plugins need to implement it like;

#[derive(Deserialize)]
pub struct AllowClientIdConfig {
    pub header: String,
    pub path: String,
}

impl RouterPluginWithConfig for AllowClientIdFromFilePlugin  {
    type Config = AllowClientIdConfig;
    fn plugin_name() -> &'static str {
        "allow_client_id_from_file"
    }
    fn new(config: AllowClientIdConfig) -> Self {
        AllowClientIdFromFilePlugin  {
            header_key: config.header,
            allowed_ids_path: PathBuf::from(config.path),
        }
    }
}

pub struct AllowClientIdFromFilePlugin {
    header_key: String,
    allowed_ids_path: PathBuf,
}

#[async_trait::async_trait]
impl RouterPlugin for AllowClientIdFromFilePlugin {

How to register a plugin

After registering the plugin to the registry like the following;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut plugin_factories = PluginRegistry::new();
    plugin_factories.register::<AllowClientIdFromFilePlugin>();

    match router_entrypoint(plugin_factories).await {
        Ok(_) => Ok(()),
        Err(err) => {
            eprintln!("Failed to start Hive Router:\n  {}", err);

            Err(err)
        }
    }
}

It automatically takes the configuration from the router config and constructs a plugin instance;

allow_client_id_from_file:
  header: 'x-client'
  allowed_ids_path: './some-file'

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

k6-benchmark results

     ✓ response code was 200
     ✓ no graphql errors
     ✓ valid response structure

     █ setup

     checks.........................: 100.00% ✓ 214146      ✗ 0    
     data_received..................: 6.3 GB  208 MB/s
     data_sent......................: 84 MB   2.8 MB/s
     http_req_blocked...............: avg=3.2µs    min=681ns   med=1.71µs  max=12.01ms  p(90)=2.37µs  p(95)=2.7µs   
     http_req_connecting............: avg=529ns    min=0s      med=0s      max=1.81ms   p(90)=0s      p(95)=0s      
     http_req_duration..............: avg=20.57ms  min=2.16ms  med=19.59ms max=184.99ms p(90)=27.98ms p(95)=31.1ms  
       { expected_response:true }...: avg=20.57ms  min=2.16ms  med=19.59ms max=184.99ms p(90)=27.98ms p(95)=31.1ms  
     http_req_failed................: 0.00%   ✓ 0           ✗ 71402
     http_req_receiving.............: avg=159.81µs min=23.67µs med=38.25µs max=107.78ms p(90)=90.3µs  p(95)=400.39µs
     http_req_sending...............: avg=24.48µs  min=5.02µs  med=10.47µs max=25.49ms  p(90)=15.17µs p(95)=27.45µs 
     http_req_tls_handshaking.......: avg=0s       min=0s      med=0s      max=0s       p(90)=0s      p(95)=0s      
     http_req_waiting...............: avg=20.39ms  min=2.1ms   med=19.46ms max=97.06ms  p(90)=27.72ms p(95)=30.74ms 
     http_reqs......................: 71402   2374.654085/s
     iteration_duration.............: avg=21.01ms  min=6.06ms  med=19.94ms max=254.14ms p(90)=28.43ms p(95)=31.6ms  
     iterations.....................: 71382   2373.988935/s
     vus............................: 50      min=50        max=50 
     vus_max........................: 50      min=50        max=50 

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

🐋 This PR was built and pushed to the following Docker images:

Image Names: ghcr.io/graphql-hive/router

Platforms: linux/amd64,linux/arm64

Image Tags: ghcr.io/graphql-hive/router:pr-584 ghcr.io/graphql-hive/router:sha-caef936

Docker metadata
{
"buildx.build.ref": "builder-65853d3b-5f35-465f-81a4-aac6f58149ca/builder-65853d3b-5f35-465f-81a4-aac6f58149ca0/7m1ad8nxofu7lp6a0jp3hgzse",
"containerimage.descriptor": {
  "mediaType": "application/vnd.oci.image.index.v1+json",
  "digest": "sha256:b2f4678b08461a14bc24c07a9b71033867c0f2cfe85121feebfd26e7fc2ce478",
  "size": 1609
},
"containerimage.digest": "sha256:b2f4678b08461a14bc24c07a9b71033867c0f2cfe85121feebfd26e7fc2ce478",
"image.name": "ghcr.io/graphql-hive/router:pr-584,ghcr.io/graphql-hive/router:sha-caef936"
}

@ardatan ardatan marked this pull request as ready for review December 2, 2025 11:12
@ardatan ardatan merged commit d2a38fd into response-cache-plugin-example Dec 2, 2025
17 of 19 checks passed
@ardatan ardatan deleted the plugin-system-config branch December 2, 2025 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant