Merge branch 'more-gqls' into 'dev'

subs, queries

See merge request discoursio/discoursio-webapp!28
This commit is contained in:
Igor 2023-03-23 13:26:34 +00:00
commit fc3f944a22
6 changed files with 67 additions and 11 deletions

View File

@ -70,7 +70,7 @@ export const InboxView = () => {
} }
} }
// TODO: удалить когда будет готова подписка /*
createEffect(() => { createEffect(() => {
setInterval(async () => { setInterval(async () => {
if (!currentDialog()) return if (!currentDialog()) return
@ -83,7 +83,7 @@ export const InboxView = () => {
} }
}, 2000) }, 2000)
}) })
*/
onMount(async () => { onMount(async () => {
try { try {
const response = await loadRecipients({ days: 365 }) const response = await loadRecipients({ days: 365 })

View File

@ -1,6 +1,6 @@
import type { Accessor, JSX } from 'solid-js' import type { Accessor, JSX } from 'solid-js'
import { createContext, createSignal, useContext, createMemo } from 'solid-js' import { createContext, createSignal, useContext, createMemo } from 'solid-js'
import { createChatClient } from '../graphql/privateGraphQLClient' import { createSubClient } from '../graphql/privateGraphQLClient'
import type { Chat, Message, MutationCreateMessageArgs } from '../graphql/types.gen' import type { Chat, Message, MutationCreateMessageArgs } from '../graphql/types.gen'
import { apiClient } from '../utils/apiClient' import { apiClient } from '../utils/apiClient'
import newMessage from '../graphql/subs/new-message' import newMessage from '../graphql/subs/new-message'
@ -29,7 +29,7 @@ export function useInbox() {
export const InboxProvider = (props: { children: JSX.Element }) => { export const InboxProvider = (props: { children: JSX.Element }) => {
const [chats, setChats] = createSignal<Chat[]>([]) const [chats, setChats] = createSignal<Chat[]>([])
const [messages, setMessages] = createSignal<Message[]>([]) const [messages, setMessages] = createSignal<Message[]>([])
const subclient = createMemo<Client>(() => createChatClient()) const subclient = createMemo<Client>(() => createSubClient())
const loadChats = async () => { const loadChats = async () => {
try { try {
const newChats = await apiClient.getChats({ limit: 50, offset: 0 }) const newChats = await apiClient.getChats({ limit: 50, offset: 0 })
@ -71,8 +71,8 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
return chat return chat
} }
const { unsubscribe } = pipe( pipe(
() => subclient().subscription(newMessage, {}), subclient().subscription(newMessage, {}),
subscribe((result) => { subscribe((result) => {
console.info('[subscription]') console.info('[subscription]')
console.debug(result) console.debug(result)
@ -83,8 +83,8 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
createChat, createChat,
loadChats, loadChats,
getMessages, getMessages,
sendMessage, sendMessage
unsubscribe // TODO: call unsubscribe some time! // unsubscribe // TODO: call unsubscribe some time!
} }
const value: InboxContextType = { chats, messages, actions } const value: InboxContextType = { chats, messages, actions }

View File

@ -7,7 +7,7 @@ import {
createClient createClient
} from '@urql/core' } from '@urql/core'
// import { createClient as createSubClient } from 'graphql-sse' // import { createClient as createSubClient } from 'graphql-sse'
import { createClient as createSubClient } from 'graphql-ws' import { createClient as createWSClient } from 'graphql-ws'
import { devtoolsExchange } from '@urql/devtools' import { devtoolsExchange } from '@urql/devtools'
import { isDev, apiBaseUrl } from '../utils/config' import { isDev, apiBaseUrl } from '../utils/config'
// import { cache } from './cache' // import { cache } from './cache'
@ -55,8 +55,8 @@ const options: ClientOptions = {
export const privateGraphQLClient = createClient(options) export const privateGraphQLClient = createClient(options)
export const createChatClient = () => { export const createSubClient = () => {
const subClient = createSubClient({ const subClient = createWSClient({
url: apiBaseUrl.replace('http', 'ws') // + '/messages' url: apiBaseUrl.replace('http', 'ws') // + '/messages'
}) })

View File

@ -0,0 +1,26 @@
import { gql } from '@urql/core'
export default gql`
subscription {
newReactions {
id
body
kind
range
createdAt
replyTo
stat {
rating
}
shout {
id
slug
}
createdBy {
name
slug
userpic
}
}
}
`

View File

@ -0,0 +1,25 @@
import { gql } from '@urql/core'
export default gql`
subscription {
newShout {
id
slug
title
subtitle
body
topics {
# id
title
slug
}
authors {
id
name
slug
userpic
caption
}
}
}
`

View File

@ -38,6 +38,7 @@ import myChats from '../graphql/query/chats-load'
import chatMessagesLoadBy from '../graphql/query/chat-messages-load-by' import chatMessagesLoadBy from '../graphql/query/chat-messages-load-by'
import authorBySlug from '../graphql/query/author-by-slug' import authorBySlug from '../graphql/query/author-by-slug'
import userSubscribers from '../graphql/query/author-followers' import userSubscribers from '../graphql/query/author-followers'
import userFollowing from '../graphql/query/author-following'
import topicBySlug from '../graphql/query/topic-by-slug' import topicBySlug from '../graphql/query/topic-by-slug'
import createChat from '../graphql/mutation/create-chat' import createChat from '../graphql/mutation/create-chat'
import reactionsLoadBy from '../graphql/query/reactions-load-by' import reactionsLoadBy from '../graphql/query/reactions-load-by'
@ -221,6 +222,10 @@ export const apiClient = {
const response = await publicGraphQLClient.query(userSubscribers, { slug }).toPromise() const response = await publicGraphQLClient.query(userSubscribers, { slug }).toPromise()
return response.data.userFollowers return response.data.userFollowers
}, },
getAuthorFollowing: async ({ slug }: { slug: string }): Promise<Author[]> => {
const response = await publicGraphQLClient.query(userFollowing, { slug }).toPromise()
return response.data.userFollowing
},
updateProfile: async (input: ProfileInput) => { updateProfile: async (input: ProfileInput) => {
const response = await privateGraphQLClient.mutation(updateProfile, { profile: input }).toPromise() const response = await privateGraphQLClient.mutation(updateProfile, { profile: input }).toPromise()
return response.data.updateProfile return response.data.updateProfile