diff --git a/server/src/api/feed/feed.graphql b/server/src/api/feed/feed.graphql index de1b1d47..460024e6 100644 --- a/server/src/api/feed/feed.graphql +++ b/server/src/api/feed/feed.graphql @@ -72,3 +72,7 @@ type Mutation { type Mutation { enrollFeed(content: String!, files: [Upload]): Boolean! } + +type Subscription { + newFeed: [IFeed] +} diff --git a/server/src/api/feed/feed.resolvers.ts b/server/src/api/feed/feed.resolvers.ts index 910d04a1..b341b0b3 100644 --- a/server/src/api/feed/feed.resolvers.ts +++ b/server/src/api/feed/feed.resolvers.ts @@ -1,5 +1,10 @@ import db from '../../db'; -import { MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE } from '../../schema/feed/query'; +import { + MATCH_FEEDS, + UPDATE_LIKE, + DELETE_LIKE, + GET_NEW_FEED +} from '../../schema/feed/query'; import { ParseResultRecords } from '../../utils/parseData'; import { MutationEnrollFeedArgs, @@ -63,7 +68,7 @@ const mutationResolvers: MutationResolvers = { enrollFeed: async ( _, { content, files }: MutationEnrollFeedArgs, - { req } + { req, pubsub } ): Promise => { const { email } = req; if (!email) return false; @@ -73,6 +78,18 @@ const mutationResolvers: MutationResolvers = { if (files && files.length) { createImages(feedId, files); } + + const registerdFeed = await requestDB(GET_NEW_FEED, { + feedId, + useremail: email + }); + // console.log('registerdFeed?? ', registerdFeed); + const parsedRegisterdFeed = ParseResultRecords(registerdFeed); + // console.log('parsedRegisterdFeed?? ', parsedRegisterdFeed); + + pubsub.publish(NEW_FEED, { + newFeed: parsedRegisterdFeed + }); return true; }, updateLike: async (_, { feedId, count }, { req }) => { @@ -112,7 +129,17 @@ const queryResolvers: QueryResolvers = { } }; +const NEW_FEED = 'NEW_FEED_PUBSUB'; + export default { Query: queryResolvers, - Mutation: mutationResolvers + Mutation: mutationResolvers, + Subscription: { + newFeed: { + subscribe: (_, __, { pubsub }) => { + console.log('subscripbed'); + return pubsub.asyncIterator(NEW_FEED); + } + } + } }; diff --git a/server/src/app.ts b/server/src/app.ts index e023d4ce..200101cb 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -1,6 +1,6 @@ import cookieParser from 'cookie-parser'; import cors from 'cors'; -import { GraphQLServer } from 'graphql-yoga'; +import { GraphQLServer, PubSub } from 'graphql-yoga'; import helmet from 'helmet'; import logger from 'morgan'; import config from './utils/config'; @@ -12,9 +12,14 @@ import setUserFromJWT from './middleware/setUserFromJWT'; class App { public app: GraphQLServer; constructor() { + const pubsub = new PubSub(); this.app = new GraphQLServer({ schema, - context: ({ request, response }) => ({ req: request, res: response }) + context: ({ request, response }) => ({ + req: request, + res: response, + pubsub + }) }); this.middlewares(); } diff --git a/server/src/schema/feed/query.ts b/server/src/schema/feed/query.ts index 2522ecaf..dbc7c589 100644 --- a/server/src/schema/feed/query.ts +++ b/server/src/schema/feed/query.ts @@ -1,14 +1,3 @@ -/* const MATCH_FEEDS1 = `MATCH (searchUser:User)-[:AUTHOR]->(feed:Feed) -OPTIONAL MATCH (likeUser:User)-[:LIKE]->(feed) -OPTIONAL MATCH (feed)-[:HAS]->(com:Comment) -WITH searchUser, feed, COLLECT(DISTINCT likeUser) AS cp , COLLECT(com) as comments -where feed.createdAt < datetime({cursor}) -RETURN searchUser , feed, ID(feed) as feedId , length(cp) AS totallikes, -length(filter(x IN cp WHERE x.email= {useremail} )) AS hasLiked, comments -order by feed.createdAt desc -LIMIT {first} -`; */ - const MATCH_FEEDS = `MATCH (searchUser:User)-[:AUTHOR]->(feed:Feed) OPTIONAL MATCH (likeUser:User)-[like:LIKE]->(feed) OPTIONAL MATCH (feed)-[:HAS]->(com:Comment) @@ -32,4 +21,15 @@ MATCH (u:User)-[r:LIKE]->(f:Feed) WHERE u.email = {useremail} AND ID(f) = {feedId} delete r`; -export { MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE }; +const GET_NEW_FEED = `MATCH (searchUser:User)-[:AUTHOR]->(feed:Feed) +OPTIONAL MATCH (likeUser:User)-[like:LIKE]->(feed) +OPTIONAL MATCH (feed)-[:HAS]->(com:Comment) +OPTIONAL MATCH (feed)<-[:HAS]-(img:Image) +WITH searchUser, feed, COLLECT(DISTINCT likeUser) AS cp , COLLECT(com) as comments, COLLECT(DISTINCT img) as imgs +where ID(feed) = {feedId} +RETURN searchUser , feed, ID(feed) as feedId , length(cp) AS totallikes, imgs as imglist, +length(filter(x IN cp WHERE x.email= {useremail} )) AS hasLiked, comments +order by feed.createdAt desc +`; + +export { MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE, GET_NEW_FEED }; diff --git a/server/src/utils/parseData.ts b/server/src/utils/parseData.ts index 25c36bf0..867274fe 100644 --- a/server/src/utils/parseData.ts +++ b/server/src/utils/parseData.ts @@ -39,7 +39,10 @@ export const Datetransform = object => { } return returnobj; }; - +/** + * 주의사항 : 결과가 여러개이면 result.records 한개이면 result를 record로 받아야 함 + * @param records + */ export const ParseResultRecords = records => { let result: any[] = []; for (const item of records) {