gpt4 book ai didi

rust - 如何将字段添加到 GraphQL Union 类型的 GraphQL 结构? (Rust 枚举和 Juniper)

转载 作者:行者123 更新时间:2023-11-29 08:20:01 29 4
gpt4 key购买 nike

我从 the 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 },
}
});

这修复了错误。我在 latest master of the Gitbook's source 上找到了这个.也许当前发布的文档已过时。

关于rust - 如何将字段添加到 GraphQL Union 类型的 GraphQL 结构? (Rust 枚举和 Juniper),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57546247/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com