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

Basic example dosn't seem to work #53

Closed
somehowchris opened this issue Jun 11, 2020 · 10 comments · Fixed by #54
Closed

Basic example dosn't seem to work #53

somehowchris opened this issue Jun 11, 2020 · 10 comments · Fixed by #54

Comments

@somehowchris
Copy link

Hey man,

I'm somewhat new to rust and really appreciate your work.
I wanted to set up a new project and just copied your example code from the README but It doesn't seem to work for me. Am I doing something wrong?

error[E0433]: failed to resolve: use of undeclared type or module `futures`
 --> src/main.rs:1:5
  |
1 | use futures::stream::StreamExt;
  |     ^^^^^^^ use of undeclared type or module `futures`

error[E0433]: failed to resolve: use of undeclared type or module `async_trait`
 --> src/main.rs:8:17
  |
8 | #[derive(Debug, Model, Serialize, Deserialize)]
  |                 ^^^^^ use of undeclared type or module `async_trait`
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type or module `tokio`
  --> src/main.rs:18:3
   |
18 | #[tokio::main]
   |   ^^^^^ use of undeclared type or module `tokio`

warning: use of deprecated item 'wither::model::Model::sync': Index management is currently missing in the underlying driver, so this method no longer does anything. We are hoping to re-enable this in a future release.
  --> src/main.rs:22:3
   |
22 |   User::sync(db.clone()).await?;
   |   ^^^^^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

error[E0599]: no method named `next` found for struct `wither::cursor::ModelCursor<User>` in the current scope
   --> src/main.rs:33:33
    |
33  |   while let Some(user) = cursor.next().await {
    |                                 ^^^^ method not found in `wither::cursor::ModelCursor<User>`
    | 
   ::: /Users/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.5/src/stream/stream/mod.rs:222:8
    |
222 |     fn next(&mut self) -> Next<'_, Self>
    |        ----
    |        |
    |        the method is available for `std::boxed::Box<wither::cursor::ModelCursor<User>>` here
    |        the method is available for `std::sync::Arc<wither::cursor::ModelCursor<User>>` here
    |        the method is available for `std::rc::Rc<wither::cursor::ModelCursor<User>>` here
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
1   | use futures_util::stream::stream::StreamExt;
    |

error[E0277]: `main` has invalid return type `impl std::future::Future`
  --> src/main.rs:19:20
   |
19 | async fn main() -> Result<()> {
   |                    ^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
   |
   = help: consider using `()`, or a `Result`

error[E0752]: `main` function is not allowed to be `async`
  --> src/main.rs:19:1
   |
19 | async fn main() -> Result<()> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 6 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0433, E0599, E0752.
For more information about an error, try `rustc --explain E0277`.
@thedodd
Copy link
Owner

thedodd commented Jun 11, 2020

@somehowchris you just need to add the various dependencies described in this error log to your Cargo.toml.

Firstly, if you want to use tokio as your runtime, then you need to add tokio to your dependencies. Regardless of which runtime you use (tokio or async-std) you will also need to add futures as a dep as well. This is base line. Nothing I can do about these items on the Wither side.

However, this does bring up one item, which I had considered, but released the alpha in part to be able to solicit feedback on this issue. async_trait on the other hand is fundamentally required, and so I'm considering just adding it as an export of Wither, and then referencing it via wither::async_trait just to make it so that folks don't need to add it as a dep.

I think I'm going to add async_trait as a pub export. That will make things a lot easier. Adding wither, tokio/async-std and futures to your dependencies is definitely something you will have to do though.

@thedodd
Copy link
Owner

thedodd commented Jun 12, 2020

@somehowchris did adding those deps solve the problem?

@thedodd
Copy link
Owner

thedodd commented Jun 12, 2020

#54 should address this issue. The other items, you will have to add as deps on your own.

If you still have any problems, or need guidance on how to add those crates as dependencies to your project, just let me know.

@rumanHuq
Copy link

rumanHuq commented Jul 2, 2020

I am using async_std instead of tokyo.

// main.rs

use futures::stream::StreamExt;
use serde::{Deserialize, Serialize};
use wither::bson::{doc, oid::ObjectId};
use wither::mongodb::Client;
use wither::prelude::*;

#[derive(Debug, Model, Serialize, Deserialize)]
#[model(index(keys = r#"doc!{"email": 1}"#, options = r#"doc!{"unique": true}"#))]
struct User {
	/// The ID of the model.
	#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
	pub id: Option<ObjectId>,
	/// The user's email address.
	pub email: String,
}

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
	let db = Client::with_uri_str("mongodb://localhost:27017/").await.unwrap().database("mydb");

	let mut cursor = User::find(db.clone(), None, None).await.unwrap();

	while let Some(user) = cursor.next().await {
		println!("{:?}", user);
	}

	let mut app = tide::with_state(db);
	app.at("/").get(|_| async { Ok("Hello, world!!!!") });
	app.listen("127.0.0.1:8080").await?;
	Ok(())
}
// cargo.toml
[package]
name = "graphql-tide-server"
version = "0.1.0"
authors = ["Ruman <rumanbsl@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tide = "0.11.0"
async-std = { version = "1.6.2", features = ["attributes"] }
juniper = "0.14.2"
serde= { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.56"
wither = "0.9.0-alpha.1"
wither_derive = "0.9.0-alpha.1"
validator = "0.10"
validator_derive = "0.10"
futures = "0.3.5"

giving Error

Running `target/debug/graphql-tide-server`
thread 'main' panicked at 'there is no timer running, must be called from the context of 
Tokio runtime', /Users/<USER>/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/time/driver/handle.rs:24:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[Finished running. Exit status: 101]

@thedodd
Copy link
Owner

thedodd commented Jul 2, 2020

@rumanbsl looks like you just need to activate the async-std-runtime for wither, and ensure the default features are deactivated.

As shown in the README, tokio is the default runtime:
Screen Shot 2020-07-02 at 2 20 40 PM

As such, you should update your wither dependency declaration to simply be:

wither = { version = "0.9.0-alpha.1", default-features = false, features = ["async-std-runtime"] }

Also, the derive code is re-exported by wither, so you can actually remove the wither_derive = "0.9.0-alpha.1" from your dependencies as well.

@rumanHuq
Copy link

rumanHuq commented Jul 2, 2020

Mongo is giving some error with async-std (switching async-std to 1.5 didn't help either)

failed to select a version for `async-std`.
    ... required by package `mongodb v1.0.0`
    ... which is depended on by `wither v0.9.0-alpha.1`
    ... which is depended on by `graphql-tide-server v0.1.0 (/Users/<USER>/Documents/dev/RUST_WORKSPACE/graphql-tide-server)`
versions that meet the requirements `~1.5.0` are: 1.5.0

all possible versions conflict with previously selected packages.

  previously selected package `async-std v1.6.2`
    ... which is depended on by `async-h1 v2.0.2`
    ... which is depended on by `tide v0.11.0`
    ... which is depended on by `graphql-tide-server v0.1.0 (/Users/<USER>/Documents/dev/RUST_WORKSPACE/graphql-tide-server)`

failed to select a version for `async-std` which could resolve this conflict

I have resolved it by enabling tokio runtime in async_std for now, incase someone stumbled with the same problem.

async-std = { version = "1.6.2", features = ["attributes", "tokio02"] }

@thedodd
Copy link
Owner

thedodd commented Jul 2, 2020

So, to be sure, Wither itself does not directly depend on tokio or async-std. So the dependency which needs to be match is whatever version mongodb itself depends upon. So, I know you mentioned that you tried to switch your dep to async-std 1.5, but did you do a cargo update after that? It could be that you updated it in your Cargo.toml, but that it wasn't updated in the lock file and therefore did not resolve the issue.

Either way, you absolutely should update your dependency on wither as I mentioned above:

wither = { version = "0.9.0-alpha.1", default-features = false, features = ["async-std-runtime"] }

I would recommend that you try that, and if you still run into issues, I'm still happy to help, I would just ask that you paste your Cargo.toml dependencies here so that I can try to pinpoint the issue.

@rumanHuq
Copy link

rumanHuq commented Jul 3, 2020

The problem is with different version of async-std requirements for tide and mongo. Tide required ^1.6 while mongo requires 1.5. Either way, it has nothing to do with wither as you've mentioned.

@thedodd
Copy link
Owner

thedodd commented Jul 3, 2020

@rumanbsl what we should probably do is request that the mongo team loosen their async-std requirement to “1” instead of a specific minor version like “1.5”.

@rumanHuq
Copy link

rumanHuq commented Jul 4, 2020

@thedodd It definitely make sense to pin only major version from mongo side, or for that matter any library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants