GithubHelp home page GithubHelp logo

graphql-type-long's Introduction

graphql-type-long

Enable 52-bit signed integer types for GraphQL.js

Usage

import GraphQLLong from 'graphql-type-long';

graphql-type-long's People

Contributors

chadlieberman avatar markbirbeck avatar m14t avatar marvin-j97 avatar

Stargazers

netop://ウエハ avatar Kartik Shukla avatar  avatar Stephan Müller avatar Jeff Fairley avatar izure avatar RAMPKORV avatar  avatar iam4x avatar William Dias avatar bekusib avatar Mauro avatar Jordan Jones avatar kyle avatar Adrian Smijulj avatar LiJunLong avatar HollenZhao avatar Isac Fadoni avatar Artur avatar Nabeel Z avatar krinoid avatar Rubens F. avatar Adam Weber avatar  avatar Vít Rozsíval avatar  avatar Robert Luong avatar

Watchers

 avatar Sean Robertson avatar  avatar

graphql-type-long's Issues

Support graphql 14/15

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

Using graphql-type-long on Cached objects

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)

GraphQL should be a peer dependency not a dependency

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.

Error: Can only create NonNull of a Nullable GraphQLType but got: Long

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)
>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.