Skip to content

Commit

Permalink
Fix IPFS CID parsing logic in GraphQL resolvers
Browse files Browse the repository at this point in the history
Signed-off-by: Filippo Costa <[email protected]>
  • Loading branch information
neysofu committed Apr 15, 2024
1 parent 61a0c23 commit 5650cc5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/common_types/src/hex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
)]
// TODO: The fact that we SQL-encode all kinds of hex strings, even fixed-length
// ones, as variable-length byte sequences is a bit of a wart. Not that big of a
// deal, but maybe there's a better way to do this.
// deal though.
#[diesel(sql_type = sql_types::Binary)]
pub struct HexString<T>(pub T);

Expand Down
26 changes: 24 additions & 2 deletions crates/common_types/src/ipfs_cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ pub struct IpfsCid(cid::Cid);
#[async_graphql::Scalar]
impl async_graphql::ScalarType for IpfsCid {
fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
Ok(Deserialize::deserialize(value.into_json()?)?)
let async_graphql::Value::String(string) = value else {
return Err(async_graphql::InputValueError::expected_type(value));
};

let cid = cid::Cid::from_str(&string)?;
Ok(IpfsCid(cid))
}

fn to_value(&self) -> async_graphql::Value {
Expand Down Expand Up @@ -62,10 +67,19 @@ impl Arbitrary for IpfsCid {

#[cfg(test)]
mod tests {
use async_graphql::ScalarType;
use quickcheck_macros::quickcheck;

use super::*;

#[quickcheck]
fn async_graphql_roundtrip(ipfs_cid: IpfsCid) -> bool {
let async_graphql_value = ipfs_cid.to_value();
let ipfs_cid2: IpfsCid = ScalarType::parse(async_graphql_value).unwrap();

ipfs_cid == ipfs_cid2
}

#[quickcheck]
fn serde_roundtrip(ipfs_cid: IpfsCid) -> bool {
let json = serde_json::to_string(&ipfs_cid).unwrap();
Expand All @@ -74,8 +88,16 @@ mod tests {
ipfs_cid == ipfs_cid2
}

#[quickcheck]
fn from_str_roundtrip_quickcheck(ipfs_cid: IpfsCid) -> bool {
let string = ipfs_cid.to_string();
let ipfs_cid2 = IpfsCid::from_str(&string).unwrap();

ipfs_cid == ipfs_cid2
}

#[test]
fn deployment_id_from_str_roundtrip() {
fn from_str_roundtrip() {
let deployment_id = "QmNY7gDNXHECV8SXoEY7hbfg4BX1aDMxTBDiFuG4huaSGA";
let ipfs_id = IpfsCid::from_str(deployment_id).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions ops/compose/dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
environment:
# Plugins:
# - https://github.com/fifemon/graphql-datasource for GraphQL data sources.
- GF_INSTALL_PLUGINS=fifemon-graphql-datasource
- GF_INSTALL_PLUGINS=fifemon-graphql-datasource,yesoreyeram-infinity-datasource
volumes:
- ./grafana/config/:/etc/grafana/
- ./grafana/data:/var/lib/grafana/
Expand Down Expand Up @@ -57,7 +57,7 @@ services:
PGDATA: "/var/lib/postgresql/data"
command: -p 5433
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
interval: 1s
timeout: 1s
retries: 1000
Expand Down
Binary file modified ops/compose/grafana/data/grafana.db
Binary file not shown.

0 comments on commit 5650cc5

Please sign in to comment.