Long Custom Scalar

Note This document is a DRAFT. It is being published to solicit feedback. It is a “live document” that can change whenever feedback is given and accepted.

This draft is © Copyright IBM Corp. 2021.

1Goals

This specification defines the custom scalar type Long that is useful for dealing with integers larger than those handled by Int.

Note: All mentions of Long are referring to the scalar type defined by this specification. All mentions of Long are referring to user declared scalar name.

2Definition

3Example of the Long Type

The following example uses Long:

Example № 1scalar LargeNumber @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/long.html")
scalar Long
scalar UnknownScalar

type Asguardian {
  name: String
  age: LargeNumber
}

type Query {
  asguardians(max: Long = 4000000000000): [Asguardian]
}

In the example above, the following points should be noted:

This is a simple example query that matches the schema:

Example № 2Query example {
  asguardians(max: 3000000000000) {
    name
    age
  }
}

4Customizing the Long Type

The lower and upper bounds of Long can be changed via the @scalarParam directive as defined here. The parameters used in the @scalarParam directive to specify the bounds are:

The following conditions apply to range modification:

5Example of Long Type Customization

The following example demonstrates how to modify the range of Long using the @scalarParam directive:

Example № 3directive @scalarParam (name: String!, value: String!) repeatable on SCALAR
scalar LargeNumber @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/long.html")
@scalarParam (name: "min", value: "0")
@scalarParam (name: "max", value: "5000000000000")
scalar Long @scalarParam (name: "max", value: "4000000000000")

type Asguardian {
  name: String
  age: LargeNumber
}

type Query {
  asguardians(max: Long = 4000000000000): [Asguardian] @listSize(slicingArguments: ["max"])
}

In this example, please note:

6Result Coercion

Fields returning the type Long expect to encounter 64-bit integer internal values.

All integer values must coerce to the equivalent Long value.

GraphQL services may coerce non-integer internal values to Long when reasonable without losing information, otherwise they must raise a field error. Examples of this may include returning 1 for the floating-point number 1.0, or returning 123 for the string "123". In scenarios where coercion may lose data, a field error should be raised. For example, the floating-point number 1.2 should raise a field error instead of being truncated to the Long value 1.

If the integer internal value represents a value less than -9223372036854775808 or greater than 9223372036854775807, a field error should be raised.

7Input Coercion

As an input type, only valid integers within the range of Long are accepted. All values that are not valid Long, including numeric strings, must raise a request error indicating an incorrect type.

  1. 1Goals
  2. 2Definition
  3. 3Example of the Long Type
  4. 4Customizing the Long Type
  5. 5Example of Long Type Customization
  6. 6Result Coercion
  7. 7Input Coercion