Search code examples
node.jsreactjsgraphqlapollo

How to query Java data types (long, short, BigDecimal) with GraphQl in a React/Node app


I'm trying to get data values types that are proper to Java, like long, short, BigDecimal, double but GraphQl does not recognize them, it understand only String, Int, Float, Boolean, ID. How can make those queries work with these data values type??

const { gql } = require("apollo-server");

const typeDefs = gql`

  type SignalValue {
    tickId: Int
    instId: Float
    tradingDay: String
    sigId: Int
    BigDecimal: Float
  }
//Accepted data values type


  type TotalPnl {
    floatingPnl: short
    updateTimestamp: long
    totalFees: BigDecimal
    winRatio: double
  }`
//Refused data values type

Here is the terminal error

/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/graphql/validation/validate.js:108
    throw new Error(errors.map(function (error) {
    ^

Error: Unknown type "short".
    at assertValidSDL (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/graphql/validation/validate.js:108:11)
    at Object.buildASTSchema (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/graphql/utilities/buildASTSchema.js:71:34)
    at Object.buildSchemaFromTypeDefinitions (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/graphql-tools/dist/generate/buildSchemaFromTypeDefinitions.js:25:28)
    at Object.makeExecutableSchema (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/graphql-tools/dist/makeExecutableSchema.js:26:29)
    at ApolloServer.initSchema (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/apollo-server-core/dist/ApolloServer.js:272:49)
    at new ApolloServerBase (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/apollo-server-core/dist/ApolloServer.js:199:30)
    at new ApolloServer (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/apollo-server-express/dist/ApolloServer.js:59:9)
    at new ApolloServer (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/node_modules/apollo-server/dist/index.js:24:9)
    at Object.<anonymous> (/Users/yohav/Desktop/monitorgui2/Apollo-NodeJS-Gateway/index.js:9:16)


Solution

  • GraphQL is a strongly-typed language. That means a type must first be defined inside the schema before it can be referenced (whether inside the schema itself or inside queries made by clients that are executed against that schema). The only exception to this rule are the five built-in scalars: Int, Float, String, Boolean and ID. Additional scalars beyond these five may be used inside the schema, but they must first be defined like any other type.

    scalar BigInt
    

    GraphQL has no way to know how custom scalar values like these should behave when used as inputs or outputs, so we also need a way to define that behavior. If you're using Apollo Server, we do this by initializing an instance of the GraphQLScalarType class and defining the serialize, parseValue and parseLiteral methods for the type. The custom scalar can than be passed in to the schema through the resolver map as shown here.

    If you need to utilize a custom scalar, chances are it's already available as a module you can import, so you usually don't have to write custom scalars yourself.