Open t1ltxz-gxd opened 2 months ago
The library is trying to simply be representations of the API, which is still unstable. After the changes, does the example work?
I changed it a little bit and now I have Meitantei Conan as the answer. Below is the code that is currently working
use chrono::{DateTime, Utc};
use shikimori::{client::ClientBuilder,
cynic,
cynic::{Id, QueryFragment, QueryVariables, QueryBuilder},
graphql::{
anime::AnimeKind,
types::EntityOrder,
scalars::{AnimeStatusString},
schema
}
};
#[derive(QueryVariables, Debug)]
pub struct AnimesQueryVariables {
pub page: i32,
pub status: AnimeStatusString,
pub order: EntityOrder,
}
#[derive(QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "AnimesQueryVariables", schema_path = "../schema.graphql")]
pub struct AnimesQuery {
#[arguments(censored: false, page: $page, status: $status, order: $order)]
pub animes: Vec<Anime>,
}
#[derive(QueryFragment, Debug)]
#[cynic(graphql_type = "Anime", variables = "AnimesQueryVariables", schema_path = "../schema.graphql")]
pub struct Anime {
pub id: Id,
pub franchise: Option<String>,
pub episodes: i32,
pub kind: Option<AnimeKind>,
pub next_episode_at: Option<DateTime<Utc>>,
pub url: String,
}
#[tokio::main]
async fn main() {
let client = ClientBuilder::new().build();
let response = client
.query(AnimesQuery::build(AnimesQueryVariables {
page: 1,
status: AnimeStatusString::new("ongoing"),
order: EntityOrder::Popularity,
}))
.await;
dbg!(&response);
}
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/shikimori`
[shikimori/src/main.rs:51:5] &response = Ok(
GraphQlResponse {
data: Some(
AnimesQuery {
animes: [
Anime {
id: Id(
"21",
),
franchise: Some(
"one_piece",
),
episodes: 0,
kind: Some(
Tv,
),
next_episode_at: Some(
2024-05-05T00:30:00Z,
),
url: "https://shikimori.one/animes/21-one-piece",
},
Anime {
id: Id(
"235",
),
franchise: Some(
"detective_conan",
),
episodes: 0,
kind: Some(
Tv,
),
next_episode_at: Some(
2024-05-04T09:00:00Z,
),
url: "https://shikimori.one/animes/z235-meitantei-conan",
},
],
},
),
errors: None,
},
)
Process finished with exit code 0
I had to download schema.graphql
from shikimori to get the code to work
I've been thinking about how to make this point better, but haven't come to a conclusion yet. Maybe I should create a feature that will export the following code:
#[cfg(feature = "codegen")]
pub fn register_schema() {
cynic_codegen::register_schema("shikimori")
.from_sdl(include_str!("./schema.graphql"))
.unwrap()
.as_default()
.unwrap();
}
in my opinion, the build should affect derive so that library users do not need to download the schema for their code to work
I noticed that r.n. there is a problem with the error Unresolved import: shikimori::graphql::schema
[E0432].
Build should generate an exportable module schema
for this to solve the problem 🐣
Build output:
My code taken from the example use case:
Here I only changed use
![img](https://github.com/negezor/shikimori-rust/assets/71944942/0f6e6090-5460-478b-983c-e57a3e4999a8)
scalars::{StatusString}
toscalars::{AnimeStatusString}
, cause the code was refactored but you forgot to update the example =)