Skip to content
\n

and then, in each query of each modules I wanted to do this:

\n

Example: [users/query.rs]

\n
use juniper::{graphql_object, FieldError, FieldResult};\n\nuse crate::graphql::schema::Context;\nuse crate::graphql::query::QueryRoot;\nuse crate::graphql::users::schema::User;\n\n\n#[graphql_object(context = Context)]\nimpl QueryRoot {\n    pub async fn get_user_by_id(context: &Context, id: i32) -> FieldResult<User> {\n        let sql = r#\"\n            select\n                id::int4,\n                email,\n                documento\n            from\n                auth_users\n            where\n                id = $1\n        \"#;\n        let data = sqlx::query_as::<_, User>(sql)\n            .bind(id)\n            .fetch_one(&context.pool)\n            .await;\n\n        match data {\n            Ok(user) => Ok(user),\n            _ => Err(FieldError::from(\"no se encontró el usuario\")),\n        }\n    }\n\n    pub async fn get_all_user(context: &Context, page: i32, page_size: i32) -> FieldResult<Vec<User>> {\n        let sql = r#\"\n            select\n                id::int4,\n                email,\n                documento\n            from\n                auth_users\n            order by\n                id\n            OFFSET $1*$2 LIMIT $2;\n        \"#;\n        let data = sqlx::query_as::<_, User>(sql)\n            .bind(page)\n            .bind(page_size)\n            .fetch_all(&context.pool)\n            .await.unwrap();\n\n        Ok(data)\n    }\n}\n
\n

I get this error:

\n
\n

src/graphql2/users/query.rs:8:1
\n|
\n8 | #[graphql_object(context = Context)]
\n| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for graphql::query::QueryRoot
\n|
\n::: src/graphql/query.rs:14:1
\n|
\n14 | #[graphql_object(context = Context)]
\n| ------------------------------------ first implementation here
\n|
\n= note: this error originates in the attribute macro graphql_object (in Nightly builds, run with -Z macro-backtrace for more info)

\n
\n

If I do not use #[graphql_object(context = Context)] in users/query.rs the functions are not available

\n

I also tried to use the functions of users/query.rs a root query.rs and use them in the impl of QueryRoot

\n
pub struct QueryRoot;\n\n\n#[graphql_object(context = Context)]\nimpl QueryRoot {\n    async fn api_version() -> &str {\n        \"0.1\"\n    }\n    async fn get_user_by_id(context: &Context, id: i32) -> FieldResult<User> {\n        get_user_by_id(context, id).await\n    }\n\n    async fn get_all_user(context: &Context, page: i32, page_size: i32) -> FieldResult<Vec<User>> {\n       get_all_user(context, page, page_size).await\n    }\n}\n\n
\n

The above works fine, but I don't like to have to redefine each of the functions

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

This #646 (comment) works for me

","upvoteCount":1,"url":"https://github.com/graphql-rust/juniper/discussions/1141#discussioncomment-4720082"}}}
Discussion options

You must be logged in to vote

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by josejachuf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant