The ethers.js
library provides the BigNumber
type. To create a BigNumber
from a string
we call BigNumber.from("42")
I have created aGraphQLScalar
called GraphQLBignumber
on my server:
import { GraphQLScalarType } from 'graphql';
import { BigNumber } from 'ethers';
const EthersBigNumberType = new GraphQLScalarType({
name: 'BigNumber',
description: 'A custom scalar type for representing ethers.js BigNumber values',
parseValue(value: string | number | BigNumber) {
return BigNumber.from(value);
},
serialize(value: BigNumber) {
// Serialize the BigNumber instance into a string
return value.toString();
},
parseLiteral(ast) {
// Parse the ast value into a BigNumber instance
if (ast.kind === Kind.STRING || ast.kind === Kind.INT || ast.kind === Kind.FLOAT) {
return BigNumber.from(ast.value);
}
return null;
},
});
Now I would like to use this BigNumber
scalar on my client using graphql-codegen
. In my codegen.yml
I add:
plugins:
// other plugins incl typescript etc.
- 'add':
content: 'import {BigNumber} from "ethers"'
scalars:
BigNumber: 'ethers.BigNumber'
Now if I have the query:
query GetBalance($address: String!) {
balance(address: $address)
}
And I use codegen to generate the code, the output of GetBalanceQuery
returns an ethers.BigNumber
When I use my application I am not getting any errors (so far) and everything seems to be working just fine.
My question is: How does my client application know how to convert the server response in to a ethers.BigNumber? As far as I can tell the client does not have a copy of the parseValue
function yet the server is sending the BigNumber
as a string so somewhere BigNumber.from(...)
needs to be called...