Skip to content

Commit c718888

Browse files
committed
connect to the actual API
1 parent 8a0c582 commit c718888

5 files changed

Lines changed: 23 additions & 64 deletions

File tree

shared/data/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ apollo {
5252
generateDataBuilders.set(true)
5353
mapScalar("GraphQLLocalDateTime", "kotlinx.datetime.LocalDateTime", "com.apollographql.adapter.datetime.KotlinxLocalDateTimeAdapter")
5454
mapScalar("GraphQLInstant", "kotlinx.datetime.Instant", "fr.androidmakers.store.graphql.KotlinxInstantAdapter")
55+
mapScalarToKotlinString("Markdown")
5556

5657
@OptIn(ApolloExperimental::class)
5758
plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin:${libs.versions.apollo.cache.get()}") {

shared/data/src/commonMain/graphql/feed.graphql

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
query GetFeedMessages {
2-
feedMessages {
3-
...FeedMessageDetails
2+
feedItemsConnection(first: 1000) {
3+
nodes {
4+
...FeedMessageDetails
45
}
6+
}
57
}
68

7-
subscription OnFeedMessageAdded {
8-
feedMessageAdded {
9-
...FeedMessageDetails
10-
}
11-
}
12-
13-
fragment FeedMessageDetails on FeedMessage {
9+
fragment FeedMessageDetails on FeedItem {
1410
id
1511
type
1612
title

shared/data/src/commonMain/graphql/schema.graphqls

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ enum ConferenceField {
162162
DAYS
163163
}
164164

165+
enum FeedItemType {
166+
INFO
167+
168+
ALERT
169+
170+
ANNOUNCEMENT
171+
}
172+
165173
enum LinkType {
166174
YouTube
167175

@@ -207,11 +215,13 @@ type FeatureFlags {
207215
type FeedItem {
208216
id: ID!
209217

218+
type: FeedItemType!
219+
210220
createdAt: GraphQLInstant!
211221

212222
title: String!
213223

214-
markdown: Markdown!
224+
body: Markdown!
215225
}
216226

217227
type FeedItemFailure {
@@ -463,7 +473,7 @@ input ConferenceOrderBy {
463473
input FeedItemInput {
464474
title: String
465475

466-
markdown: Markdown
476+
body: Markdown
467477
}
468478

469479
input SessionOrderBy {
@@ -502,32 +512,7 @@ directive @defer (label: String, if: Boolean! = true) on FRAGMENT_SPREAD|INLINE_
502512

503513
directive @specifiedBy (url: String!) on SCALAR
504514

505-
enum FeedMessageType {
506-
INFO
507-
508-
ALERT
509-
510-
ANNOUNCEMENT
511-
}
512-
513-
type FeedMessage {
514-
id: String!
515-
516-
type: FeedMessageType!
517-
518-
title: String!
519-
520-
body: String!
521-
522-
createdAt: GraphQLInstant!
523-
}
524-
525-
type RootSubscription {
526-
feedMessageAdded: FeedMessage!
527-
}
528-
529515
schema {
530516
query: RootQuery
531517
mutation: RootMutation
532-
subscription: RootSubscription
533518
}

shared/data/src/commonMain/kotlin/fr/androidmakers/store/graphql/FeedGraphQLRepository.kt

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,8 @@ class FeedGraphQLRepository(
1515
) : FeedRepository {
1616

1717
override fun getFeedItems(): Flow<Result<List<FeedItem>>> {
18-
val initialItems = apolloClient.query(GetFeedMessagesQuery())
18+
return apolloClient.query(GetFeedMessagesQuery())
1919
.cacheAndNetwork()
20-
.map { result -> result.map { data -> data.feedMessages.map { it.feedMessageDetails.toFeedItem() } } }
21-
22-
val newItems = apolloClient.subscription(OnFeedMessageAddedSubscription())
23-
.toFlow()
24-
.mapNotNull { response ->
25-
response.data?.let { data ->
26-
Result.success(listOf(data.feedMessageAdded.feedMessageDetails.toFeedItem()))
27-
}
28-
}
29-
.catch { /* subscription error — silently stop, initial data still shown */ }
30-
31-
return merge(initialItems, newItems)
32-
.scan<Result<List<FeedItem>>, Result<List<FeedItem>>>(Result.success(emptyList())) { acc, result ->
33-
val newData = result.getOrNull()
34-
if (newData == null) {
35-
// Propagate error only if we have no data yet
36-
if (acc.getOrNull().isNullOrEmpty()) result else acc
37-
} else {
38-
val existing = acc.getOrNull().orEmpty()
39-
val existingIds = existing.map { it.id }.toSet()
40-
val unique = newData.filter { it.id !in existingIds }
41-
Result.success(existing + unique)
42-
}
43-
}
20+
.map { result -> result.map { data -> data.feedItemsConnection.nodes.map { it.feedMessageDetails.toFeedItem() } } }
4421
}
4522
}

shared/data/src/commonMain/kotlin/fr/androidmakers/store/graphql/FeedMappers.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package fr.androidmakers.store.graphql
33
import fr.androidmakers.domain.model.FeedItem
44
import fr.androidmakers.domain.model.MessageType
55
import fr.androidmakers.store.graphql.fragment.FeedMessageDetails
6-
import fr.androidmakers.store.graphql.type.FeedMessageType
6+
import fr.androidmakers.store.graphql.type.FeedItemType
77

88
fun FeedMessageDetails.toFeedItem(): FeedItem {
99
return when (type) {
10-
FeedMessageType.ALERT -> FeedItem.Alert(
10+
FeedItemType.ALERT -> FeedItem.Alert(
1111
id = id,
1212
title = title,
1313
message = body,
1414
)
1515
else -> FeedItem.Message(
1616
id = id,
1717
type = when (type) {
18-
FeedMessageType.ANNOUNCEMENT -> MessageType.ANNOUNCEMENT
18+
FeedItemType.ANNOUNCEMENT -> MessageType.ANNOUNCEMENT
1919
else -> MessageType.INFO
2020
},
2121
title = title,

0 commit comments

Comments
 (0)