From d4a662eee419ea6ebaec25b1ff82e636320affa4 Mon Sep 17 00:00:00 2001 From: Gabriel de Quadros Ligneul <8294320+gligneul@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:34:57 -0300 Subject: [PATCH] fix(graphql-server): coerce BigInt to string Also make sure values are not coerced to different types. --- CHANGELOG.md | 1 + offchain/graphql-server/src/schema/scalar.rs | 21 +++++-------------- .../graphql-server/tests/responses/input.json | 2 +- .../tests/responses/inputs.json | 2 +- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a423589..dfa7c55b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/offchain/graphql-server/src/schema/scalar.rs b/offchain/graphql-server/src/schema/scalar.rs index 92fb0cc8..56f71237 100644 --- a/offchain/graphql-server/src/schema/scalar.rs +++ b/offchain/graphql-server/src/schema/scalar.rs @@ -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 { @@ -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(self, value: u32) -> Result 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(self, value: u64) -> Result 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(self, value: f64) -> Result { diff --git a/offchain/graphql-server/tests/responses/input.json b/offchain/graphql-server/tests/responses/input.json index 12d599dd..91196464 100644 --- a/offchain/graphql-server/tests/responses/input.json +++ b/offchain/graphql-server/tests/responses/input.json @@ -1 +1 @@ -{"data":{"input":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":1676489717,"blockNumber":0,"payload":"0x696e7075742d30"}}} \ No newline at end of file +{"data":{"input":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":"1676489717","blockNumber":"0","payload":"0x696e7075742d30"}}} \ No newline at end of file diff --git a/offchain/graphql-server/tests/responses/inputs.json b/offchain/graphql-server/tests/responses/inputs.json index 8f27b636..69fc4410 100644 --- a/offchain/graphql-server/tests/responses/inputs.json +++ b/offchain/graphql-server/tests/responses/inputs.json @@ -1 +1 @@ -{"data":{"inputs":{"totalCount":1,"edges":[{"node":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":1676489717,"blockNumber":0,"payload":"0x696e7075742d30"}}]}}} \ No newline at end of file +{"data":{"inputs":{"totalCount":1,"edges":[{"node":{"index":0,"msgSender":"0x6d73672d73656e646572","timestamp":"1676489717","blockNumber":"0","payload":"0x696e7075742d30"}}]}}} \ No newline at end of file