Skip to content
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 24 commits into from
Dec 16, 2021

Conversation

noghartt
Copy link
Owner

@noghartt noghartt commented Dec 15, 2021

Description

With this PR, we are adding these mutations: communityCreate and communityJoin that will be responsible to create and join communities based on communityId.

communityCreate

To run this mutation, you just run it:

mutation communityCreate($displayName: String!, $communityId: String!) {
  communityCreate(
    input: { displayName: $displayName, communityId: $communityId }
  ) {
    community {
      id
      name
      displayName
      members
    }
  }
}

communityJoin

To run this mutation, you just run it:

mutation communityJoin($communityId: String!) {
  communityJoin(input: { communityId: $communityId }) {
    community {
      id
      members
    }
    me {
      id
      communities
    }
  }
}

Observations

  • The tests for these mutations were added too;
  • We bump the version of koa-graphql to ^v0.12.0;
  • We change the dependency from koa-router to @koa/router (is the same package);
  • We add user object to context from each request;
    • Maybe it can be refactored in the future to validate if the user is logged in on a middleware.

): Promise<Maybe<UserDocument>> => {
if (!token) return null;

// TODO: Maybe it should be a crime

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

Copy link
Owner Author

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

ctx: GraphQLContext,
) => {
// TODO: In some way, you can pass it to a middleware. But IDK how to do it yet.
if (!ctx.user) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve: async ({ communityId }) =>
await CommunityModel.findOne({ _id: communityId }),
},
me: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create the auth auth middleware based on this https://github.com/daniloab/rbaf-graphql-api/blob/master/src/server/app.ts#L18

then, expose me to the query type, and should be enough. This will help you to avoid declaring me on mutation output fields.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand exactly what you suggest here, how can I do it?

const community = await CommunityModel.findOne({ name: communityId });

if (!community) {
throw new Error("This community doesn't exist. Please, try again.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can return errors on graphql output mutation

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it, but for now, I intend to keep throw because I can more objectively handle the error on the front-end.

But I guess that it's one of the ways that I think to implement error handling on the "server-side". I'm just waiting to answer my question here to decide how I deal with errors.

community.updateOne({
$addToSet: { members: [...community.members, ctx.user._id] },
}),
ctx.user.updateOne({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to get the user itself from MongoDB and then update it.

implement dataloaders to help you with this.

}

interface UserDocument extends User, Document {
export interface UserDocument extends User, Document {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IUser could be cool

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the I prefix on interfaces is unnecessary because in all modern editors (and IDEs) you can know that UserDocument is an interface, I think that this prefix isn't necessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

Copy link

@daniloab daniloab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome pr

Copy link

@daniloab daniloab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge this and send the changes into small prs

@noghartt
Copy link
Owner Author

merge this and send the changes into small prs

Sure! I'll create issues with the rest of the changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants