|
| 1 | +# Extended Scalars for graphql-java |
1 | 2 |
|
| 3 | +This library provides extended scalars for [graphql-java](https://github.com/graphql-java/graphql-java) |
2 | 4 |
|
3 | | -# Big work in PROGRESS |
| 5 | +Scalars in graphql are the leaf nodes of a query, the non compound values that cant be queried further via sub field selections. |
4 | 6 |
|
5 | | -Not released and not ready for external consumption yet! |
| 7 | +The graphql standard specifies `String`, `Int`, `Float`, `Boolean` and `ID` must be present in a graphql type |
| 8 | +system but after that it is up to an implementation about what custom scalars are present. |
6 | 9 |
|
7 | | -Still exploring what it could be |
| 10 | +You would use custom scalars when you want to describe more meaningful behavior or ranges of values. |
| 11 | + |
| 12 | +## DateTime Scalars |
| 13 | + |
| 14 | +* `DateTime` |
| 15 | + * An RFC-3339 compliant date time scalar that accepts string values like `1996-12-19T16:39:57-08:00` and produces |
| 16 | + `java.time.OffsetDateTime` objects at runtime |
| 17 | +* `Time` |
| 18 | + * An RFC-3339 compliant time scalar that accepts string values like `16:39:57-08:00` and produces |
| 19 | + `java.time.LocalDate` objects at runtime |
| 20 | +* `Date` |
| 21 | + * An RFC-3339 compliant date scalar that accepts string values like `1996-12-19` and produces |
| 22 | + `java.time.LocalDate` objects at runtime |
| 23 | + |
| 24 | +See [the rfc3339 spec](https://www.ietf.org/rfc/rfc3339.txt) for more details on the format. |
| 25 | + |
| 26 | +An example declaration in SDL might be: |
| 27 | + |
| 28 | +```graphql |
| 29 | + |
| 30 | + type Customer { |
| 31 | + birthDay : Date |
| 32 | + workStartTime : Time |
| 33 | + bornAt : DateTime |
| 34 | + } |
| 35 | + |
| 36 | + type Query { |
| 37 | + customers(bornAfter : DateTime) : [Customers] |
| 38 | + } |
| 39 | + |
| 40 | +``` |
| 41 | + |
| 42 | +And example query might look like: |
| 43 | +```graphql |
| 44 | + |
| 45 | + query { |
| 46 | + customers(bornAfter : "1996-12-19T16:39:57-08:00") { |
| 47 | + birthDay |
| 48 | + bornAt |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +## Object / Json Scalars |
| 55 | + |
| 56 | +* `Object` |
| 57 | + * An object scalar that accepts any object as a scalar value |
| 58 | + |
| 59 | +* `Json` |
| 60 | + * A synonym for the `Object` scalar, it will accept any object as a scalar value |
| 61 | + |
| 62 | +One of the design goals of graphql, is that the type system describes the shape of the data returned. |
| 63 | + |
| 64 | +The `Object` / `Json` scalars work against this some what because they can return compound values outside the type system. As such |
| 65 | +they should be used sparingly. In general your should aim to describe the data via the graphql type system where you can and only |
| 66 | +resort to the `Object` / `Json` scalars in very rare circumstances. |
| 67 | + |
| 68 | +An example might be an extensible graphql system where systems can input custom metadata objects that cant be known at |
| 69 | +schema type design time. |
| 70 | + |
| 71 | +An example declaration in SDL might be: |
| 72 | + |
| 73 | +```graphql |
| 74 | + |
| 75 | + type Customer { |
| 76 | + name : String |
| 77 | + associatedMetaData : Json |
| 78 | + } |
| 79 | + |
| 80 | + type Query { |
| 81 | + customers(filterSyntax : Json) : [Customers] |
| 82 | + } |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +And example query might look like: |
| 87 | + |
| 88 | +```graphql |
| 89 | + |
| 90 | + query { |
| 91 | + customers(filterSyntax : { |
| 92 | + startSpan : "First", |
| 93 | + matchCriteria : { |
| 94 | + countryCode : "AU", |
| 95 | + isoCodes : ["27B-34R", "95A-E23"], |
| 96 | + |
| 97 | + } |
| 98 | + }) { |
| 99 | + name |
| 100 | + associatedMetaData |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +Note : The `Json` scalar is a simple alias type to the `Object` scalar because often the returned data is a blob of JSON. They are |
| 107 | +all just objects at runtime in graphql-java terms and what network serialisation protocol is up to you. Choose whichever name you think |
| 108 | +adds more semantic readers to your schema consumers. |
| 109 | + |
| 110 | + |
| 111 | +## Numeric Scalars |
| 112 | + |
| 113 | + |
| 114 | +* `PositiveInt` |
| 115 | + * An `Int` scalar that MUST be a greater than zero |
| 116 | +* `NegativeInt` |
| 117 | + * An `Int` scalar that MUST be a less than zero |
| 118 | +* `NonPositiveInt` |
| 119 | + * An `Int` scalar that MUST be a less than or equal to zero |
| 120 | +* `NonNegativeInt` |
| 121 | + * An `Int` scalar that MUST be a greater than or equal to zero |
| 122 | +* `PositiveFloat` |
| 123 | + * An `Float` scalar that MUST be a greater than zero |
| 124 | +* `NegativeFloat` |
| 125 | + * An `Float` scalar that MUST be a less than zero |
| 126 | +* `NonPositiveFloat` |
| 127 | + * An `Float` scalar that MUST be a less than or equal to zero |
| 128 | +* `NonNegativeFloat` |
| 129 | + * An `Float` scalar that MUST be a greater than or equal to zero |
| 130 | + |
| 131 | +The numeric scalars are derivations of the standard graphql `Int` and `Float` scalars that enforce range limits. |
| 132 | + |
| 133 | +An example declaration in SDL might be: |
| 134 | + |
| 135 | +```graphql |
| 136 | + |
| 137 | + type Customer { |
| 138 | + name : String |
| 139 | + currentHeight : PositiveInt |
| 140 | + weightLossGoal : NonPositiveInt |
| 141 | + averageWeightLoss : NegativeFloat |
| 142 | + } |
| 143 | + |
| 144 | + type Query { |
| 145 | + customers(height : PositiveInt) : [Customers] |
| 146 | + } |
| 147 | + |
| 148 | +``` |
| 149 | + |
| 150 | +And example query might look like: |
| 151 | + |
| 152 | +```graphql |
| 153 | + |
| 154 | + query { |
| 155 | + customers(height : 182) { |
| 156 | + name |
| 157 | + height |
| 158 | + weightLossGoal |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +``` |
| 163 | + |
| 164 | + |
| 165 | +## Other Scalars |
| 166 | + |
| 167 | +* `Url` |
| 168 | + * An url scalar that accepts string values like `https://www.w3.org/Addressing/URL/url-spec.txt` and produces |
| 169 | + `java.net.URL` objects at runtime |
0 commit comments