5
5
[ ![ Latest Snapshot] ( https://img.shields.io/maven-central/v/com.graphql-java/graphql-java-extended-scalars?label=maven-central%20snapshot )] ( https://maven-badges.herokuapp.com/maven-central/com.graphql-java/graphql-java-extended-scalars/ )
6
6
[ ![ MIT licensed] ( https://img.shields.io/badge/license-MIT-green )] ( https://github.com/graphql-java/graphql-java-extended-scalars/blob/master/LICENSE.md )
7
7
8
-
9
8
This library provides extended scalars for [ graphql-java] ( https://github.com/graphql-java/graphql-java )
10
9
11
10
Scalars in graphql are the leaf nodes of a query, the non-compound values that can't be queried further via sub-field selections.
12
11
13
- The graphql standard specifies that the ` String ` , ` Int ` , ` Float ` , ` Boolean ` and ` ID ` scalars must be present in a graphql type
12
+ The graphql standard specifies that the ` String ` , ` Int ` , ` Float ` , ` Boolean ` and ` ID ` scalars must be present in a graphql type
14
13
system but after that it is up to an implementation about what custom scalars are present.
15
14
16
15
You would use custom scalars when you want to describe more meaningful behavior or ranges of values.
17
16
18
17
## How to install
18
+
19
19
To use this library put the following into your gradle config
20
20
21
21
implementation 'com.graphql-java:graphql-java-extended-scalars:20.0'
22
-
22
+
23
23
or the following into your Maven config
24
24
25
25
<dependency>
@@ -31,17 +31,17 @@ or the following into your Maven config
31
31
> Note:
32
32
>
33
33
> use 19.0 or above for graphql-java 19.x and above
34
- >
34
+ >
35
35
> use 20.0 or above for graphql-java 20.x and above
36
36
37
-
38
37
It's currently available from Maven Central.
39
38
40
39
## How to use extended scalars
40
+
41
41
Register the scalar with graphql-java
42
42
43
43
RuntimeWiring.newRuntimeWiring().scalar(ExtendedScalars.DateTime)
44
-
44
+
45
45
Or if using [ Spring for GraphQL] ( https://docs.spring.io/spring-graphql/docs/current/reference/html/ ) , register the scalar with ` RuntimeWiringConfigurer `
46
46
47
47
@Configuration
@@ -53,172 +53,157 @@ Or if using [Spring for GraphQL](https://docs.spring.io/spring-graphql/docs/curr
53
53
}
54
54
55
55
And use the scalar in your schema
56
-
56
+
57
57
scalar DateTime
58
58
type Something {
59
59
someDateTime: DateTime
60
60
}
61
61
62
62
## DateTime Scalars
63
63
64
- * ` DateTime `
65
- * An RFC-3339 compliant date time scalar that accepts string values like ` 1996-12-19T16:39:57-08:00 ` and produces
66
- ` java.time.OffsetDateTime ` objects at runtime
67
- * ` Time `
68
- * An RFC-3339 compliant time scalar that accepts string values like ` 16:39:57-08:00 ` and produces
69
- ` java.time.OffsetTime ` objects at runtime
70
- * ` LocalTime `
71
- * 24-hour clock time string in the format ` hh:mm:ss.sss ` or ` hh:mm:ss ` if partial seconds is zero and
72
- produces ` java.time.LocalTime ` objects at runtime.
73
- * ` Date `
74
- * An RFC-3339 compliant date scalar that accepts string values like ` 1996-12-19 ` and produces
75
- ` java.time.LocalDate ` objects at runtime
76
-
77
- See [ the rfc3339 spec] ( https://www.ietf.org/rfc/rfc3339.txt ) for more details on the format.
64
+ - ` DateTime ` - [ specification] ( https://scalars.graphql.org/andimarek/date-time.html )
65
+ - See specification
66
+ - An RFC-3339 compliant date time scalar that accepts string values like ` 1996-12-19T16:39:57-08:00 ` and produces
67
+ ` java.time.OffsetDateTime ` objects at runtime.
68
+ - ` Time `
69
+ - An RFC-3339 compliant time scalar that accepts string values like ` 16:39:57-08:00 ` and produces
70
+ ` java.time.OffsetTime ` objects at runtime
71
+ - ` LocalTime `
72
+ - 24-hour clock time string in the format ` hh:mm:ss.sss ` or ` hh:mm:ss ` if partial seconds is zero and
73
+ produces ` java.time.LocalTime ` objects at runtime.
74
+ - ` Date `
75
+ - An RFC-3339 compliant date scalar that accepts string values like ` 1996-12-19 ` and produces
76
+ ` java.time.LocalDate ` objects at runtime
78
77
79
78
An example declaration in SDL might be:
80
79
81
80
``` graphql
82
-
83
- type Customer {
84
- birthDay : Date
85
- workStartTime : Time
86
- bornAt : DateTime
87
- }
88
-
89
- type Query {
90
- customers (bornAfter : DateTime) : [Customers ]
91
- }
92
-
93
- ```
81
+ type Customer {
82
+ birthDay : Date
83
+ workStartTime : Time
84
+ bornAt : DateTime
85
+ }
86
+
87
+ type Query {
88
+ customers (bornAfter : DateTime ): [Customers ]
89
+ }
90
+ ```
94
91
95
92
And example query might look like :
93
+
96
94
```graphql
97
-
98
- query {
99
- customers (bornAfter : " 1996-12-19T16:39:57-08:00" ) {
100
- birthDay
101
- bornAt
102
- }
103
- }
104
-
105
- ```
95
+ query {
96
+ customers (bornAfter : " 1996-12-19T16:39:57-08:00" ) {
97
+ birthDay
98
+ bornAt
99
+ }
100
+ }
101
+ ```
102
+
106
103
## ID Scalars
107
104
108
- * ` UUID `
109
- * A universally unique identifier scalar that accepts uuid values like ` 2423f0a0-3b81-4115-a189-18df8b35e8fc ` and produces
105
+ - ` UUID `
106
+ - A universally unique identifier scalar that accepts uuid values like ` 2423f0a0-3b81-4115-a189-18df8b35e8fc ` and produces
110
107
` java.util.UUID ` instances at runtime.
111
108
112
109
## Object / JSON Scalars
113
110
114
- * ` Object `
115
- * An object scalar that accepts any object as a scalar value
111
+ - ` Object `
112
+
113
+ - An object scalar that accepts any object as a scalar value
114
+
115
+ - ` JSON `
116
+ - A synonym for the ` Object ` scalar, it will accept any object as a scalar value
116
117
117
- * ` JSON `
118
- * A synonym for the ` Object ` scalar, it will accept any object as a scalar value
119
-
120
118
One of the design goals of graphql, is that the type system describes the shape of the data returned.
121
119
122
- The ` Object ` / ` JSON ` scalars work against this some what because they can return compound values outside the type system. As such
123
- they should be used sparingly. In general your should aim to describe the data via the graphql type system where you can and only
124
- resort to the ` Object ` / ` JSON ` scalars in very rare circumstances.
120
+ The ` Object ` / ` JSON ` scalars work against this some what because they can return compound values outside the type system. As such
121
+ they should be used sparingly. In general your should aim to describe the data via the graphql type system where you can and only
122
+ resort to the ` Object ` / ` JSON ` scalars in very rare circumstances.
125
123
126
- An example might be an extensible graphql system where systems can input custom metadata objects that cant be known at
124
+ An example might be an extensible graphql system where systems can input custom metadata objects that cant be known at
127
125
schema type design time.
128
126
129
127
An example declaration in SDL might be:
130
128
131
129
``` graphql
132
-
133
- type Customer {
134
- name : String
135
- associatedMetaData : JSON
136
- }
137
-
138
- type Query {
139
- customers (filterSyntax : JSON) : [Customers ]
140
- }
141
-
142
- ```
130
+ type Customer {
131
+ name : String
132
+ associatedMetaData : JSON
133
+ }
134
+
135
+ type Query {
136
+ customers (filterSyntax : JSON ): [Customers ]
137
+ }
138
+ ```
143
139
144
140
And example query might look like :
145
141
146
142
```graphql
147
-
148
- query {
149
- customers (filterSyntax : {
150
- startSpan : " First" ,
151
- matchCriteria : {
152
- countryCode : " AU" ,
153
- isoCodes : ["27B-34R" , " 95A-E23" ],
154
-
155
- }
156
- }) {
157
- name
158
- associatedMetaData
159
- }
143
+ query {
144
+ customers (
145
+ filterSyntax : {
146
+ startSpan : " First"
147
+ matchCriteria : { countryCode : " AU" , isoCodes : ["27B-34R" , " 95A-E23" ] }
160
148
}
161
-
162
- ```
163
-
164
- 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
165
- all just objects at runtime in graphql-java terms and what network serialisation protocol is up to you. Choose whichever name you think
166
- adds more semantic readers to your schema consumers.
149
+ ) {
150
+ name
151
+ associatedMetaData
152
+ }
153
+ }
154
+ ```
167
155
156
+ 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
157
+ all just objects at runtime in graphql-java terms and what network serialisation protocol is up to you. Choose whichever name you think
158
+ adds more semantic readers to your schema consumers.
168
159
169
160
## Numeric Scalars
170
161
171
-
172
- * ` PositiveInt `
173
- * An ` Int ` scalar that MUST be greater than zero
174
- * ` NegativeInt `
175
- * An ` Int ` scalar that MUST be less than zero
176
- * ` NonPositiveInt `
177
- * An ` Int ` scalar that MUST be less than or equal to zero
178
- * ` NonNegativeInt `
179
- * An ` Int ` scalar that MUST be greater than or equal to zero
180
- * ` PositiveFloat `
181
- * An ` Float ` scalar that MUST be greater than zero
182
- * ` NegativeFloat `
183
- * An ` Float ` scalar that MUST be less than zero
184
- * ` NonPositiveFloat `
185
- * An ` Float ` scalar that MUST be less than or equal to zero
186
- * ` NonNegativeFloat `
187
- * An ` Float ` scalar that MUST be greater than or equal to zero
162
+ - ` PositiveInt `
163
+ - An ` Int ` scalar that MUST be greater than zero
164
+ - ` NegativeInt `
165
+ - An ` Int ` scalar that MUST be less than zero
166
+ - ` NonPositiveInt `
167
+ - An ` Int ` scalar that MUST be less than or equal to zero
168
+ - ` NonNegativeInt `
169
+ - An ` Int ` scalar that MUST be greater than or equal to zero
170
+ - ` PositiveFloat `
171
+ - An ` Float ` scalar that MUST be greater than zero
172
+ - ` NegativeFloat `
173
+ - An ` Float ` scalar that MUST be less than zero
174
+ - ` NonPositiveFloat `
175
+ - An ` Float ` scalar that MUST be less than or equal to zero
176
+ - ` NonNegativeFloat `
177
+ - An ` Float ` scalar that MUST be greater than or equal to zero
188
178
189
179
The numeric scalars are derivations of the standard graphql ` Int ` and ` Float ` scalars that enforce range limits.
190
180
191
181
An example declaration in SDL might be:
192
182
193
183
``` graphql
194
-
195
- type Customer {
196
- name : String
197
- currentHeight : PositiveInt
198
- weightLossGoal : NonPositiveInt
199
- averageWeightLoss : NegativeFloat
200
- }
201
-
202
- type Query {
203
- customers (height : PositiveInt) : [Customers ]
204
- }
205
-
206
- ```
184
+ type Customer {
185
+ name : String
186
+ currentHeight : PositiveInt
187
+ weightLossGoal : NonPositiveInt
188
+ averageWeightLoss : NegativeFloat
189
+ }
190
+
191
+ type Query {
192
+ customers (height : PositiveInt ): [Customers ]
193
+ }
194
+ ```
207
195
208
196
And example query might look like :
209
197
210
198
```graphql
211
-
212
- query {
213
- customers (height : 182 ) {
214
- name
215
- height
216
- weightLossGoal
217
- }
218
- }
219
-
220
- ```
221
-
199
+ query {
200
+ customers (height : 182 ) {
201
+ name
202
+ height
203
+ weightLossGoal
204
+ }
205
+ }
206
+ ```
222
207
223
208
## Regex Scalars
224
209
@@ -239,89 +224,90 @@ For example, imagine a `phoneNumber` scalar like this :
239
224
240
225
## Locale Scalar
241
226
242
- The Locale scalar handles [ IETF BCP 47] ( https://tools.ietf.org/html/bcp47 ) language tags via the
243
- JDK method [ Locale.forLanguageTag] ( https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#forLanguageTag(java.lang.String) )
227
+ The Locale scalar handles [ IETF BCP 47] ( https://tools.ietf.org/html/bcp47 ) language tags via the
228
+ JDK method [ Locale.forLanguageTag] ( < https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#forLanguageTag(java.lang.String) > )
244
229
245
230
``` graphql
246
-
247
- type Customer {
248
- name : String
249
- locale : Locale
250
- }
251
-
252
- type Query {
253
- customers (inLocale : Locale) : [Customers ]
254
- }
255
- ```
231
+ type Customer {
232
+ name : String
233
+ locale : Locale
234
+ }
235
+
236
+ type Query {
237
+ customers (inLocale : Locale ): [Customers ]
238
+ }
239
+ ```
256
240
257
241
An example query to look for customers in the Romanian locale might look like :
258
242
259
243
```graphql
260
-
261
- query {
262
- customers (inLocale : " ro-RO" ) {
263
- name
264
- locale
265
- }
266
- }
267
-
268
- ```
244
+ query {
245
+ customers (inLocale : " ro-RO" ) {
246
+ name
247
+ locale
248
+ }
249
+ }
250
+ ```
251
+
269
252
## Country Code Scalar
270
- The CountryCode scalar type as defined by [ ISO 3166-1 alpha-2] ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ) .
271
253
254
+ The CountryCode scalar type as defined by [ ISO 3166-1 alpha-2] ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ) .
272
255
273
256
An example declaration in SDL might be:
257
+
274
258
``` graphql
275
- scalar CountryCode
259
+ scalar CountryCode
276
260
277
- type Customer {
278
- name : String
279
- countryCode : CountryCode
280
- }
281
- ```
261
+ type Customer {
262
+ name : String
263
+ countryCode : CountryCode
264
+ }
265
+ ```
282
266
283
267
And example query might look like :
284
268
285
269
```graphql
286
- query {
287
- customers (code : " US" ) {
288
- name
289
- countryCode
290
- }
291
- }
292
- ```
270
+ query {
271
+ customers (code : " US" ) {
272
+ name
273
+ countryCode
274
+ }
275
+ }
276
+ ```
277
+
293
278
## Currency Scalar
294
279
295
280
A field whose value is an [ ISO-4217] ( https://en.wikipedia.org/wiki/ISO_4217 ) currency.
296
281
297
282
An example declaration in SDL might be:
283
+
298
284
``` graphql
299
- scalar Currency
285
+ scalar Currency
300
286
301
- type Account {
302
- id : String
303
- currency : Currency
304
- accountNumber : String
305
- }
306
- ```
287
+ type Account {
288
+ id : String
289
+ currency : Currency
290
+ accountNumber : String
291
+ }
292
+ ```
307
293
308
294
And example query might look like :
309
295
310
296
```graphql
311
- query {
312
- accounts (currency : " USD" ) {
313
- id
314
- currency
315
- accountNumber
316
- }
317
- }
318
- ```
297
+ query {
298
+ accounts (currency : " USD" ) {
299
+ id
300
+ currency
301
+ accountNumber
302
+ }
303
+ }
304
+ ```
319
305
320
306
## Alias Scalars
321
307
322
308
You can create aliases for existing scalars to add more semantic meaning to them.
323
309
324
- For example a link to a social media post could be representing by a ` String ` but the name ` SocialMediaLink ` is a
310
+ For example a link to a social media post could be representing by a ` String ` but the name ` SocialMediaLink ` is a
325
311
more semantically meaningful name for that scalar type.
326
312
327
313
For example, you would build it like this:
@@ -337,35 +323,31 @@ For example, you would build it like this:
337
323
And use it in a SDL schema like this :
338
324
339
325
``` graphql
340
-
341
- type Customer {
342
- name : String
343
- socialMediaLink : SocialMediaLink
344
- }
345
-
326
+ type Customer {
327
+ name : String
328
+ socialMediaLink : SocialMediaLink
329
+ }
346
330
```
347
331
348
332
Note : A future version of the graphql specification may add this capability but in the meantime you can use this facility .
349
333
350
334
## Java Primitives
351
- * `GraphQLLong `
352
- * A scalar which represents `java .lang .Long `
353
- * `GraphQLShort `
354
- * A scalar which represents `java .lang .Short `
355
- * `GraphQLByte `
356
- * A scalar which represents `java .lang .Byte `
357
- * `GraphQLBigDecimal `
358
- * A scalar which represents `java .math .BigDecimal `
359
- * `GraphQLBigInteger `
360
- * A scalar which represents `java .math .BigInteger `
361
- * `GraphQLChar `
362
- * A scalar which represents `java .lang .Character `
363
335
336
+ - `GraphQLLong `
337
+ - A scalar which represents `java .lang .Long `
338
+ - `GraphQLShort `
339
+ - A scalar which represents `java .lang .Short `
340
+ - `GraphQLByte `
341
+ - A scalar which represents `java .lang .Byte `
342
+ - `GraphQLBigDecimal `
343
+ - A scalar which represents `java .math .BigDecimal `
344
+ - `GraphQLBigInteger `
345
+ - A scalar which represents `java .math .BigInteger `
346
+ - `GraphQLChar `
347
+ - A scalar which represents `java .lang .Character `
364
348
365
349
## Other Scalars
366
350
367
- * `Url `
368
- * An url scalar that accepts string values like `https ://www .w3 .org /Addressing /URL /url -spec .txt ` and produces
369
- `java .net .URL ` objects at runtime
370
-
371
-
351
+ - `Url `
352
+ - An url scalar that accepts string values like `https ://www .w3 .org /Addressing /URL /url -spec .txt ` and produces
353
+ `java .net .URL ` objects at runtime
0 commit comments