Giter Site home page Giter Site logo

qmx / juniper-from-schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidpdrsn/juniper-from-schema

0.0 2.0 0.0 362 KB

Schema first GraphQL in Rust with Juniper

Home Page: https://crates.io/crates/juniper-from-schema

Rust 99.84% Shell 0.16%

juniper-from-schema's Introduction

This library contains a procedural macro that reads a GraphQL schema file, and generates the corresponding Juniper macro calls. This means you can have a real schema file and be guaranteed that it matches your Rust implementation. It also removes most of the boilerplate involved in using Juniper.

Example

Imagine you have a GraphQL schema like this:

schema {
  query: Query
}

type Query {
  helloWorld(name: String!): String! @juniper(ownership: "owned")
}

That can be implemented like so:

use juniper_from_schema::graphql_schema_from_file;

// This is the important line
graphql_schema_from_file!("readme_schema.graphql");

pub struct Context;

impl juniper::Context for Context {}

pub struct Query;

// This trait is generated by `graphql_schema_from_file!` based on the schema
impl QueryFields for Query {
    fn field_hello_world(
        &self,
        executor: &juniper::Executor<'_, Context>,
        name: String,
    ) -> juniper::FieldResult<String> {
        Ok(format!("Hello, {}!", name))
    }
}

pub struct Mutation;

// This trait is generated by `graphql_schema_from_file!` based on the schema
impl MutationFields for Mutation {
    fn field_noop(
      &self,
      executor: &juniper::Executor<'_, Context>,
    ) -> juniper::FieldResult<&bool> {
        Ok(&true)
    }
}

fn main() {
    let ctx = Context;

    let query = "query { helloWorld(name: \"Ferris\") }";

    let (result, errors) = juniper::execute(
        query,
        None,
        &Schema::new(Query, Mutation),
        &juniper::Variables::new(),
        &ctx,
    )
    .unwrap();

    assert_eq!(errors.len(), 0);
    assert_eq!(
        result
            .as_object_value()
            .unwrap()
            .get_field_value("helloWorld")
            .unwrap()
            .as_scalar_value::<String>()
            .unwrap(),
        "Hello, Ferris!",
    );
}

See the crate documentation for a usage examples and more info.

N+1s

If you're having issues with N+1 query bugs consider using juniper-eager-loading. It was built to integrate seamlessly with juniper-from-schema.


License: MIT

juniper-from-schema's People

Contributors

davidpdrsn avatar legneato avatar husseinraoouf avatar kiljacken avatar mgeisler avatar

Watchers

James Cloos avatar  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.