Skip to content

Commit

Permalink
Add Subscription for Plugin creation
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddheshKukade committed Jul 2, 2023
1 parent 6af8fff commit 214084b
Show file tree
Hide file tree
Showing 9 changed files with 1,081 additions and 63 deletions.
1,066 changes: 1,006 additions & 60 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"homepage": "https://github.com/PalisadoesFoundation/talawa-api#readme",
"dependencies": {
"@apollo/server": "^4.7.4",
"@esbuild/win32-x64": "^0.18.11",
"@graphql-inspector/cli": "^3.4.19",
"@graphql-tools/schema": "^10.0.0",
"@graphql-tools/utils": "^10.0.1",
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ type Subscription {
directMessageChat: MessageChat
messageSentToDirectChat: DirectChatMessage
messageSentToGroupChat: GroupChatMessage
onPluginUpdate: Plugin
}

type Task {
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ const wsServer = new WebSocketServer({

// Hand in the schema we just created and have the
// WebSocketServer start listening.
const serverCleanup = useServer({ schema }, wsServer);
const serverCleanup = useServer(
{ schema, context: (ctx, _msg, _args) => ({ pubsub }) },
wsServer
);

async function startServer(): Promise<void> {
await database.connect();
Expand Down Expand Up @@ -109,6 +112,7 @@ async function startServer(): Promise<void> {
await logIssues();

console.log(`🚀 Server ready at http://localhost:4000/graphql`);
console.log(`🚀 Subscription endpoint ready at ws://localhost:4000/graphql`);
}

startServer();
7 changes: 6 additions & 1 deletion src/resolvers/Mutation/createPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import { Plugin } from "../../models";

export const createPlugin: MutationResolvers["createPlugin"] = async (
_parent,
args
args,
context
) => {
// Creates new plugin.
const createdPlugin = await Plugin.create({
...args,
});
// calls subscription
context.pubsub.publish("TALAWA_PLUGIN_UPDATED", {
Plugin: createdPlugin.toObject(),
});

// Returns createdPlugin.
return createdPlugin.toObject();
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers/Subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import type { SubscriptionResolvers } from "../../types/generatedGraphQLTypes";
import { directMessageChat } from "./directMessageChat";
import { messageSentToDirectChat } from "./messageSentToDirectChat";
import { messageSentToGroupChat } from "./messageSentToGroupChat";

import { onPluginUpdate } from "./onPluginUpdate";
export const Subscription: SubscriptionResolvers = {
directMessageChat,
messageSentToDirectChat,
messageSentToGroupChat,
onPluginUpdate,
};
57 changes: 57 additions & 0 deletions src/resolvers/Subscription/onPluginUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { withFilter } from "graphql-subscriptions";
import type { SubscriptionResolvers } from "../../types/generatedGraphQLTypes";
import { Plugin } from "../../models";
// import { GroupChat } from "../GroupChat";

const TALAWA_PLUGIN_UPDATED = "TALAWA_PLUGIN_UPDATED";
/**
* This property included a `subscribe` method, which is used to
* subscribe the `current_user` to get updates for Group chats.
*
* @remarks To control updates on a per-client basis, the function uses the `withFilter`
* method imported from `apollo-server-express` module.
* You can learn about `subscription` {@link https://www.apollographql.com/docs/apollo-server/data/subscriptions/ | here }.
*/
// const subscribers: any = [];
// const messages: any = [];
// const onMessagesUpdates = (fn:any) => subscribers.push(fn);
export const filterFunction = async function (
payload: any,
context: any
): Promise<boolean> {
// const { currentUserId } = context.context;
// const groupChatId = payload.messageSentToGroupChat.groupChatMessageBelongsTo;

// const groupChat = await GroupChat.findOne({
// _id: groupChatId,
// }).lean();

// if (groupChat) {
// const currentUserIsGroupChatMember = groupChat.users.some((user) =>
// user.equals(currentUserId)
// );
// return currentUserIsGroupChatMember;
// } else {
// return false;
// }
console.log("=====> PAYLOAD", payload);
console.log("=====> context", context);
return true;
};
// const getPlugins = async() => await Plugin.find().lean();
export const onPluginUpdate: SubscriptionResolvers["onPluginUpdate"] = {
// @ts-ignorep
subscribe: withFilter(
(_parent, _args, context) =>
context.pubsub.asyncIterator([TALAWA_PLUGIN_UPDATED]),

(payload, _variables, context) => filterFunction(payload, context)
),
resolve: (payload: any) => {
console.log("============OG PAYLOAD", payload);
return payload.Plugin;
},
};
// const res = getPlugins();
// console.log("datais ", _parent, _args, context,res);
// context.pubsub.publish(TALAWA_PLUGIN_UPDATED , {res})
1 change: 1 addition & 0 deletions src/typeDefs/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const subscriptions = gql`
directMessageChat: MessageChat
messageSentToDirectChat: DirectChatMessage
messageSentToGroupChat: GroupChatMessage
onPluginUpdate: Plugin
}
`;
2 changes: 2 additions & 0 deletions src/types/generatedGraphQLTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ export type Subscription = {
directMessageChat?: Maybe<MessageChat>;
messageSentToDirectChat?: Maybe<DirectChatMessage>;
messageSentToGroupChat?: Maybe<GroupChatMessage>;
onPluginUpdate?: Maybe<Plugin>;
};

export type Task = {
Expand Down Expand Up @@ -2531,6 +2532,7 @@ export type SubscriptionResolvers<ContextType = any, ParentType extends Resolver
directMessageChat?: SubscriptionResolver<Maybe<ResolversTypes['MessageChat']>, "directMessageChat", ParentType, ContextType>;
messageSentToDirectChat?: SubscriptionResolver<Maybe<ResolversTypes['DirectChatMessage']>, "messageSentToDirectChat", ParentType, ContextType>;
messageSentToGroupChat?: SubscriptionResolver<Maybe<ResolversTypes['GroupChatMessage']>, "messageSentToGroupChat", ParentType, ContextType>;
onPluginUpdate?: SubscriptionResolver<Maybe<ResolversTypes['Plugin']>, "onPluginUpdate", ParentType, ContextType>;
};

export type TaskResolvers<ContextType = any, ParentType extends ResolversParentTypes['Task'] = ResolversParentTypes['Task']> = {
Expand Down

0 comments on commit 214084b

Please sign in to comment.