Async-graphql: Make connection types more flexible?

Created on 25 May 2020  路  3Comments  路  Source: async-graphql/async-graphql

Background:

I am refactoring my code to use this crate with the elasticsearch crate.

Currently, I am using juniper and since they do not support the relay cursor connection spec, I have defined code to translate an elasticsearch search response to a graphql connection object, but with extra elasticsearch information:

#[derive(Deserialize, Debug)]
struct Hits<T> {
    #[serde(default)]
    page_size: usize,

    #[serde(default)]
    took: Duration,

    #[serde(default)]
    timed_out: bool,

    #[serde(default, rename = "total")]
    total_count: Count,

    #[serde(default)]
    max_score: Option<f32>,

    #[serde(default = "Vec::new", rename = "hits")]
    edges: Vec<Hit<T>>,
}
#[derive(Deserialize, Debug)]
struct Hit<T> {
    #[serde(default)]
    sort: InnerCursor,

    #[serde(rename = "_version")]
    version: Option<u64>,

    #[serde(rename = "_seq_no")]
    sequence_number: Option<u64>,

    #[serde(rename = "_primary_term")]
    primary_term: Option<u64>,

    #[serde(rename = "_score")]
    score: Option<f32>,

    #[serde(default)]
    highlight: HashMap<String, Vec<String>>,

    #[serde(rename = "_id")]
    id: String,

    #[serde(rename = "_index")]
    index: String,

    #[serde(rename = "_source")]
    node: T,
}

And then I use a proc-macro (not ideal) to use #[async_graphql::Object] and convert a Hits to a *Connection and a Hit to an *Edge, using the sort field from elasticsearch to create a cursor etc.

My questions are:

  • Would it be possible to update the built in Connection object type to add custom fields like highlight, sequence_number etc? Maybe via a trait (maybe more ideal?) or add a generic field extra_info or something (less ideal?)?
  • If not, is there a way to get around the fact that graphql types ending in Connection and Edge are reserved so that I can keep my current code without having to rename them?

Most helpful comment

We fully support the features you mentioned, but unfortunately, we are modifying the API of Relay Connection, because the old version is not easy to use in many places. The new API will be released in a few hours, we need to update the document. Then I will show you how to add additional fields to Edge. 馃榿

All 3 comments

We fully support the features you mentioned, but unfortunately, we are modifying the API of Relay Connection, because the old version is not easy to use in many places. The new API will be released in a few hours, we need to update the document. Then I will show you how to add additional fields to Edge. 馃榿

Adding additional fields to Connection is not yet supported, and I am adding this feature.

I released version 1.14.0, and both Connection and Edge were able to extend the fields, which depended on the new DataSource implementation, so this is a breaking change again. 馃槀

Was this page helpful?
0 / 5 - 0 ratings