Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to make the response of a query generic? #708

Open
cowlicks opened this issue Jun 9, 2023 · 1 comment · May be fixed by #893
Open

Is it possible to make the response of a query generic? #708

cowlicks opened this issue Jun 9, 2023 · 1 comment · May be fixed by #893

Comments

@cowlicks
Copy link

cowlicks commented Jun 9, 2023

I would like to be able to define some query structs, and let the end user provide the structs for the response. My first attempt didn't work, and I got weird errors. Is there a way to do this?

Here the generic T parameter was previously ScanLogsConnection

#[derive(cynic::QueryVariables, Debug)]
pub struct LastTagAtScannerVariables {
    pub scanner_id: super::scalars::Uuid,
    pub tagged_type: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "LastTagAtScannerVariables")]
pub struct LastTagAtScanner<T> {
    #[arguments(orderBy: "_CREATED_AT_DESC", condition: { hasTaggedType: $tagged_type, scannerId: $scanner_id }, first: 1)]
    pub scan_logs: Option<T>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct ScanLogsConnection {
    pub edges: Vec<ScanLogsEdge>,
}

But I could not figure out how to actually tell LastTagAtScanner about what I want T to be when I build it.

use foo::cynic_queries::{
    last_tag_at_scanner::{
        LastTagAtScanner, LastTagAtScannerVariables, ScanLogsConnection,
    },
    scalars::Uuid,
    QueryBuilder,
};
let mut client = Client::try_from_env()?;
let operation = LastTagAtScanner::build(LastTagAtScannerVariables {
    scanner_id: Uuid("0000d754-0000-0000-0000-0000646417b5".to_string()),
    tagged_type: "Thing".to_string(),
});
let x = client.cynic_query(operation).await?;

This is the error I get. The missing generic argument ... <T><T> is strange.

error[E0412]: cannot find type `T` in this scope
   --> src/cynic_queries.rs:844:31
    |
844 |         pub scan_logs: Option<T>,
    |                               ^ not found in this scope

error[E0107]: missing generics for struct `LastTagAtScanner`
   --> src/cynic_queries.rs:842:16
    |
842 |     pub struct LastTagAtScanner<T> {
    |                ^^^^^^^^^^^^^^^^ expected 1 generic argument
    |
note: struct defined here, with 1 generic parameter: `T`
   --> src/cynic_queries.rs:842:16
    |
842 |     pub struct LastTagAtScanner<T> {
    |                ^^^^^^^^^^^^^^^^ -
help: add missing generic argument
    |
842 |     pub struct LastTagAtScanner<T><T> {
    |                ~~~~~~~~~~~~~~~~~~~

Some errors have detailed explanations: E0107, E0412.
@obmarg
Copy link
Owner

obmarg commented Jun 9, 2023

Hi @cowlicks - I think this should be possible yeah.

Those errors are a bit odd - are you on v3 yet? Generic support was only added in v3, but those errors look kind of like what I'd expect from v2.

You'll also need to put some generic bounds in which you don't have there:

pub struct LastTagAtScanner<T> 
    where 
        T: cynic::QueryFragment<SchemaType = schema::ScanLogsConnection>,
        LastTagAtScannerVariablesFields: cynic::queries::VariableMatch<T::VariablesFields>,
{
   ...
}

But I could not figure out how to actually tell LastTagAtScanner about what I want T to be when I build it.

You'll want to use a turbofish to do that

LastTagAtScanner::<ScanLogsConnection>::build::(...)

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 a pull request may close this issue.

2 participants