Enable 52-bit signed integer types for GraphQL.js
import GraphQLLong from 'graphql-type-long';
Enable 52-bit signed integer types for GraphQL
Enable 52-bit signed integer types for GraphQL.js
import GraphQLLong from 'graphql-type-long';
This package has a peer dependency on graphql 13, but we are already on graphql 15. This needs to be tested on 15. There is already a PR here for 14: #6
Need help in using graphql-type-long on Cached (JSON.stringify()) and deserialized from Cache (JSON.parse()) objects.
Since Javascript does not natively support 64-bit Long. we use long package to represent 64-bit long returned from thrift*. long
is an object of type {low:int, high:int, unsigned:boolean}
GraphQL requires all longs to be converted to Number (53-bit). So, in a GraphQL schema we represent Long's as Scalar with GraphQLLong for conversion from long
to Number.
This works fine when a long
is obtained from downstream thrift because toNumber() method is attached to it by deserialization code.
However, when the downstream response is cached (serialized using JSON.stringify() and later deserialized using JSON.parse()), it doesn't have the methods such as toNumber() attached to it. This causes an exception "Long cannot represent non 52-bit signed integer value: [object Object]" thrown by graphql-type-long package.
To use both caching and supporting 52-bit signed integers, we tried explicitly formatting the field in GraphQL schema as below
import Long from 'long';
function getNumberFromLong(obj) {
if (!Long.isLong(obj)) {
const longNumber = Long.fromBits(obj.low, obj.high, obj.unsigned);
return longNumber.toNumber();
}
return obj.toNumber();
}
// ...
field1: {
resolve(contactType) {
return getNumberFromLong(o.field1);
},
},
What is the right way to use graphql-type-long on objects deserialized from Cache?
(*) thrift natively supports 64-bit Long. On wire, a long (in binary) is of the triplet form (type, id, value). Ex: (0x0a, 0x0007, 0x0000_0000_0000_0000)
This module is not 'dependent' on GraphQL as such but should be installed alongside it. In many situations it won't make much difference, but if this module is used in a module which itself is then used in another module which includes GraphQL, all bets are off, and we get the following error:
Error: Cannot use GraphQLSchema "[object Object]" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
at instanceOf (/usr/local/lib/node_modules/graphql/jsutils/instanceOf.js:15:13)
at isSchema (/usr/local/lib/node_modules/graphql/type/schema.js:52:35)
at validateSchema (/usr/local/lib/node_modules/graphql/type/validate.js:55:25)
at assertValidSchema (/usr/local/lib/node_modules/graphql/type/validate.js:80:16)
at Object.validate (/usr/local/lib/node_modules/graphql/validation/validate.js:58:35)
at doRunQuery (/usr/local/lib/node_modules/graphql-server-express/node_modules/apollo-server-core/dist/runQuery.js:110:38)
at /usr/local/lib/node_modules/graphql-server-express/node_modules/apollo-server-core/dist/runQuery.js:21:56
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
The solution is simply to ensure that modules that relate to GraphQL use peerDependencies
rather than dependencies
in the package.json
file.
Fails with [email protected]
Worked ok with [email protected]
# node
> const GraphQLLong = require('graphql-type-long');
undefined
> const {GraphQLNonNull} = require('graphql');
undefined
> new GraphQLNonNull(GraphQLLong)
Error: Can only create NonNull of a Nullable GraphQLType but got: Long.
at invariant (/node_modules/graphql/jsutils/invariant.js:19:11)
at new GraphQLNonNull (/node_modules/graphql/type/definition.js:769:29)
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:433:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
>
<3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.