GraphQL custom scalar types

Hello,

Dgraph has several custom scalar types implemented for GraphQL such as DateTime, Polygon and Int64.

But there’s no ability yet to specify additional custom scalar types (and resolvers/ serialization/ validation).

The most common custom scalar types I have used so far were:

  • Email,
  • URI, DOI, ISRC (music records), UPC (trade items)
  • PhoneNumber
  • Time, Date, DateRange
  • TimeCode
  • Bézier
  • Symbol (e.g. for a single Emoji)
  • Coordinates {x: …,y: …, z: …}
  • Colors: RGB(A), HEX, HSL(A),
  • Array (with preserved order and duplicates),
  • JSON

Of course one can always encode as a string. But what I love about Dgraph ist that it can drastically simplify my business logic and I can use it as an all-in-one backend service.

An alternative to custom scalar types would be the addition of @constraint directives for validation:

email: String! @constraint(format: "email", maxLength: 255)

As far as I understand custom remote fields or lambda fields are the recommended (?) workaround in Dgraph (without search however).

3 Likes

Great idea! But yes, that is the only workaround the way DGraph currently stands.

J

+1 for this functionality.
It should be easy to implement just to rename string to other scalar type but then when it is exposed by schema I could teach my client library to parse this scalar type to something more meaningful.

Any updates? :slightly_smiling_face: