Skip to content

Commit 77597c2

Browse files
smyrickShane Myrick
andauthored
Add docs for set support (#1111)
* Add docs for set support * Update lists.md * Update lists.md Co-authored-by: Shane Myrick <[email protected]>
1 parent a8b0801 commit 77597c2

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/hooks/CustomSchemaGeneratorHooks.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import java.math.BigDecimal
3232
import java.util.UUID
3333
import kotlin.reflect.KClass
3434
import kotlin.reflect.KType
35+
import kotlin.reflect.full.createType
3536
import kotlin.reflect.full.isSubclassOf
3637

3738
/**
@@ -52,9 +53,10 @@ class CustomSchemaGeneratorHooks(override val wiringFactory: KotlinDirectiveWiri
5253
* Register Reactor Mono monad type.
5354
*/
5455
override fun willResolveMonad(type: KType): KType = when (type.classifier) {
55-
Mono::class -> type.arguments.firstOrNull()?.type
56+
Mono::class -> type.arguments.first().type ?: type
57+
Set::class -> List::class.createType(type.arguments)
5658
else -> type
57-
} ?: type
59+
}
5860

5961
/**
6062
* Exclude the Spring bean factory interface

examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/query/SimpleQuery.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ class SimpleQuery : Query {
8383
Selection.ONE -> "You chose the first one"
8484
Selection.TWO -> "You chose the second one"
8585
}
86+
87+
fun setList(): Set<String> = setOf("one", "one", "two")
8688
}

website/docs/schema-generator/writing-schemas/lists.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ title: Lists
44
---
55
Both `kotlin.Array` and `kotlin.collections.List` are automatically mapped to the GraphQL `List` type (for unsupported
66
use cases see below). Type arguments provided to Kotlin collections are used as the type arguments in the GraphQL `List`
7-
type. Kotlin specialized classes representing arrays of Java primitive types without boxing overhead (e.g. `IntArray`)
7+
type. Kotlin specialized classes (e.g. `IntArray`) representing arrays of Java primitive types without boxing overhead
88
are also supported.
99

1010
```kotlin
11-
1211
class SimpleQuery {
1312
fun generateList(): List<Int> {
1413
// some logic here that generates list
@@ -22,19 +21,16 @@ class SimpleQuery {
2221
// some logic here that processes list
2322
}
2423
}
25-
2624
```
2725

2826
The above Kotlin class would produce the following GraphQL schema:
2927

3028
```graphql
31-
3229
type Query {
3330
generateList: [Int!]!
3431
doSomethingWithIntArray(ints: [Int!]!): String!
3532
doSomethingWithIntList(ints: [Int!]!): String!
3633
}
37-
3834
```
3935

4036
## Primitive Arrays
@@ -52,12 +48,22 @@ the `kotlin.Array` of objects the underlying type is automatically mapped to Gra
5248
| `kotlin.CharArray` |
5349
| `kotlin.BooleanArray` |
5450

55-
&gt; NOTE: Underlying GraphQL types of primitive arrays will be corresponding to the built-in scalar types or extended
51+
&gt; NOTE: The underlying GraphQL types of primitive arrays will be corresponding to the built-in scalar types or extended
5652
&gt; scalar types provided by `graphql-java`.
5753

5854
## Unsupported Collection Types
5955

60-
Currently GraphQL spec only supports `Lists`. Therefore even though Java and Kotlin support number of other collection
56+
Currently, the GraphQL spec only supports `Lists`. Therefore, even though Java and Kotlin support number of other collection
6157
types, `graphql-kotlin-schema-generator` only explicitly supports `Lists` and primitive arrays. Other collection types
6258
such as `Sets` (see [#201](https://github.com/ExpediaGroup/graphql-kotlin/issues/201)) and arbitrary `Map` data
63-
structures are not supported.
59+
structures are not supported out of the box. While we do not reccomend using `Map` or `Set` in the schema,
60+
they are supported with the use of the schema hooks.
61+
62+
```kotlin
63+
override fun willResolveMonad(type: KType): KType = when (type.classifier) {
64+
Set::class -> List::class.createType(type.arguments)
65+
else -> type
66+
}
67+
```
68+
69+
See [Discussion #1110](https://github.com/ExpediaGroup/graphql-kotlin/discussions/1110) for more details.

0 commit comments

Comments
 (0)