cala_server/graphql/
job.rs

1use async_graphql::{types::connection::*, *};
2use serde::{Deserialize, Serialize};
3
4use super::{convert::ToGlobalId, primitives::*};
5
6#[derive(SimpleObject)]
7pub struct Job {
8    pub id: ID,
9    pub job_id: UUID,
10    pub name: String,
11    pub description: Option<String>,
12}
13
14#[derive(Serialize, Deserialize)]
15pub(super) struct JobByNameCursor {
16    pub name: String,
17    pub id: crate::primitives::JobId,
18}
19
20impl CursorType for JobByNameCursor {
21    type Error = String;
22
23    fn encode_cursor(&self) -> String {
24        use base64::{engine::general_purpose, Engine as _};
25        let json = serde_json::to_string(&self).expect("could not serialize token");
26        general_purpose::STANDARD_NO_PAD.encode(json.as_bytes())
27    }
28
29    fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
30        use base64::{engine::general_purpose, Engine as _};
31        let bytes = general_purpose::STANDARD_NO_PAD
32            .decode(s.as_bytes())
33            .map_err(|e| e.to_string())?;
34        let json = String::from_utf8(bytes).map_err(|e| e.to_string())?;
35        serde_json::from_str(&json).map_err(|e| e.to_string())
36    }
37}
38
39impl ToGlobalId for crate::primitives::JobId {
40    fn to_global_id(&self) -> async_graphql::types::ID {
41        async_graphql::types::ID::from(format!("job:{self}"))
42    }
43}
44
45impl From<JobByNameCursor> for crate::job::JobByNameCursor {
46    fn from(cursor: JobByNameCursor) -> Self {
47        Self {
48            name: cursor.name,
49            id: cursor.id,
50        }
51    }
52}
53
54impl From<&crate::job::Job> for JobByNameCursor {
55    fn from(job: &crate::job::Job) -> Self {
56        Self {
57            name: job.name.clone(),
58            id: job.id,
59        }
60    }
61}
62
63impl From<crate::job::Job> for Job {
64    fn from(job: crate::job::Job) -> Self {
65        Self {
66            id: job.id.to_global_id(),
67            job_id: UUID::from(job.id),
68            name: job.name,
69            description: job.description,
70        }
71    }
72}