-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Scalar aliases #308
Conversation
This adds the type definition syntax to the GraphQL specification.
I really like the idea. Though, I think, scalar aliases are not the right type of information to expose. Scalar aliases are extremely helpful for type-safe languages, but this is not necessarily the concept one want to expose. For example, we have a When it comes to scalars, I think it is quite helpful to make a distinction between syntax and semantic elements:
For example, enum __Literal {
BOOLEAN
NUMBER
STRING
}
extend type __Type {
literals: [__Literal!]
} It is a list because scalars like |
I've worked through an implementation of this idea in graphql/graphql-js#914 and quite like it! Thanks for proposing some spec text! (Though note the base branch should be @OlegIlyenko I'd like to dig a bit more into this. I originally came to the same conclusion that you did in that we should consider these in terms of literals, but I was convinced otherwise when presented this same problem by @dschafer as the real value extracted by doing this is to allow clients to understand the serialized response. In that sense, we do actually want to consider this in terms of the semantic scalar values. You could think of the examples from the Github schema which @IvanGoncharov mentioned as constrained scalars. That is, a These custom scalars defined in terms of existing scalars should also guarantee to play by the same rules that the built-in scalars do. I think the case where a valid literal that is an invalid scalar (like |
I think this idea deserves some bikeshedding for naming. "Alias" implies that the newly defined type is the same as built-in it's based on. I don't think that's quite right. A |
Maybe it's a difference between a scalar type "encoding" and the type itself. Maybe it's a case of extension? scalar URI implements String |
Encoding is better name for this. I'd like to avoid overriding the |
Joining the bikesheding committee: scalar URI extends String |
@leebyron agree, I think it is a very good point. There is a lot of value in knowing not only the literal representation but also some semantic information. I think the only place where it makes sense to define a completely custom scalar values, as you mentioned, is a number. It should be possible to represent |
817ee31
to
3044621
Compare
3044621
to
c717399
Compare
I opened an RFC which adds this (except for the schema language addition) to #326 The reference implementation is at graphql/graphql-js#914 (which currently uses Please let me know what you think of these - if there's consensus then I'll get to merging |
I like that GraphQL allows creating custom scalars. It gives great flexibility.
However, usually, they are used just to have more descriptive type name or to attach additional validation to the type.
Here is the list of custom scalars used by public GitHub API:
But in the response, they all are represented exactly same as
String
type and you can't deduce that from introspection. This creates problems for tooling. For example, in code generation, there is no way to reliably map custom scalar to the native type, e.g. issue on apollo ios. The same issue affects tools for mocking GraphQL APIs: issue on graphql-faker.These are only two examples from the top of my head.
@OlegIlyenko has added a partial solution to this issue in Sangria which allows you to define scalar alias as Scala object:
However, these aliases don't appear in introspection so all the semantic is lost for the clients. Also, there is no way to use it in IDL.
In general, full support for custom scalars is pretty complicated because of their flexibility and this PR doesn't try to solve this. However, in the majority of cases, custom scalars can be replaced with scalar aliases.
Note: as far as I understand, no breaking changes are introduced by this PR.