Skip to content

Commit decffb9

Browse files
committed
feat: support alt text for images
Adds `alt` attribute to the `Image` type and optional `alt` parameter to the `prepareImageUpload` mutation.
1 parent ab102e6 commit decffb9

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/schema/mod.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ impl Image {
7979
self.0.into()
8080
}
8181

82+
async fn alt<'a>(
83+
&self,
84+
ctx: &Context<'a>,
85+
) -> Result<Option<String>> {
86+
let data = ctx.data::<AppData>()?;
87+
let client = match &data.pg_pool {
88+
Some(pool) => pool.get().await?,
89+
None => {
90+
return Err("Database connection is not available".into());
91+
}
92+
};
93+
94+
let result = client
95+
.query_one(
96+
"
97+
SELECT alt
98+
FROM images
99+
WHERE id = $1
100+
",
101+
&[&self.0],
102+
)
103+
.await?;
104+
105+
let value: Option<&str> = result.try_get(0)?;
106+
107+
Ok(value.map(|s| s.to_string()))
108+
}
109+
82110
async fn download_url<'a>(
83111
&self,
84112
ctx: &Context<'a>,
@@ -1630,6 +1658,10 @@ impl MutationRoot {
16301658
validator(min_length = 1),
16311659
desc = "Name of image file",
16321660
)] name: String,
1661+
#[graphql(
1662+
validator(min_length = 1),
1663+
desc = "Alternative text",
1664+
)] alt: Option<String>,
16331665
) -> Result<PrepareImageUploadResult> {
16341666
let appdata = ctx.data::<AppData>()?;
16351667

@@ -1650,7 +1682,7 @@ impl MutationRoot {
16501682

16511683
let image = Image(
16521684
transaction
1653-
.query_one("INSERT INTO images DEFAULT VALUES RETURNING id", &[])
1685+
.query_one("INSERT INTO images (alt) VALUES ($1) RETURNING id", &[&alt])
16541686
.await?
16551687
.get::<_, i32>(0),
16561688
);

0 commit comments

Comments
 (0)