Skip to content

Commit

Permalink
feat(graphql): add MissingRootObject error for DF queries
Browse files Browse the repository at this point in the history
  • Loading branch information
unmaykr-aftermath committed Jun 9, 2024
1 parent 5afe7a4 commit 8cbe505
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
8 changes: 8 additions & 0 deletions crates/sui-graphql-rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ pub enum Error {
CursorNoFirstLast,
#[error("Connection's page size of {0} exceeds max of {1}")]
PageTooLarge(u64, u64),
#[error(
"To get the dynamic field(s) of an IObject, the query must be rooted in a non-child object,\
i.e, a shared or an owned object, so that its version can be used to bound the DFs' \
versions. To get the dynamic fields of an address at the latest checkpoint, use the \
Query.owner field instead."
)]
MissingRootObject,
// Catch-all for client-fault errors
#[error("{0}")]
Client(String),
Expand All @@ -85,6 +92,7 @@ impl ErrorExtensions for Error {
| Error::CursorNoFirstLast
| Error::PageTooLarge(_, _)
| Error::ProtocolVersionUnsupported(_, _)
| Error::MissingRootObject
| Error::Client(_) => {
e.set("code", code::BAD_USER_INPUT);
}
Expand Down
13 changes: 10 additions & 3 deletions crates/sui-graphql-rpc/src/types/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Coin {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_field(ctx, name, self.super_.root_version())
.dynamic_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -261,7 +261,7 @@ impl Coin {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_object_field(ctx, name, self.super_.root_version())
.dynamic_object_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -278,7 +278,14 @@ impl Coin {
before: Option<object::Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_fields(ctx, first, after, last, before, self.super_.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.super_.get_root_version()?),
)
.await
}

Expand Down
13 changes: 10 additions & 3 deletions crates/sui-graphql-rpc/src/types/coin_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl CoinMetadata {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_field(ctx, name, self.super_.root_version())
.dynamic_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -250,7 +250,7 @@ impl CoinMetadata {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_object_field(ctx, name, self.super_.root_version())
.dynamic_object_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -267,7 +267,14 @@ impl CoinMetadata {
before: Option<object::Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_fields(ctx, first, after, last, before, self.super_.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.super_.get_root_version()?),
)
.await
}

Expand Down
20 changes: 17 additions & 3 deletions crates/sui-graphql-rpc/src/types/move_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl MoveObject {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_)
.dynamic_field(ctx, name, self.root_version())
.dynamic_field(ctx, name, Some(self.get_root_version()?))
.await
}

Expand All @@ -329,7 +329,7 @@ impl MoveObject {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_)
.dynamic_object_field(ctx, name, self.root_version())
.dynamic_object_field(ctx, name, Some(self.get_root_version()?))
.await
}

Expand All @@ -346,7 +346,14 @@ impl MoveObject {
before: Option<object::Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(&self.super_)
.dynamic_fields(ctx, first, after, last, before, self.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.get_root_version()?),
)
.await
}

Expand Down Expand Up @@ -462,6 +469,13 @@ impl MoveObject {
pub(crate) fn root_version(&self) -> Option<u64> {
self.super_.root_version()
}

/// Require this object's root parent object version for dynamic field queries.
///
/// Fails with an error if the query was rooted in a child object.
pub(crate) fn get_root_version(&self) -> Result<u64, Error> {
self.super_.get_root_version()
}
}

impl TryFrom<&Object> for MoveObject {
Expand Down
20 changes: 17 additions & 3 deletions crates/sui-graphql-rpc/src/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl Object {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(self)
.dynamic_field(ctx, name, self.root_version())
.dynamic_field(ctx, name, Some(self.get_root_version()?))
.await
}

Expand All @@ -491,7 +491,7 @@ impl Object {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(self)
.dynamic_object_field(ctx, name, self.root_version())
.dynamic_object_field(ctx, name, Some(self.get_root_version()?))
.await
}

Expand All @@ -508,7 +508,14 @@ impl Object {
before: Option<Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(self)
.dynamic_fields(ctx, first, after, last, before, self.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.get_root_version()?),
)
.await
}

Expand Down Expand Up @@ -729,6 +736,13 @@ impl Object {
self.root_version
}

/// Require this object's root parent object version for dynamic field queries.
///
/// Fails with an error if the query was rooted in a child object.
pub(crate) fn get_root_version(&self) -> Result<u64, Error> {
self.root_version.ok_or(Error::MissingRootObject)
}

/// Query the database for a `page` of objects, optionally `filter`-ed.
///
/// `checkpoint_viewed_at` represents the checkpoint sequence number at which this page was
Expand Down
13 changes: 10 additions & 3 deletions crates/sui-graphql-rpc/src/types/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl StakedSui {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_field(ctx, name, self.super_.root_version())
.dynamic_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -269,7 +269,7 @@ impl StakedSui {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_object_field(ctx, name, self.super_.root_version())
.dynamic_object_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -286,7 +286,14 @@ impl StakedSui {
before: Option<object::Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_fields(ctx, first, after, last, before, self.super_.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.super_.get_root_version()?),
)
.await
}

Expand Down
13 changes: 10 additions & 3 deletions crates/sui-graphql-rpc/src/types/suins_registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl SuinsRegistration {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_field(ctx, name, self.super_.root_version())
.dynamic_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -306,7 +306,7 @@ impl SuinsRegistration {
name: DynamicFieldName,
) -> Result<Option<DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_object_field(ctx, name, self.super_.root_version())
.dynamic_object_field(ctx, name, Some(self.super_.get_root_version()?))
.await
}

Expand All @@ -323,7 +323,14 @@ impl SuinsRegistration {
before: Option<object::Cursor>,
) -> Result<Connection<String, DynamicField>> {
OwnerImpl::from(&self.super_.super_)
.dynamic_fields(ctx, first, after, last, before, self.super_.root_version())
.dynamic_fields(
ctx,
first,
after,
last,
before,
Some(self.super_.get_root_version()?),
)
.await
}

Expand Down

0 comments on commit 8cbe505

Please sign in to comment.