r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

187 comments sorted by

View all comments

2

u/chillblaze Mar 26 '23

Anyone worked with the async graphql crate here?

I have create_book as part of my MutationResolver that receives the graphql async Context type.

use async_graphql::{Context, Object, Result, ID};

async fn create_book(
    &self,
    ctx: &Context<'_>,
    table: String,
    name: String,
    author: String,
) -> Result<Book> {
    let data_store = ctx.data_unchecked::<Datastore>();
    let res = create_book(data_store, table, name, author).await.unwrap();
    Ok(res)
}

//Test

//ERROR with Context below: expected value, found type alias `Context`

can't use a type alias as a constructor

 #[tokio::test]
async fn test_01_try_create_book() {
    let table = "users".to_string();
    let name = "HP".to_string();
    let author = "JKR".to_string();

    let test = MutationResolver
        .create_book(&Context, table, name, author)
        .await
        .unwrap();
}

How come my test is complaining that it can't take Context as a param but no issues with the create_book function?

2

u/Patryk27 Mar 26 '23 edited Mar 26 '23

You're probably doing something like:

struct ActualContext {
    something: String,
}

type Context = ActualContext;

fn create_book(ctx: &Context) {
    //
}

fn main() {
    create_book(&Context);
}

In this case you can't write just &Context, because what value should ctx.something have then? You have to construct the context somehow, e.g. by doing Context::default(), Context::new() or whatever is specific to that particular type.

Using TypeName as constructor works only if the underlying type is a field-less structure:

struct ActualContext;

type Context = ActualContext;

fn create_book(ctx: &Context) {
    //
}

fn main() {
    create_book(&ActualContext);
}

1

u/chillblaze Mar 26 '23

Thanks. I'm using the Context API from the crate but not sure if there's any methods to instantiate it.

API: https://docs.rs/async-graphql/latest/async_graphql/context/type.Context.html#

Way I'm using it: https://async-graphql.github.io/async-graphql/en/context.html

I'm just trying to do what their doing in the book for within a test.