rust - 如何向 GraphQL Union 类型的 GraphQL 结构添加字段?(Rust 枚举和瞻博网络)
问题描述
我从Juniper GitBook复制了简单的 GraphQL Union 示例。
use juniper::{graphql_union, GraphQLObject};
// I copied the below from the Juniper Gitbook
#[derive(GraphQLObject)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
}
enum Character {
Human(Human),
Droid(Droid),
}
graphql_union!(Character: () where Scalar = <S> |&self| {
instance_resolvers: |_| {
&Human => match *self { Character::Human(ref h) => Some(h), _ => None },
&Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
}
});
这很好用。但后来我尝试在 GraphQLObject 结构中使用我的新字段。
// I added this type myself
#[derive(GraphQLObject)]
struct Movie {
protagonist: Character,
title: String,
}
这给了我一个奇怪的错误:
error[E0277]: the trait bound `Character: juniper::types::base::GraphQLType<__S>` is not satisfied
--> src/main.rs:24:10
|
24 | #[derive(GraphQLObject)]
| ^^^^^^^^^^^^^ the trait `juniper::types::base::GraphQLType<__S>` is not implemented for `Character`
|
= help: consider adding a `where Character: juniper::types::base::GraphQLType<__S>` bound
= note: required because of the requirements on the impl of `juniper::types::base::GraphQLType<__S>` for `&Character`
= note: required because of the requirements on the impl of `juniper::executor::IntoResolvable<'_, __S, &Character, _>` for `&Character`
= note: required by `juniper::executor::IntoResolvable::into`
我不知道是怎么回事。我知道我的 Movie 结构的 GraphQLObject 语法是正确的。
解决方案
您需要添加where Scalar = <S>
到您的graphql_union!
定义中:
graphql_union!(Character: () where Scalar = <S> |&self| {
instance_resolvers: |_| {
&Human => match *self { Character::Human(ref h) => Some(h), _ => None },
&Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
}
});
这修复了错误。我在最新的 Gitbook 源代码大师中找到了这个。也许当前发布的文档已经过时了。
推荐阅读
- python-3.x - 迭代所有页面和爬虫表的元素,在 Python 中保存为数据框
- excel - 在页脚中设置文本格式
- javascript - 如何在打字稿中形成数组数组?
- javascript - Webpack esm 模块解析“../node_modules/react-bootstrap/esm/index.js 中的警告”
- c# - MongoDB .NET - 按名称获取 IMongoCollection
- c++ - cfmakeraw() 在 Macos X / Unix C / C++ 上可用吗?
- javascript - 如何将对象数组转换为基本对象?
- ruby - 通过厨师的 rspec 测试运行 ntpq -p 命令
- cmake - 如何使 CMake 的 add_custom_command(TARGET ... POST_BUILD ...) 依赖于常规文件?
- sql - 使用 azcopy 将 .bak 文件复制到 Azure 存储