• Stars
    star
    231
  • Rank 173,434 (Top 4 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Schema first GraphQL in Rust with Juniper

juniper-from-schema

Build crates.io Documentation

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.

Looking for juniper 0.15 support?

The version of juniper-from-schema that is released on crates.io (0.5.2) doesn't support juniper 0.15. However the master branch does! So you will have to use a git dependency for now. We plan to do an official release soon. Follow this milestone to see whats left.

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))
    }
}

fn main() {
    let ctx = Context;

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

    let (result, errors) = juniper::execute_sync(
        query,
        None,
        &Schema::new(Query, juniper::EmptyMutation::new()),
        &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.

Development

If you're seeing No such file or directory (os error 2) when running the tests

This might be caused by setting CARGO_TARGET_DIR. Setting that env var changes the directory the trybuild tests are run from which means all the paths to the test schemas no longer match. The only workaround is to unset CARGO_TARGET_DIR when working on juniper-from-schema. I recommend direnv to unset the env var only this directory and not globally.

More Repositories

1

todo-or-die

TODOs you cannot forget!
Rust
585
star
2

axum-live-view

Real-time user experiences with server-rendered HTML
Rust
200
star
3

extend

Create extensions for types you don't own with extension traits but without the boilerplate
Rust
124
star
4

graphql-app-example

A complete example how to setup a Rust GraphQL web server
Rust
104
star
5

witter

Twitter clone we're building live on twitch.tv/davidpdrsn
Rust
86
star
6

assert-json-diff

Easily compare two JSON values and get great output
Rust
79
star
7

juniper-eager-loading

Library for avoiding N+1 query bugs with Juniper
Rust
68
star
8

dotfiles

My dotfiles
Vim Script
59
star
9

vim-spectacular

Run tests easily in any language, with any framework
Ruby
31
star
10

axum-tungstenite

WebSocket connections for axum directly using tungstenite
Rust
24
star
11

axum-flash

Flash messages for axum
Rust
23
star
12

diesel-factories

Test factories for Diesel
Rust
23
star
13

axum-typed-websockets

axum::extract::ws with type safe messages
Rust
20
star
14

robin

[UNMAINTAINED] Background jobs for Rust
Rust
18
star
15

git-remove-merged-branches

CLI to remove git branches whose PR has been merged
Rust
16
star
16

i18n_codegen

i18n library for Rust with compile time checks
Rust
15
star
17

dilemma

[Experimental]: SQL query builder favouring composition over compile time safety
Rust
14
star
18

cargo-public-api-crates

Cargo subcommand to find crates in your public API
Rust
12
star
19

tower-hyper-http-body-compat

Adapters between hyper 0.14-1.0, http-body 0.4-1.0, and tower-service 0.3.
Rust
11
star
20

CardistryIO

The cardistry community of the future
Ruby
8
star
21

hashtag-rs

Hashtag parser
Rust
7
star
22

axum-resource

Rust
6
star
23

sns-push-notifications

Library for sending iOS and Android push notifications with AWS SNS
Rust
6
star
24

tokio-console-web

Prototype web UI for tokio-console
Rust
6
star
25

load-dotenv

This is a small procedural macro to load your .env file at compile time
Rust
5
star
26

git-branch-picker

Fuzzy search for git branches
Rust
5
star
27

json-parser

JSON parser built live on stream
Rust
5
star
28

vim-leaderboard

Keep track of which Vim leader commands you run
Vim Script
4
star
29

delete-git-branches

CLI to delete old Git branches
Rust
3
star
30

vim-smart-commandt

Only flush Command-T cache when you have to
Vim Script
3
star
31

smell-spec

A small testing framework for SML inspired by RSpec
Standard ML
3
star
32

Java-Style-Guide

A Java style guide for people studying Object Oriented Programming and Design at Copenhagen University
Java
3
star
33

git-prompt

Print something like `[master@484c4c9670bb2]` for prompt
Rust
3
star
34

serializers

Easily create different JSON representations of the same type.
Rust
2
star
35

axum-extractor-config

Rust
2
star
36

Socially-fitter

Let's get fit together (school project)
PHP
2
star
37

klee-presentation

C
2
star
38

Vim-normal-mode-commands

All of Vims normale mode commands explained briefly
2
star
39

smart-pwd

Shortest unique pwd
Rust
1
star
40

lang

A silly programming language
Rust
1
star
41

Ten-Moves

iOS app I'm working on
Objective-C
1
star
42

tower-pipeline

A Tower Service combinator that "pipelines" two services
Rust
1
star
43

memosine

Toy SQL database built from scratch
Rust
1
star
44

markdown-live-preview

Live preview of a markdown file
Rust
1
star
45

oops-lang

A silly programming language
Rust
1
star
46

leaderboard

Parse a vimrc and find all the leader commands
Ruby
1
star
47

axum-handle-error-slow-compile

Reproduction of slow axum compile
Rust
1
star
48

git-git

Rewrite of https://github.com/tonsser/api-git-scripts in Rust
Rust
1
star
49

juniper-look-ahead-with-fragments-error

Rust
1
star
50

test-wrapper

Hack to run Rust tests in a file or on a line
Rust
1
star
51

vim-notable

Easy per-project notes
Vim Script
1
star
52

trust-me-its-minitest

Minitest implemented on top of Rspec
Ruby
1
star