Skip to content

Commit f513d41

Browse files
dariuszkucgscheibel
authored andcommitted
Add example of optional input fields (#52)
* Add example of optional input fields * Add example of reusing same object for input and output fields
1 parent b980c28 commit f513d41

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

example/src/main/kotlin/com.expedia.graphql.sample/model/Widget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.expedia.graphql.annotations.GraphQLIgnore
66
@GraphQLDescription("A useful widget")
77
data class Widget(
88
@property:GraphQLDescription("The widget's value that can be null")
9-
val value: Int?,
9+
var value: Int? = null,
1010
@property:Deprecated(message = "This field is deprecated", replaceWith = ReplaceWith("value"))
1111
@property:GraphQLDescription("The widget's deprecated value that shouldn't be used")
1212
val deprecatedValue: Int? = value,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.expedia.graphql.sample.mutation
2+
3+
import com.expedia.graphql.annotations.GraphQLDescription
4+
import com.expedia.graphql.sample.model.Widget
5+
import org.springframework.stereotype.Component
6+
7+
/**
8+
* Simple widget mutation that shows that same objects can be used for input and output GraphQL types.
9+
*/
10+
@Component
11+
class WidgetMutation : Mutation {
12+
13+
@GraphQLDescription("modifies passed in widget so it doesn't have null value")
14+
fun processWidget(@GraphQLDescription("widget to be modified") widget: Widget): Widget {
15+
if (null == widget.value) {
16+
widget.value = 42
17+
}
18+
return widget
19+
}
20+
}

example/src/main/kotlin/com.expedia.graphql.sample/query/SimpleQuery.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ class SimpleQuery: Query {
4646
val random = Random()
4747
return (1..10).map { random.nextInt(100) }.toList()
4848
}
49+
50+
@GraphQLDescription("query with optional input")
51+
fun doSomethingWithOptionalInput(
52+
@GraphQLDescription("this field is required") requiredValue: Int,
53+
@GraphQLDescription("this field is optional") optionalValue: Int? = null)
54+
= "required value=$requiredValue, optional value=$optionalValue"
4955
}

0 commit comments

Comments
 (0)