Skip to content

Commit 318c139

Browse files
smyrickShane Myrickdariuszkuc
authored
[example] Add example of undefined input vs null (#785)
* Add example of undefined input vs null * Add docs page * Apply suggestions from code review Co-authored-by: Dariusz Kuc <[email protected]> Co-authored-by: Shane Myrick <[email protected]> Co-authored-by: Dariusz Kuc <[email protected]>
1 parent 629f35d commit 318c139

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
id: optional-undefined-arguments
3+
title: Optional Undefined Arguments
4+
---
5+
6+
In GraphQL, input types can be optional which means that the client can either:
7+
8+
* Not specify a value at all
9+
* Send null explictly
10+
* Send the non-null type
11+
12+
Optional input types are represented as nullable parameters in Kotlin
13+
```kotlin
14+
fun optionalInput(value: String?): String? = value
15+
```
16+
17+
```graphql
18+
query OptionalInputQuery {
19+
undefined: optionalInput
20+
null: optionalInput(value: null)
21+
foo: optionalInput(value: "foo")
22+
}
23+
```
24+
25+
By default, if an optional input value is not specified, then the execution engine will set the argument in Kotlin to `null`.
26+
This means that you can not tell, by just the value alone, whether the request did not contain any argument or the client explicitly passed in `null`.
27+
28+
Instead, you should inspect the [DataFetchingEnvironment](./data-fetching-environment.md) where you can see if the request had the variable defined and even check parent arguments as well.
29+
30+
```kotlin
31+
fun optionalInput(value: String?, dataFetchingEnvironment: DataFetchingEnvironment): String =
32+
if (dataFetchingEnvironment.containsArgument("value")) {
33+
"The value was $value"
34+
} else {
35+
"The value was undefined"
36+
}
37+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.examples.mutation
18+
19+
import com.expediagroup.graphql.spring.operations.Mutation
20+
import graphql.schema.DataFetchingEnvironment
21+
import org.springframework.stereotype.Component
22+
23+
@Component
24+
class OptionalInputMutation : Mutation {
25+
26+
/**
27+
* Example of how to check the difference between a client not sending a value
28+
* or explicitly passing in null
29+
*/
30+
fun optionalInput(value: String?, dataFetchingEnvironment: DataFetchingEnvironment): String =
31+
if (dataFetchingEnvironment.containsArgument("value")) {
32+
"The value was $value"
33+
} else {
34+
"The value was undefined"
35+
}
36+
}

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"schema-generator/execution/exceptions",
4545
"schema-generator/execution/data-fetching-environment",
4646
"schema-generator/execution/contextual-data",
47+
"schema-generator/execution/optional-undefined-arguments",
4748
"schema-generator/execution/subscriptions",
4849
"schema-generator/execution/introspection"
4950
]

0 commit comments

Comments
 (0)