graphql / graphql/graphql-spec

Typed Scalars

Open
#990 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

# Typed Scalars

Some time before i started writing subset language of GQL (with name [iris](https://github.com/iris-qraphql/iris-js)) which would take best parts of GQL and simplify and generalize it. although it made a lot of fun writing it, i think best way to solve problems would be to introduce this features directly in GQL, one of which is typed scalars (with name `data`).

## motivation

- support [input unions](https://github.com/graphql/graphql-wg/blob/main/rfcs/InputUnion.md) with non breaking changes
- support tree types (see [why GQL needs typed scalars](https://github.com/iris-qraphql/iris-spec/blob/main/why-gql-needs-typed-scalars.md))
- GQL can be used for `server-to-server` requests (like `RPC`)

hypothetically it can make `enums/inputObjects` redundant and reduce schema complexity.

I know the requirement for new features in the language is high, but I think this feature can be beneficial and removes unnecessary limitations for the language. for now, instead of modeling tree types in GQL, people either go to another language or model them with untyped JSON objects, which is not such a good option. same problem is with input unions.

## definition

core idea of typed scalars is to extend GQL type system with typed JSON objects. this new kind of `data` types will not resolve any value (no dedicated type resolvers) but only check their validity.

in my opinion use of algebraic data types for their definitions would be most suitable (syntax is described in language definition [iris](https://github.com/iris-qraphql/iris-spec)).

```gql
data Image = { src: String! } # standalone variant so called "variant type"

data RichText
= NewLine {} # can accept string literal "NewLine" or {__typename:"NewLine"}
| Text { text : String! } # inline variant is only accessable inside Richtext
| Node { children: [RichText!]! } # inline variant is only accessable inside Richtext
| Image # reference of variant type
```

since there is already implementation for this feature in [iris](https://github.com/iris-qraphql/iris-js), i could freely open PR for that.

#### non breaking

client with old GQL version will read it as scalar types with type definition as JSDoc annotations (PR in [apollo](https://github.com/apollographql/apollo-tooling/pull/2545)) in description. however for new clients it will provide field `dataVariants`: which will include description of variant definitions for the type.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.