Skip to content

Commit

Permalink
Merge pull request #4 from asuc-octo/user-graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyzollman authored Feb 18, 2023
2 parents 68d30e6 + cd533ef commit f4957f6
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 65 deletions.
43 changes: 37 additions & 6 deletions src/generated-types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,38 @@ export type Grade = {
export type Query = {
__typename?: 'Query';
Enrollment?: Maybe<Enrollment>;
User?: Maybe<User>;
grades?: Maybe<Array<Maybe<Grade>>>;
users?: Maybe<Array<Maybe<User>>>;
};


export type QueryEnrollmentArgs = {
classId: Scalars['String'];
};


export type QueryUserArgs = {
email: Scalars['String'];
};

export type User = {
__typename?: 'User';
email?: Maybe<Scalars['String']>;
google_id?: Maybe<Scalars['String']>;
date_joined: Scalars['String'];
email: Scalars['String'];
email_berkeleytime_update?: Maybe<Scalars['Boolean']>;
email_class_update?: Maybe<Scalars['Boolean']>;
email_enrollment_opening?: Maybe<Scalars['Boolean']>;
email_grade_update?: Maybe<Scalars['Boolean']>;
first_name: Scalars['String'];
id: Scalars['String'];
is_active: Scalars['Boolean'];
is_staff: Scalars['Boolean'];
is_superuser: Scalars['Boolean'];
last_login?: Maybe<Scalars['String']>;
last_name: Scalars['String'];
major?: Maybe<Array<Maybe<Scalars['String']>>>;
password: Scalars['String'];
username: Scalars['String'];
};


Expand Down Expand Up @@ -166,14 +184,27 @@ export type GradeResolvers<ContextType = any, ParentType extends ResolversParent

export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
Enrollment?: Resolver<Maybe<ResolversTypes['Enrollment']>, ParentType, ContextType, RequireFields<QueryEnrollmentArgs, 'classId'>>;
User?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType, RequireFields<QueryUserArgs, 'email'>>;
grades?: Resolver<Maybe<Array<Maybe<ResolversTypes['Grade']>>>, ParentType, ContextType>;
users?: Resolver<Maybe<Array<Maybe<ResolversTypes['User']>>>, ParentType, ContextType>;
};

export type UserResolvers<ContextType = any, ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User']> = {
email?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
google_id?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
date_joined?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
email?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
email_berkeleytime_update?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType>;
email_class_update?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType>;
email_enrollment_opening?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType>;
email_grade_update?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType>;
first_name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
is_active?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
is_staff?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
is_superuser?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
last_login?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
last_name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
major?: Resolver<Maybe<Array<Maybe<ResolversTypes['String']>>>, ParentType, ContextType>;
password?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
username?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down
13 changes: 9 additions & 4 deletions src/modules/user/controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { User } from "../../generated-types/graphql";
import { formatUser } from "./formatter";
import { UserModel } from "./model";
import { UserModel, UserType } from "./model";

export async function users() {
const users = await UserModel.find();
export async function getByEmail(email: string): Promise<User> {
const user = await UserModel.findOne({ email });

return users.map(formatUser);
if (!user) {
throw new Error("User not found");
}

return formatUser(user as UserType);
}
25 changes: 0 additions & 25 deletions src/modules/user/fixture.ts

This file was deleted.

24 changes: 19 additions & 5 deletions src/modules/user/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import { UserModule } from "./generated-types/module-types";
import { UserType } from "./model";

export function formatUser(user: UserType): UserModule.User {
return {
id: user._id.toString(),
google_id: user.google_id,
email: user.email,
};
return Object.assign({
id: user._id as string,
password: user.password as string,
is_superuser: user.is_superuser as boolean,
username: user.username as string,
first_name: user.first_name as string,
last_name: user.last_name as string,
email: user.email as string,
is_staff: user.is_staff as boolean,
is_active: user.is_active as boolean,
date_joined: user.date_joined as string,
},
user.last_login === null ? null : { last_login: user.last_login },
user.major === null ? [null] : { major: user.major },
user.email_class_update === null ? null : { last_login: user.email_class_update },
user.email_grade_update === null ? null : { last_login: user.email_grade_update },
user.email_enrollment_opening === null ? null : { last_login: user.email_enrollment_opening },
user.email_berkeleytime_update === null ? null : { last_login: user.email_berkeleytime_update }
);
}
21 changes: 17 additions & 4 deletions src/modules/user/generated-types/module-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as Types from "../../../generated-types/graphql";
import * as gm from "graphql-modules";
export namespace UserModule {
interface DefinedFields {
User: 'id' | 'google_id' | 'email';
Query: 'users';
User: 'id' | 'password' | 'last_login' | 'is_superuser' | 'username' | 'first_name' | 'last_name' | 'email' | 'is_staff' | 'is_active' | 'date_joined' | 'major' | 'email_class_update' | 'email_grade_update' | 'email_enrollment_opening' | 'email_berkeleytime_update';
Query: 'User';
};

export type User = Pick<Types.User, DefinedFields['User']>;
Expand All @@ -24,12 +24,25 @@ export namespace UserModule {
User?: {
'*'?: gm.Middleware[];
id?: gm.Middleware[];
google_id?: gm.Middleware[];
password?: gm.Middleware[];
last_login?: gm.Middleware[];
is_superuser?: gm.Middleware[];
username?: gm.Middleware[];
first_name?: gm.Middleware[];
last_name?: gm.Middleware[];
email?: gm.Middleware[];
is_staff?: gm.Middleware[];
is_active?: gm.Middleware[];
date_joined?: gm.Middleware[];
major?: gm.Middleware[];
email_class_update?: gm.Middleware[];
email_grade_update?: gm.Middleware[];
email_enrollment_opening?: gm.Middleware[];
email_berkeleytime_update?: gm.Middleware[];
};
Query?: {
'*'?: gm.Middleware[];
users?: gm.Middleware[];
User?: gm.Middleware[];
};
};
}
30 changes: 16 additions & 14 deletions src/modules/user/model.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import mongoose, { Schema, InferSchemaType, Types } from "mongoose";

export const UserSchema = new Schema({
_id: { type: Types.ObjectId, required: true },
_created: String,
_updated: String,
_version: Number,
access_token: String,
bio: String,
email: String,
google_id: String,
notify_update_classes: Boolean,
notify_update_grades: Boolean,
notify_update_berkeleytime: Boolean,
classes_saved: [String],
classes_watching: [String],
friends: [String],
_id: { type: Types.ObjectId, required: true },
password: { type: String, required: true },
last_login: { type: String, required: true },
is_superuser: { type: Boolean, required: false },
username: { type: String, required: true },
first_name: { type: String, required: true },
last_name: { type: String, required: true },
email: { type: String, required: true },
is_staff: { type: Boolean, required: true },
is_active: { type: Boolean, required: true },
date_joined: { type: String, required: true },
major: { type: [String], required: false },
email_class_update: { type: Boolean, required: false },
email_grade_update: { type: Boolean, required: false },
email_enrollment_opening: { type: Boolean, required: false },
email_berkeleytime_update: { type: Boolean, required: false }
});

export const UserModel = mongoose.model("user", UserSchema, "user");
Expand Down
10 changes: 6 additions & 4 deletions src/modules/user/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { users } from "./controller";
import { getByEmail } from "./controller";
import { UserModule } from "./generated-types/module-types";

const resolvers: UserModule.Resolvers = {
Query: {
users,
},
Query: {
User(_parent, args: { email: string }) {
return getByEmail(args.email);
},
},
};

export default resolvers;
19 changes: 16 additions & 3 deletions src/modules/user/typedefs/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ import { gql } from "graphql-tag";
const typedef = gql`
type User {
id: String!
google_id: String
email: String
password: String!
last_login: String
is_superuser: Boolean!
username: String!
first_name: String!
last_name: String!
email: String!
is_staff: Boolean!
is_active: Boolean!
date_joined: String!
major: [String],
email_class_update: Boolean,
email_grade_update: Boolean,
email_enrollment_opening: Boolean,
email_berkeleytime_update: Boolean
}
type Query {
users: [User]
User(email: String!): User
}
`;

Expand Down

0 comments on commit f4957f6

Please sign in to comment.