Skip to content

Clarify relationship between custom scalars and serialization formats #337

Closed
@martijnwalraven

Description

@martijnwalraven

We've had some discussions about differing interpretations of serializing custom scalars, so I was wondering what other people's thoughts about this are.

Section 7.1 (Serialization Format) of the spec lists a number of required and optional primitives that may be used to serialize values:

[T]he serialization format must support representations of the following four primitives:

  • Map
  • List
  • String
  • Null

And:

A serialization format may support the following primitives, however, strings may be used as a substitute for those primitives.

  • Boolean
  • Int
  • Float
  • Enum Value

Is this meant as an exhaustive list? Or can serialization formats add support for additional primitives?

Because built-in scalars have a predefined mapping to one of the listed primitives, this only seems relevant for custom scalars.

So I think the question comes down to: are custom scalar values always eventually coerced into one of the primitives listed in the spec? Or can custom scalars be defined on top of arbitrary types, and leave the encoding to a serialization mechanism that comes after execution?

Specifically, this came up in the context of encoding Date and Time in CBOR. The idea would be that resolvers can return language-specific types (such as a JavaScript Date or java.time.Instant), and instead of defining a custom scalar that would coerce these to a string, it would leave them as is and allow the serialization format to choose a more specific encoding, that may be more efficient or more semantically correct. The downside of that seems to be that it forces serialization mechanisms to be aware of these additional types (so you may not be able to just use JSON.stringify in JavaScript for example), which may be a source of incompatibilities.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions