-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add NullableInputVariableSerializer (#557)
* feat: add NullableInputVariableSerializer * fix reference of interface for input value serializer * refactor: internalize serialize assignment * fix nullable params * remove unnecessary condition * fix constructors
- Loading branch information
Cole Turner
authored
May 25, 2023
1 parent
d91aac3
commit d468c72
Showing
6 changed files
with
182 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...e/src/main/kotlin/com/netflix/graphql/dgs/client/codegen/InputValueSerializerInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.client.codegen | ||
|
||
import graphql.language.Value | ||
|
||
/** | ||
* Marks this property invisible for input value serialization. | ||
*/ | ||
@Target(AnnotationTarget.PROPERTY) | ||
internal annotation class Transient | ||
|
||
interface InputValueSerializerInterface { | ||
fun serialize(input: Any?): String | ||
fun toValue(input: Any?): Value<*> | ||
} | ||
|
||
interface InputValue { | ||
fun inputValues(): List<Pair<String, Any?>> | ||
} |
50 changes: 50 additions & 0 deletions
50
...re/src/main/kotlin/com/netflix/graphql/dgs/client/codegen/NullableInputValueSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2021 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.client.codegen | ||
|
||
import graphql.language.NullValue | ||
import graphql.language.ObjectField | ||
import graphql.language.ObjectValue | ||
import graphql.language.Value | ||
import graphql.schema.Coercing | ||
import kotlin.reflect.full.allSuperclasses | ||
|
||
class NullableInputValueSerializer(scalars: Map<Class<*>, Coercing<*, *>> = emptyMap()) : | ||
InputValueSerializer(scalars) { | ||
|
||
override fun toValue(input: Any?): Value<*> { | ||
if (input == null) { | ||
return NullValue.newNullValue().build() | ||
} | ||
|
||
val optionalValue = getOptionalValue(input) | ||
|
||
if (optionalValue.isPresent) { | ||
return optionalValue.get() | ||
} | ||
|
||
val classes = (sequenceOf(input::class) + input::class.allSuperclasses.asSequence()) - Any::class | ||
val propertyValues = getPropertyValues(classes, input) | ||
|
||
val objectFields = propertyValues.asSequence() | ||
.map { (name, value) -> ObjectField(name, toValue(value)) } | ||
.toList() | ||
return ObjectValue.newObjectValue() | ||
.objectFields(objectFields) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters