Task: Add query for account with completable future and want to test with flux and fetch accounts with subscriptions

This commit is contained in:
Sambo Chea 2021-08-08 19:48:50 +07:00
parent bfff91a8df
commit ea9c700f37
5 changed files with 71 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import React from 'react';
import './App.less';
import {gql, useQuery, useSubscription} from "@apollo/client";
import { Table } from 'antd';
interface User {
id: number
@ -33,14 +34,46 @@ const ACCOUNTS = gql`
}
`
const SUB_ACCOUNTS = gql`
subscription {
fetchAccounts {
id
code
balance
user {
code
name
}
}
}
`
const HELLO = gql`
subscription {
hello
}
`
const accountColumns = [
{
title: "Account ID",
dataIndex: "id",
key: "id",
},
{
title: "Account Code",
dataIndex: "code",
key: "code",
},
{
title: "User",
dataIndex: ["user", "name"],
key: "user.name",
}
]
function App() {
const {error, loading, data} = useQuery<AccountResult>(ACCOUNTS)
// const {error, loading, data} = useQuery<AccountResult>(ACCOUNTS)
const {error, loading, data} = useSubscription<AccountResult>(SUB_ACCOUNTS)
// const {error, loading, data} = useSubscription(HELLO)
console.log(data)
return (
@ -48,14 +81,15 @@ function App() {
<h1>Accounts</h1>
{
loading || !data ? <p>Loading...</p> :
data.fetchAccounts.map(account => (
<>
<div>Account ID: {account.id}</div>
<div>Account Code: {account.code}</div>
<div>Account User: {account.user.name}</div>
</>
)
)
// data.fetchAccounts.map(account => (
// <>
// <div>Account ID: {account.id}</div>
// <div>Account Code: {account.code}</div>
// <div>Account User: {account.user.name}</div>
// </>
// )
// )
<Table dataSource={data.fetchAccounts} columns={accountColumns}/>
// <p>{`${data.hello}`}</p>
}
</>

View File

@ -1,8 +1,10 @@
package com.cubetiqs.graphql.demo.config
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.transaction.annotation.EnableTransactionManagement
@Configuration
@EnableTransactionManagement
@EnableAsync
class ManagementConfig

View File

@ -2,7 +2,12 @@ package com.cubetiqs.graphql.demo.repository
import com.cubetiqs.graphql.demo.domain.account.Account
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Repository
import java.util.concurrent.CompletableFuture
@Repository
interface AccountRepository : JpaRepository<Account, Long>
interface AccountRepository : JpaRepository<Account, Long> {
@Async
fun readAllBy(): CompletableFuture<Collection<Account>>
}

View File

@ -1,16 +1,34 @@
package com.cubetiqs.graphql.demo.resolver.subscription
import com.cubetiqs.graphql.demo.context.GSubscription
import com.cubetiqs.graphql.demo.domain.account.Account
import com.cubetiqs.graphql.demo.repository.AccountRepository
import com.netflix.graphql.dgs.DgsSubscription
import graphql.schema.DataFetchingEnvironment
import kotlinx.coroutines.reactor.awaitSingle
import org.reactivestreams.Publisher
import org.springframework.beans.factory.annotation.Autowired
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toFlux
import reactor.kotlin.core.publisher.toMono
import java.time.Duration
@GSubscription
class HelloSubscriptionResolver {
@Autowired
private lateinit var accountRepository: AccountRepository
@DgsSubscription(field = "hello")
fun hello(env: DataFetchingEnvironment): Publisher<Int> {
return Flux.range(1, 10).delayElements(Duration.ofSeconds(1))
}
@DgsSubscription(field = "fetchAccounts")
fun fetchAccounts(env: DataFetchingEnvironment): Publisher<Collection<Account>> {
return Flux.generate<Collection<Account>?> { sink ->
sink.next(accountRepository.findAll())
}.delayElements(Duration.ofSeconds(1))
// return Mono.fromFuture(accounts).toFlux().delayElements(Duration.ofSeconds(1))
}
}

View File

@ -12,6 +12,8 @@ type Query {
type Subscription {
hello: Int
fetchAccounts: [Account]
}
type Mutation {