Add BigInt and BigDecimal GraphQL scalars (#452)

This commit is contained in:
prathamesh0 2023-11-06 15:00:24 +05:30 committed by GitHub
parent 8a720ef175
commit d5c92aa150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 21 deletions

View File

@ -49,7 +49,6 @@
"@cerc-io/graph-node": "^0.2.67",
{{/if}}
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1",
"decimal.js": "^10.3.1",
"ethers": "^5.4.4",

View File

@ -3,13 +3,8 @@
//
import assert from 'assert';
import BigInt from 'apollo-type-bigint';
import debug from 'debug';
import Decimal from 'decimal.js';
import {
GraphQLScalarType,
GraphQLResolveInfo
} from 'graphql';
import { GraphQLResolveInfo } from 'graphql';
import {
{{#if queries}}
@ -19,6 +14,8 @@ import {
gqlQueryCount,
getResultState,
IndexerInterface,
GraphQLBigInt,
GraphQLBigDecimal,
{{#if (subgraphPath)}}
BlockHeight,
jsonBigIntStringReplacer,
@ -45,20 +42,9 @@ export const createResolvers = async (indexerArg: IndexerInterface, eventWatcher
const gqlCacheConfig = indexer.serverConfig.gqlCache;
return {
BigInt: new BigInt('bigInt'),
BigInt: GraphQLBigInt,
BigDecimal: new GraphQLScalarType({
name: 'BigDecimal',
description: 'BigDecimal custom scalar type',
parseValue (value) {
// value from the client
return new Decimal(value);
},
serialize (value: Decimal) {
// value sent to the client
return value.toFixed();
}
}),
BigDecimal: GraphQLBigDecimal,
Event: {
__resolveType: (obj: any) => {

View File

@ -21,6 +21,7 @@
"apollo-server-core": "^3.11.1",
"apollo-server-express": "^3.11.1",
"apollo-server-plugin-response-cache": "^3.8.1",
"apollo-type-bigint": "^0.1.3",
"bunyan": "^1.8.15",
"debug": "^4.3.1",
"decimal.js": "^10.3.1",

View File

@ -9,7 +9,8 @@ import { hideBin } from 'yargs/helpers';
import { utils, providers } from 'ethers';
import JSONbig from 'json-bigint';
import Decimal from 'decimal.js';
import { GraphQLResolveInfo } from 'graphql';
import ApolloBigInt from 'apollo-type-bigint';
import { GraphQLResolveInfo, GraphQLScalarType, ValueNode } from 'graphql';
import _ from 'lodash';
import { DEFAULT_CONFIG_PATH } from './constants';
@ -305,3 +306,55 @@ export const setGQLCacheHints = (info: GraphQLResolveInfo, block: BlockHeight, g
info.cacheControl.setCacheHint({ maxAge });
};
class GraphQLBigIntType extends ApolloBigInt {
constructor () {
super('bigInt');
}
name = 'BigInt';
description = 'BigInt custom scalar type';
parseLiteral = function (ast: ValueNode) {
if (ast.kind === 'IntValue' || ast.kind === 'StringValue') {
return global.BigInt(ast.value);
} else {
throw new TypeError(`BigInt cannot represent value kind: ${ast.kind}`);
}
};
parseValue = function (value: any) {
if (value === '') {
throw new TypeError('The value cannot be converted from BigInt because it is empty string');
}
if (typeof value !== 'number' && typeof value !== 'bigint' && typeof value !== 'string') {
throw new TypeError(
`The value ${value} cannot be converted to a BigInt because it is not an integer`
);
}
try {
return global.BigInt(value);
} catch {
throw new TypeError(
`The value ${value} cannot be converted to a BigInt because it is not an integer`
);
}
};
}
export const GraphQLBigInt = new GraphQLBigIntType();
export const GraphQLBigDecimal = new GraphQLScalarType({
name: 'BigDecimal',
description: 'BigDecimal custom scalar type',
parseValue (value) {
// value from the client
return new Decimal(value);
},
serialize (value: Decimal) {
// value sent to the client
return value.toFixed();
}
});

View File

@ -4703,6 +4703,11 @@ apollo-server-types@^3.8.0:
apollo-reporting-protobuf "^3.4.0"
apollo-server-env "^4.2.1"
apollo-type-bigint@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/apollo-type-bigint/-/apollo-type-bigint-0.1.3.tgz#9242115ca909b9467ba5c4bc6493a56a06984c0b"
integrity sha512-nyfwEWRZ+kon3Nnot20DufGm2EHZrkJoryYzw3soD+USdxhkcW434w1c/n+mjMLQDl86Z6EvlkvMX5Lordf2Wg==
app-root-path@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/app-root-path/-/app-root-path-3.0.0.tgz"