-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Partly) take over aPureBase/KGraphQL#171
- Loading branch information
1 parent
a3629cb
commit bc01867
Showing
5 changed files
with
193 additions
and
31 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
58 changes: 53 additions & 5 deletions
58
kgraphql/src/main/kotlin/com/apurebase/kgraphql/helpers/KGraphQLExtensions.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 |
---|---|---|
@@ -1,17 +1,65 @@ | ||
package com.apurebase.kgraphql.helpers | ||
|
||
import com.apurebase.kgraphql.schema.execution.Execution | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
|
||
/** | ||
* This returns a list of all scalar fields requested on this type. | ||
*/ | ||
fun Execution.getFields(): List<String> = when (this) { | ||
is Execution.Fragment -> elements.flatMap(Execution::getFields) | ||
is Execution.Node -> { | ||
if (children.isEmpty()) listOf(key) | ||
else children | ||
.filterNot { (it is Execution.Node && it.children.isNotEmpty()) } | ||
.flatMap(Execution::getFields) | ||
|
||
if (children.isEmpty()) { | ||
listOf(key) | ||
} else { | ||
children | ||
.filterNot { (it is Execution.Node && it.children.isNotEmpty()) } | ||
.flatMap(Execution::getFields) | ||
} | ||
} | ||
}.distinct() | ||
|
||
|
||
/** | ||
* Collection : Convert to JsonElement | ||
*/ | ||
fun Collection<*>.toJsonElement(): JsonElement { | ||
val list: MutableList<JsonElement> = mutableListOf() | ||
forEach { | ||
val value = it ?: return@forEach | ||
when (value) { | ||
is Number -> list.add(JsonPrimitive(value)) | ||
is Boolean -> list.add(JsonPrimitive(value)) | ||
is String -> list.add(JsonPrimitive(value)) | ||
is Map<*, *> -> list.add((value).toJsonElement()) | ||
is Collection<*> -> list.add(value.toJsonElement()) | ||
is Array<*> -> list.add(value.toList().toJsonElement()) | ||
else -> list.add(JsonPrimitive(value.toString())) // other type | ||
} | ||
} | ||
return JsonArray(list) | ||
} | ||
|
||
/** | ||
* Map : Convert to JsonElement | ||
*/ | ||
fun Map<*, *>.toJsonElement(): JsonElement { | ||
val map: MutableMap<String, JsonElement> = mutableMapOf() | ||
forEach { | ||
val key = it.key as? String ?: return@forEach | ||
val value = it.value ?: return@forEach | ||
when (value) { | ||
is Number -> map[key] = JsonPrimitive(value) | ||
is Boolean -> map[key] = JsonPrimitive(value) | ||
is String -> map[key] = JsonPrimitive(value) | ||
is Map<*, *> -> map[key] = (value).toJsonElement() | ||
is Collection<*> -> map[key] = value.toJsonElement() | ||
is Array<*> -> map[key] = value.toList().toJsonElement() | ||
else -> map[key] = JsonPrimitive(value.toString()) // other type | ||
} | ||
} | ||
return JsonObject(map) | ||
} |
68 changes: 68 additions & 0 deletions
68
kgraphql/src/test/kotlin/com/apurebase/kgraphql/GraphQLErrorTest.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,68 @@ | ||
package com.apurebase.kgraphql | ||
|
||
import kotlinx.serialization.json.addJsonObject | ||
import kotlinx.serialization.json.buildJsonArray | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.jupiter.api.Test | ||
|
||
class GraphQLErrorTest { | ||
|
||
@Test | ||
fun `test graphql error with custom error type`() { | ||
val graphqlError = GraphQLError( | ||
message = "test", | ||
extensionsErrorType = "AUTHORIZATION_ERROR" | ||
) | ||
|
||
val expectedJson = buildJsonObject { | ||
put("errors", buildJsonArray { | ||
addJsonObject { | ||
put("message", "test") | ||
put("locations", buildJsonArray {}) | ||
put("path", buildJsonArray {}) | ||
put("extensions", buildJsonObject { | ||
put("type", "AUTHORIZATION_ERROR") | ||
}) | ||
} | ||
}) | ||
}.toString() | ||
|
||
graphqlError.serialize() shouldBeEqualTo expectedJson | ||
} | ||
|
||
@Test | ||
fun `test graphql error with custom error type and detail`() { | ||
val graphqlError = GraphQLError( | ||
message = "test", | ||
extensionsErrorType = "VALIDATION_ERROR", | ||
extensionsErrorDetail = mapOf<String, Any?>( | ||
"singleCheck" to mapOf("email" to "not an email", "age" to "Limited to 150"), | ||
"multiCheck" to "The 'from' number must not exceed the 'to' number" | ||
) | ||
) | ||
|
||
val expectedJson = buildJsonObject { | ||
put("errors", buildJsonArray { | ||
addJsonObject { | ||
put("message", "test") | ||
put("locations", buildJsonArray {}) | ||
put("path", buildJsonArray {}) | ||
put("extensions", buildJsonObject { | ||
put("type", "VALIDATION_ERROR") | ||
put("detail", buildJsonObject { | ||
put("singleCheck", buildJsonObject { | ||
put("email", "not an email") | ||
put("age", "Limited to 150") | ||
}) | ||
put("multiCheck", "The 'from' number must not exceed the 'to' number") | ||
}) | ||
}) | ||
} | ||
}) | ||
}.toString() | ||
|
||
graphqlError.serialize() shouldBeEqualTo expectedJson | ||
} | ||
} |
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