• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

davidpdrsn/juniper-from-schema: Schema first GraphQL in Rust with Juniper

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

davidpdrsn/juniper-from-schema

开源软件地址(OpenSource Url):

https://github.com/davidpdrsn/juniper-from-schema

开源编程语言(OpenSource Language):

Rust 99.9%

开源软件介绍(OpenSource Introduction):

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap