-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write mutations to: create a community and join a community #41
Merged
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
fab0fa7
build(deps): update version of koa-graphql dependency
noghartt 796eddd
feat: add headerEditorEnabled on graphiql option on koa-graphql
noghartt 7c52e92
feat: write a auth function to get user if exists
noghartt d49a8d6
feat: add user to context on graphql request
noghartt b4c70bd
feat: add communities array as field on the user document
noghartt 9aae6dd
feat: write CommunityModel
noghartt cbe405b
feat: write CommunityType
noghartt 1ab48e2
feat: write createCommunityMutation
noghartt 26e0ffd
feat: write joinCommunityMutation
noghartt fce6b07
test: write a method to upsert models on tests
noghartt b2ecbaf
style: improve types on createUser fixture function
noghartt 38fb0fb
test: write a fixture to create communities
noghartt 7dd3290
test: write tests to createCommunityMutation
noghartt 8a2fe45
test: write tests to joinCommunity.test.ts
noghartt ccb831f
build(tsconfig): exclude test files and fixtures when build server
noghartt 9c530da
chore(gitignore): fix /graphql folder on gitignore
noghartt 033f29a
style: add setting to detect react version on eslint config
noghartt 7fe1e33
feat: write graphql modules with initial GraphQLContext type
noghartt 97f6002
style: rename community mutations to avoid redundancy
noghartt 671a6bf
fix(community): instantiates a new model with correctly properties
noghartt 0468961
style: move outputFields on community mutations down from resolvers
noghartt 49c02f5
style: change findedCommunity to communityFound
noghartt 6370492
test: rename community mutation tests to according with mutations
noghartt c746bcf
feat: add ref prop to fields on Community schema
noghartt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
graphql/ | ||
/graphql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
import { Maybe } from '@fakeddit/types'; | ||
|
||
import { UserModel, UserDocument } from './modules/user/UserModel'; | ||
import { config } from './environment'; | ||
|
||
export const getUser = async ( | ||
token: Maybe<string>, | ||
): Promise<Maybe<UserDocument>> => { | ||
if (!token) return null; | ||
|
||
// TODO: Maybe it should be a crime | ||
[, token] = token.split('JWT '); | ||
|
||
const decodedToken = jwt.verify(token, config.JWT_SECRET) as { id: string }; | ||
|
||
const user = await UserModel.findOne({ _id: decodedToken.id }); | ||
|
||
if (!user) return null; | ||
|
||
return user; | ||
}; | ||
|
||
export const generateJwtToken = (userId: string) => | ||
`JWT ${jwt.sign({ id: userId }, config.JWT_SECRET)}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import mongoose, { Schema, Types } from 'mongoose'; | ||
|
||
export interface Community { | ||
name: string; | ||
displayName: string; | ||
admin: Types.ObjectId; | ||
members: Types.ObjectId[]; | ||
mods: Types.ObjectId[]; | ||
} | ||
|
||
const CommunitySchema = new Schema<Community>( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
maxlength: 21, | ||
}, | ||
admin: { | ||
type: Schema.Types.ObjectId, | ||
noghartt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
required: true, | ||
}, | ||
displayName: { | ||
type: String, | ||
required: true, | ||
}, | ||
mods: [Schema.Types.ObjectId], | ||
members: [Schema.Types.ObjectId], | ||
}, | ||
{ | ||
timestamps: { | ||
createdAt: true, | ||
updatedAt: true, | ||
}, | ||
}, | ||
); | ||
|
||
export const CommunityModel = mongoose.model('Community', CommunitySchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
GraphQLObjectType, | ||
GraphQLString, | ||
GraphQLList, | ||
GraphQLID, | ||
GraphQLNonNull, | ||
} from 'graphql'; | ||
import { globalIdField } from 'graphql-relay'; | ||
|
||
import { Community } from './CommunityModel'; | ||
|
||
export const CommunityType = new GraphQLObjectType<Community>({ | ||
name: 'Community', | ||
fields: { | ||
id: globalIdField('Community'), | ||
name: { | ||
type: new GraphQLNonNull(GraphQLString), | ||
resolve: community => community.name, | ||
description: 'The slugged name of the community - this is unique', | ||
}, | ||
displayName: { | ||
type: new GraphQLNonNull(GraphQLString), | ||
resolve: community => community.displayName, | ||
description: | ||
"Some custom name that doens't necessary be the name of the community", | ||
}, | ||
admin: { | ||
type: new GraphQLNonNull(GraphQLID), | ||
}, | ||
mods: { | ||
type: new GraphQLNonNull(new GraphQLList(GraphQLID)), | ||
resolve: community => community.mods, | ||
}, | ||
members: { | ||
// TODO: Turn it a list of ObjectIDs | ||
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))), | ||
resolve: community => community.members, | ||
description: | ||
'A list containing the IDs of all users that is member of this community', | ||
}, | ||
}, | ||
}); |
133 changes: 133 additions & 0 deletions
133
packages/server/src/modules/community/__tests__/createCommunity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { graphql } from 'graphql'; | ||
|
||
import { | ||
clearDatabaseAndRestartCounters, | ||
connectWithMongoose, | ||
disconnectWithMongoose, | ||
} from '../../../../test'; | ||
import { schema } from '../../../schema'; | ||
|
||
import { createUser } from '../../user/fixtures/createUser'; | ||
|
||
beforeAll(connectWithMongoose); | ||
beforeEach(clearDatabaseAndRestartCounters); | ||
afterAll(disconnectWithMongoose); | ||
|
||
it('should create a new community', async () => { | ||
const user = await createUser(); | ||
|
||
const mutation = ` | ||
mutation M($displayName: String!, $communityId: String!) { | ||
communityCreate( | ||
input: { displayName: $displayName, communityId: $communityId } | ||
) { | ||
community { | ||
id | ||
name | ||
displayName | ||
members | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const rootValue = {}; | ||
|
||
const variables = { | ||
displayName: 'A community to lovers of tests', | ||
communityId: 'WeLoveTests', | ||
}; | ||
|
||
const result = await graphql( | ||
schema, | ||
mutation, | ||
rootValue, | ||
{ user }, | ||
variables, | ||
); | ||
|
||
expect(result.errors).toBeUndefined(); | ||
|
||
const { community } = result?.data?.communityCreate; | ||
|
||
expect(community.id).toBeDefined(); | ||
expect(community.name).toBe(variables.communityId); | ||
expect(community.displayName).toBe(variables.displayName); | ||
expect(community.members).toHaveLength(1); | ||
expect(community.members).toContain(user._id.toString()); | ||
}); | ||
|
||
it("should not allow create a community if doesn't have authorization header", async () => { | ||
const mutation = ` | ||
mutation M($displayName: String!, $communityId: String!) { | ||
communityCreate( | ||
input: { displayName: $displayName, communityId: $communityId } | ||
) { | ||
community { | ||
id | ||
name | ||
displayName | ||
members | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const rootValue = {}; | ||
|
||
const variables = { | ||
displayName: 'A community to lovers of tests', | ||
communityId: 'WeLoveTests', | ||
}; | ||
|
||
const result = await graphql(schema, mutation, rootValue, {}, variables); | ||
|
||
expect(result?.data?.communityCreate).toBeNull(); | ||
|
||
expect(result?.errors).toBeDefined(); | ||
expect(result.errors && result.errors[0]?.message).toBe( | ||
'You are not logged in. Please, try again!', | ||
); | ||
}); | ||
|
||
it('should not create a duplicate community', async () => { | ||
const user = await createUser(); | ||
|
||
const mutation = ` | ||
mutation M($displayName: String!, $communityId: String!) { | ||
communityCreate( | ||
input: { displayName: $displayName, communityId: $communityId } | ||
) { | ||
community { | ||
id | ||
name | ||
displayName | ||
members | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const rootValue = {}; | ||
|
||
const variables = { | ||
displayName: 'A community to lovers of tests', | ||
communityId: 'WeLoveTests', | ||
}; | ||
|
||
await graphql(schema, mutation, rootValue, { user }, variables); | ||
const result = await graphql( | ||
schema, | ||
mutation, | ||
rootValue, | ||
{ user }, | ||
variables, | ||
); | ||
|
||
expect(result?.data?.communityCreate).toBeNull(); | ||
|
||
expect(result?.errors).toBeDefined(); | ||
expect(result.errors && result.errors[0].message).toBe( | ||
'A community with this name has already been created. Please, try again!', | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drastic measures are sometimes necessary lmao