Giter Site home page Giter Site logo

cgubbin / juniper-compose Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nikis05/juniper-compose

0.0 0.0 0.0 13 KB

Merge multiple Juniper object definitions into a single object type.

Home Page: https://docs.rs/juniper-compose

License: MIT License

Rust 96.14% Dockerfile 3.86%

juniper-compose's Introduction

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.

juniper-compose's People

Contributors

nikis05 avatar cgubbin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.