RFC: Define custom scalars in terms of built-in scalars. #1552
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As @leebyron suggested during last WG I'm adopting #914 and rebasing it on top of current
master
.Here is the original description:
This proposes an additive change which allows custom scalars to be defined in terms of the built-in scalars. The motivation is for client-side code generators to understand how to map between the GraphQL type system and a native type system. As an example, a
URL
custom type may be defined in terms of the built-in scalarString
. It could define additional serialization and parsing logic, however client tools can know to treatURL
values asString
. Presently, we do this by defining these mappings manually on the client, which is difficult to scale, or by giving up and making no assumptions of how the custom types serialize.Another real use case of giving client tools this information is GraphiQL: this change will allow GraphiQL to show more useful errors when a literal of an incorrect kind is provided to a custom scalar. Currently GraphiQL simply accepts all values.
To accomplish this, this proposes adding the following:
A new property when defining
GraphQLScalarType
(ofType
) which asserts that only built-in scalar types are provided.A second type coercion to guarantee to a client that the serialized values match the
ofType
.Delegating the
parseLiteral
andparseValue
functions to those inofType
(this enables downstream validation / GraphiQL features)Exposing
ofType
in the introspection system, and consuming that introspection inbuildClientSchema
.Adding optional syntax to the SDL, and consuming that in
buildASTSchema
andextendSchema
as well as inschemaPrinter
.Adding a case to
findBreakingChanges
which looks for a scalar's ofType changing.