Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Create reaction resolver #76

Merged
merged 5 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotenv/dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ VIRGIL_APP_ID=sample
VIRGIL_APP_KEY_ID=sample
VIRGIL_APP_KEY=sample

JWT_SECRET=dooboolab
JWT_SECRET=dooboolab
61 changes: 61 additions & 0 deletions migrations/20200301025425-Reaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const {
STRING,
TEXT,
DATE,
UUID,
UUIDV4
} = require('sequelize');

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable(
'reactions',
{
id: {
type: UUID,
defaultValue: UUIDV4,
allowNull: false,
primaryKey: true,
},
type: {
type: STRING,
allowNull: false,
},
createdAt: {
type: DATE
},
updatedAt: {
type: DATE
},
deletedAt: {
type: DATE
},
messageId: {
type: UUID,
references: {
model: 'messages',
key: 'id'
},
allowNull: true,
},
userId: {
type: UUID,
references: {
model: 'users',
key: 'id'
},
allowNull: true
},
},
{
charset: 'utf8mb4',
}
);
},

down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('reactions');
}
};
2 changes: 2 additions & 0 deletions schemas/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# import User from "user.graphql"
# import Channel from "channel.graphql"
# import Photo from "photo.graphql"
# import Reaction from "reaction.graphql"

scalar DateTime

Expand All @@ -19,6 +20,7 @@ type Message {
picture: [Photo]
filePath: String
replies: [Reply]
reactions: [Reaction]
createdAt: DateTime
updatedAt: DateTime
deletedAt: DateTime
Expand Down
9 changes: 9 additions & 0 deletions schemas/reaction.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scalar DateTime

type Reaction {
id: ID!
type: String
createdAt: DateTime
updatedAt: DateTime
deletedAt: DateTime
}
2 changes: 2 additions & 0 deletions schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type Mutation {
updateGallery(galleryId: ID!, photoURL: String!): Int
deleteGallery(galleryId: ID!): Int
singleUpload(file: Upload!, dir: String): String!
createReaction(messageId: ID!, type: String!): Reaction
deleteReaction(reactionId: ID!): Int
}

type Subscription {
Expand Down
38 changes: 38 additions & 0 deletions src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export type Message = {
filePath?: Maybe<Scalars['String']>;
id: Scalars['String'];
picture?: Maybe<Array<Maybe<Photo>>>;
reactions?: Maybe<Array<Maybe<Reaction>>>;
replies?: Maybe<Array<Maybe<Reply>>>;
sender?: Maybe<User>;
text?: Maybe<Scalars['String']>;
Expand All @@ -148,9 +149,11 @@ export type Mutation = {
* Do not pass current userId inside `users`.
*/
createMessage?: Maybe<MessagePayload>;
createReaction?: Maybe<Reaction>;
deleteChannel?: Maybe<Scalars['Int']>;
deleteFriend?: Maybe<User>;
deleteGallery?: Maybe<Scalars['Int']>;
deleteReaction?: Maybe<Scalars['Int']>;
findPassword?: Maybe<Scalars['Boolean']>;
removeNotificationToken?: Maybe<Scalars['Int']>;
sendVerification?: Maybe<Scalars['Boolean']>;
Expand Down Expand Up @@ -197,6 +200,12 @@ export type MutationCreateMessageArgs = {
};


export type MutationCreateReactionArgs = {
messageId: Scalars['ID'];
type: Scalars['String'];
};


export type MutationDeleteChannelArgs = {
channelId: Scalars['ID'];
};
Expand All @@ -212,6 +221,11 @@ export type MutationDeleteGalleryArgs = {
};


export type MutationDeleteReactionArgs = {
reactionId: Scalars['ID'];
};


export type MutationFindPasswordArgs = {
email: Scalars['String'];
};
Expand Down Expand Up @@ -340,6 +354,15 @@ export type QueryUsersArgs = {
after?: Maybe<Scalars['String']>;
};

export type Reaction = {
__typename?: 'Reaction';
createdAt?: Maybe<Scalars['DateTime']>;
deletedAt?: Maybe<Scalars['DateTime']>;
id: Scalars['ID'];
type?: Maybe<Scalars['String']>;
updatedAt?: Maybe<Scalars['DateTime']>;
};

export type Reply = {
__typename?: 'Reply';
createdAt?: Maybe<Scalars['DateTime']>;
Expand Down Expand Up @@ -546,6 +569,7 @@ export type ResolversTypes = {
UserModeType: UserModeType,
Message: ResolverTypeWrapper<Message>,
Photo: ResolverTypeWrapper<Photo>,
Reaction: ResolverTypeWrapper<Reaction>,
Reply: ResolverTypeWrapper<Reply>,
ChannelType: ChannelType,
Gallery: ResolverTypeWrapper<Gallery>,
Expand Down Expand Up @@ -588,6 +612,7 @@ export type ResolversParentTypes = {
UserModeType: UserModeType,
Message: Message,
Photo: Photo,
Reaction: Reaction,
Reply: Reply,
ChannelType: ChannelType,
Gallery: Gallery,
Expand Down Expand Up @@ -691,6 +716,7 @@ export type MessageResolvers<ContextType = MyContext, ParentType extends Resolve
filePath?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>,
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>,
picture?: Resolver<Maybe<Array<Maybe<ResolversTypes['Photo']>>>, ParentType, ContextType>,
reactions?: Resolver<Maybe<Array<Maybe<ResolversTypes['Reaction']>>>, ParentType, ContextType>,
replies?: Resolver<Maybe<Array<Maybe<ResolversTypes['Reply']>>>, ParentType, ContextType>,
sender?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>,
text?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>,
Expand All @@ -712,9 +738,11 @@ export type MutationResolvers<ContextType = MyContext, ParentType extends Resolv
createChannel?: Resolver<Maybe<ResolversTypes['Channel']>, ParentType, ContextType, RequireFields<MutationCreateChannelArgs, never>>,
createGallery?: Resolver<Maybe<ResolversTypes['Gallery']>, ParentType, ContextType, RequireFields<MutationCreateGalleryArgs, 'photoURL'>>,
createMessage?: Resolver<Maybe<ResolversTypes['MessagePayload']>, ParentType, ContextType, RequireFields<MutationCreateMessageArgs, 'message' | 'channelId'>>,
createReaction?: Resolver<Maybe<ResolversTypes['Reaction']>, ParentType, ContextType, RequireFields<MutationCreateReactionArgs, 'messageId' | 'type'>>,
deleteChannel?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType, RequireFields<MutationDeleteChannelArgs, 'channelId'>>,
deleteFriend?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType, RequireFields<MutationDeleteFriendArgs, 'friendId'>>,
deleteGallery?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType, RequireFields<MutationDeleteGalleryArgs, 'galleryId'>>,
deleteReaction?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType, RequireFields<MutationDeleteReactionArgs, 'reactionId'>>,
findPassword?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType, RequireFields<MutationFindPasswordArgs, 'email'>>,
removeNotificationToken?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType, RequireFields<MutationRemoveNotificationTokenArgs, 'token'>>,
sendVerification?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType, RequireFields<MutationSendVerificationArgs, 'email'>>,
Expand Down Expand Up @@ -766,6 +794,15 @@ export type QueryResolvers<ContextType = MyContext, ParentType extends Resolvers
users?: Resolver<Maybe<ResolversTypes['UsersConnection']>, ParentType, ContextType, RequireFields<QueryUsersArgs, never>>,
};

export type ReactionResolvers<ContextType = MyContext, ParentType extends ResolversParentTypes['Reaction'] = ResolversParentTypes['Reaction']> = {
createdAt?: Resolver<Maybe<ResolversTypes['DateTime']>, ParentType, ContextType>,
deletedAt?: Resolver<Maybe<ResolversTypes['DateTime']>, ParentType, ContextType>,
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>,
type?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>,
updatedAt?: Resolver<Maybe<ResolversTypes['DateTime']>, ParentType, ContextType>,
__isTypeOf?: isTypeOfResolverFn<ParentType>,
};

export type ReplyResolvers<ContextType = MyContext, ParentType extends ResolversParentTypes['Reply'] = ResolversParentTypes['Reply']> = {
createdAt?: Resolver<Maybe<ResolversTypes['DateTime']>, ParentType, ContextType>,
deletedAt?: Resolver<Maybe<ResolversTypes['DateTime']>, ParentType, ContextType>,
Expand Down Expand Up @@ -842,6 +879,7 @@ export type Resolvers<ContextType = MyContext> = {
PageInfo?: PageInfoResolvers<ContextType>,
Photo?: PhotoResolvers<ContextType>,
Query?: QueryResolvers<ContextType>,
Reaction?: ReactionResolvers<ContextType>,
Reply?: ReplyResolvers<ContextType>,
Subscription?: SubscriptionResolvers<ContextType>,
Upload?: GraphQLScalarType,
Expand Down
44 changes: 44 additions & 0 deletions src/models/Reaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
BuildOptions,
Model,
STRING,
TEXT,
UUID,
UUIDV4,
} from 'sequelize';

import Message from './Message';
import User from './User';
import sequelize from '../db';

class Reaction extends Model {
public id!: string;
public type: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public readonly deletedAt!: Date;
}

Reaction.init({
id: {
type: UUID,
defaultValue: UUIDV4,
allowNull: false,
primaryKey: true,
},
type: STRING,
}, {
sequelize,
modelName: 'reaction',
timestamps: true,
paranoid: true,
});

Reaction.belongsTo(Message, { as: 'message' });
Reaction.belongsTo(User, { as: 'user' });

export type ReactionModelStatic = typeof Model & {
new (values?: object, options?: BuildOptions): Reaction;
}

export default Reaction as ReactionModelStatic;
3 changes: 3 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Membership, { MembershipModelStatic } from './Membership';
import Message, { MessageModelStatic } from './Message';
import Notification, { NotificationModelStatic } from './Notification';
import Photo, { PhotoModelStatic } from './Photo';
import Reaction, { ReactionModelStatic } from './Reaction';
import Reply, { ReplyModelStatic } from './Reply';
import User, { UserModelStatic } from './User';

Expand All @@ -18,6 +19,7 @@ export default {
Photo,
Reply,
Gallery,
Reaction,
};

export interface ModelType {
Expand All @@ -30,4 +32,5 @@ export interface ModelType {
Notification: NotificationModelStatic;
Photo: PhotoModelStatic;
Gallery: GalleryModelStatic;
Reaction: ReactionModelStatic;
};
3 changes: 3 additions & 0 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import File from './file';
import Friend from './friend';
import Gallery from './gallery';
import Notification from './notification';
import Reaction from './reaction';
import User from './user';

export const allResolvers = [
Expand All @@ -14,6 +15,7 @@ export const allResolvers = [
Channel,
Gallery,
File,
Reaction,
];

export default {
Expand All @@ -24,4 +26,5 @@ export default {
Channel,
Gallery,
File,
Reaction,
};
46 changes: 46 additions & 0 deletions src/resolvers/reaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Reaction, Resolvers } from '../generated/graphql';

import { checkAuth } from '../utils/auth';

const resolver: Resolvers = {
Mutation: {
createReaction: async (
_,
{ messageId, type },
{ verifyUser, models },
): Promise<Reaction> => {
const auth = verifyUser();
checkAuth(auth);

const { Reaction: reactionModel } = models;

const reaction = await reactionModel.create({
messageId,
userId: auth.userId,
type,
});

return reaction;
},
deleteReaction: async (
_,
{ reactionId },
{ verifyUser, models },
): Promise<number> => {
const auth = verifyUser();
checkAuth(auth);

const { Reaction: reactionModel } = models;

const result = await reactionModel.destroy({
where: {
id: reactionId,
},
});

return result;
},
},
};

export default resolver;
Loading