2 releases

0.0.3 Jul 24, 2022
0.0.2 Jul 23, 2022
0.0.1 Jul 23, 2022

#26 in #juniper

Download history 7/week @ 2024-02-19 40/week @ 2024-02-26 4/week @ 2024-03-11 11/week @ 2024-03-25 64/week @ 2024-04-01

79 downloads per month
Used in juniper-compose

MIT license

13KB
282 lines

juniper-compose

Merge multiple Juniper object definitions into a single object type.

crates.io | docs | github

Motivation

You are building a GraphQL server using Juniper. At some point you realize that you have gigantic Query and Mutation types:

#[derive(Default)]
struct Query;

#[juniper::graphql_object]
impl Query {
    async fn user(ctx: &Context, id: Uuid) -> User {
        // ...
    }

    async fn users(ctx: &Context) -> Vec<User> {
        // ...
    }

    async fn task(ctx: &Context, id: Uuid) -> Task {
        // ...
    }

    async fn tasks(ctx: &Context) -> Vec<Task> {
        // ...
    }
    
    // ...many more
}

You would like to split it up into multiple domain-specific files, and have e.g. all User queries in one file and all Task queries in the other. With current Juniper API, it is very hard to do, but this crate can help you.

Usage

#[derive(Default)]
struct UserQueries;

#[composable_object]
#[juniper::graphql_object]
impl UserQueries {
    async fn user(ctx: &Context, id: Uuid) -> User {
        // ...
    }

    async fn users(ctx: &Context) -> Vec<User> {
        // ...
    }
}

#[derive(Default)]
struct TaskQueries;

#[composable_object]
#[juniper::graphql_object]
impl TaskQueries {
    async fn task(ctx: &Context, id: Uuid) -> Task {
        // ...
    }

    async fn tasks(ctx: &Context) -> Vec<Task> {
        // ...
    }
}

composite_object!(Query(UserQueries, TaskQueries));

Custom contexts are supported:

composite_object!(Query<Context = MyCustomContext>(UserQueries, TaskQueries));

Visibility specifier for generated type is supported:

composite_object!(pub(crate) Query<Context = MyCustomContext>(UserQueries, TaskQueries));

Custom scalars are currently not supported, but will be added if requested.

Dependencies

~1.5MB
~33K SLoC