From 29486f42214a8f89a524738ac03936889b35e250 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 1 Nov 2024 11:39:55 +0000 Subject: [PATCH] Fix graphql empty queries --- .../network/BangumiRelatedPeopleService.kt | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/app/shared/app-data/src/commonMain/kotlin/data/network/BangumiRelatedPeopleService.kt b/app/shared/app-data/src/commonMain/kotlin/data/network/BangumiRelatedPeopleService.kt index 9a5da5ce7a..54737b775c 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/network/BangumiRelatedPeopleService.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/network/BangumiRelatedPeopleService.kt @@ -108,50 +108,58 @@ class BangumiRelatedPeopleService( private suspend fun queryGraphQLCharacters( ids: Sequence, - ) = client.executeGraphQL( - "BangumiRelatedPeopleService.queryGraphQLCharacters", - """ - query MyQuery { - ${ids.distinct().joinToString("\n") { "c${it}: character(id: ${it}) { ...CharacterFragment }" }} - } - - fragment CharacterFragment on Character { - infobox { - values { - k - v + ): List { + val distinctIds = ids.distinct().toList() + if (distinctIds.isEmpty()) return emptyList() + return client.executeGraphQL( + "BangumiRelatedPeopleService.queryGraphQLCharacters", + """ + query MyQuery { + ${distinctIds.joinToString("\n") { "c${it}: character(id: ${it}) { ...CharacterFragment }" }} } - key - } - id - } - """.trimIndent(), - )["data"]!!.jsonObject.values.map { - json.decodeFromJsonElement(QCharacterOrPerson.serializer(), it) + + fragment CharacterFragment on Character { + infobox { + values { + k + v + } + key + } + id + } + """.trimIndent(), + )["data"]!!.jsonObject.values.map { + json.decodeFromJsonElement(QCharacterOrPerson.serializer(), it) + } } private suspend fun queryGraphQLPersons( ids: Sequence, - ) = client.executeGraphQL( - "BangumiRelatedPeopleService.queryGraphQLPersons", - """ - query MyQuery { - ${ids.joinToString("\n") { "c${it}: person(id: ${it}) { ...CharacterFragment }" }} - } - - fragment CharacterFragment on Person { - infobox { - values { - k - v + ): List { + val distinctIds = ids.distinct().toList() + if (distinctIds.isEmpty()) return emptyList() + return client.executeGraphQL( + "BangumiRelatedPeopleService.queryGraphQLPersons", + """ + query MyQuery { + ${distinctIds.joinToString("\n") { "c${it}: person(id: ${it}) { ...CharacterFragment }" }} } - key - } - id - } - """.trimIndent(), - )["data"]!!.jsonObject.values.map { - json.decodeFromJsonElement(QCharacterOrPerson.serializer(), it) + + fragment CharacterFragment on Person { + infobox { + values { + k + v + } + key + } + id + } + """.trimIndent(), + )["data"]!!.jsonObject.values.map { + json.decodeFromJsonElement(QCharacterOrPerson.serializer(), it) + } }