Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
fix(graphql-server): coerce BigInt to string
Browse files Browse the repository at this point in the history
Also make sure values are not coerced to different types.
  • Loading branch information
gligneul committed Sep 5, 2023
1 parent 0e25296 commit d4a662e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed timestamp in GraphQL API
- Fixed BigInt in GraphQL API

## [1.0.0] 2023-08-22

Expand Down
21 changes: 5 additions & 16 deletions offchain/graphql-server/src/schema/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub enum RollupsGraphQLScalarValue {
#[graphql_scalar(name = "BigInt")]
impl GraphQLScalar for i64 {
fn resolve(&self) -> Value {
Value::scalar(*self)
// Convert to string because some clients can't handle 64 bits integers
Value::scalar(self.to_string())
}

fn from_input_value(v: &juniper::InputValue) -> Option<i64> {
Expand Down Expand Up @@ -120,33 +121,21 @@ impl<'de> serde::de::Visitor<'de> for RollupsGraphQLScalarValueVisitor {
where
E: serde::de::Error,
{
if value <= i32::max_value() as i64 {
self.visit_i32(value as i32)
} else {
Ok(RollupsGraphQLScalarValue::BigInt(value))
}
Ok(RollupsGraphQLScalarValue::BigInt(value))
}

fn visit_u32<E>(self, value: u32) -> Result<RollupsGraphQLScalarValue, E>
where
E: serde::de::Error,
{
if value <= i32::max_value() as u32 {
self.visit_i32(value as i32)
} else {
self.visit_u64(value as u64)
}
self.visit_i32(value as i32)
}

fn visit_u64<E>(self, value: u64) -> Result<RollupsGraphQLScalarValue, E>
where
E: serde::de::Error,
{
if value <= i64::MAX as u64 {
self.visit_i64(value as i64)
} else {
Ok(RollupsGraphQLScalarValue::Float(value as f64))
}
self.visit_i64(value as i64)
}

fn visit_f64<E>(self, value: f64) -> Result<RollupsGraphQLScalarValue, E> {
Expand Down
2 changes: 1 addition & 1 deletion offchain/graphql-server/tests/responses/input.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"data":{"input":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":1676489717,"blockNumber":0,"payload":"0x696e7075742d30"}}}
{"data":{"input":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":"1676489717","blockNumber":"0","payload":"0x696e7075742d30"}}}
2 changes: 1 addition & 1 deletion offchain/graphql-server/tests/responses/inputs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"data":{"inputs":{"totalCount":1,"edges":[{"node":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":1676489717,"blockNumber":0,"payload":"0x696e7075742d30"}}]}}}
{"data":{"inputs":{"totalCount":1,"edges":[{"node":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":"1676489717","blockNumber":"0","payload":"0x696e7075742d30"}}]}}}

0 comments on commit d4a662e

Please sign in to comment.