interface field as interface inherit to maping not implement error #1093
-
Hi, I had a schema.graphql, But i can't map it to juniper rust code correct! My confused If graphql's interface must map to rust trait? and i finally used box as a field. and @ilslv told me representd interface ImageData map to rust enum? i do not know how to map it. @ my schema.graphql code scalar BigInt
# Long is a 64 bit unsigned integer.
scalar Long
interface ImageData {
src: String!
contentType: String
}
interface CollectionVolume {
change24h: Int
}
interface UserAvatarOmit {
id: String!
tokenId: String!
image: ImageData!
}
interface CollectionOwner {
address: String!
isVerified: Boolean!
name: String!
avatar: UserAvatarOmit
}
interface CollectionBase {
name: String!
logo: ImageData
}
"The root query object of the schema"
type QueryRoot {
collections() : [Collection]
}
type Collection implements CollectionBase {
"collectionbase attr"
name: String!
logo: ImageData
"type collection attr"
owner: CollectionOwner!
}
schema {
query: QueryRoot
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
This is @ilslv 's response
|
Beta Was this translation helpful? Give feedback.
-
@ilslv I now understand i need retyped imageData to rust enum but I still can't compiler correct , pls help when you are free,thanks. #![allow(missing_docs)]
use std::collections::HashMap;
use juniper::{graphql_interface, graphql_object, Context, GraphQLEnum};
#[derive(Clone, Copy, Debug)]
pub struct Query;
#[graphql_object(context = Database)]
/// The root query object of the schema
impl Query {
fn collection(
#[graphql(context)] database: &Database,
#[graphql(description = "id of the collection")] id: String,
) -> Option<&Collection> {
database.get_collection(&id)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Subscription;
#[derive(Clone)]
pub struct ImageData {
src: String,
contentType: String,
}
impl ImageData {
pub fn new(src: &str, contentType: &str) -> Self {
Self {
src: src.into(),
contentType: contentType.into(),
}
}
}
#[graphql_interface(enum = CollectionBaseValue for = Collection)]
pub trait CollectionBase {
/// The id of the character
fn id(&self) -> &str;
/// The name of the collectionbase
fn name(&self) -> Option<&str>;
/// logo
fn logo(&self) -> ImageData;
}
// #[derive(GraphQLObject)]
#[derive(Clone)]
// #[graphql_object(impl = CollectionBase)]
pub struct Collection {
/// base
id: String,
name: String,
logo: ImageData,
// owner method
// owner: Box<dyn CollectionOwner>,
}
impl Collection {
pub fn new(
id: &str,
name: &str,
logo: ImageData,
// owner: Box<dyn CollectionOwner>,
) -> Self {
Self {
id: id.into(),
/// base
name: name.into(),
// name: name.map(Into::into),
logo: logo.clone().into(),
// owner: owner,
}
}
}
#[graphql_object(context = Database, impl = CollectionBaseValue)]
impl Collection {
pub fn id(&self) -> &str {
&self.id
}
fn name(&self) -> Option<&str> {
Some(self.name.as_str())
}
fn logo(&self) -> ImageData {
let logo = ImageData::new("hello", "images");
logo
}
// Collection
// fn owner(&self) -> Box<dyn CollectionOwner> {
// self.owner
// }
}
#[derive(Clone, Default)]
pub struct Database {
collections: HashMap<String, Collection>,
}
impl Context for Database {}
impl Database {
pub fn new() -> Database {
let mut collections = HashMap::new();
collections.insert(
"3000".into(),
Collection::new(
"3000",
"monkey",
ImageData {
src: String::from("sdf"),
contentType: String::from("image/png"),
},
),
);
Database { collections }
}
pub fn get_collection(&self, id: &str) -> Option<&Collection> {
self.collections.get(id)
}
pub fn get_collectionBase(&self, id: &str) -> Option<CollectionBaseValue> {
#[allow(clippy::manual_map)]
if let Some(h) = self.collections.get(id) {
Some(h.clone().into())
} else {
None
}
}
}
fn main() {
let db = Database::new();
} |
Beta Was this translation helpful? Give feedback.
-
@stanhangzhou Rust representation of schema you've described in original question would look something like: #[graphql_interface]
trait ImageData {
fn src(&self) -> String;
fn content_type(&self) -> Option<String>,
}
#[graphql_interface]
trait CollectionVolume {
fn change_24h(&self) -> Option<i32>;
}
#[graphql_interface]
trait UserAvatarOmit {
fn id(&self) -> String;
fn token_id(&self) -> String;
fn image(&self) -> ImageDataValue;
}
#[graphql_interface]
trait UserAvatarOmit {
fn address(&self) -> String;
fn is_verified(&self) -> bool;
fn name(&self) -> String;
fn avatar(&self) -> Option<UserAvatarOmitValue>;
}
#[graphql_interface]
trait CollectionBase {
fn name(&self) -> String;
fn logo(&self) -> Option<ImageDataValue>;
} |
Beta Was this translation helpful? Give feedback.
@stanhangzhou Rust representation of schema you've described in original question would look something like: