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

Improve performance of me query and move functionality into auth models #320

Merged
merged 3 commits into from
May 6, 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
18 changes: 18 additions & 0 deletions app/apollo/models/user.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ UserLocalSchema.statics.createToken = async (user, secret, expiresIn) => {
});
};

UserLocalSchema.statics.getCurrentUser = ({me , req_id, logger}) => {
let result = me;
if (result != null) {
result = {
type: me.type,
id: me._id,
email: me.email,
identifier: me.identifier,
org_id: me.org_id,
role: me.role,
meta: me.meta,
};
} else {
logger.debug(`Can not locate the user for the user _id: ${me._id} for the request ${req_id}`);
}
return result;
};

UserLocalSchema.statics.signUp = async (models, args, secret, context) => {
logger.debug({ req_id: context.req_id }, `local signUp ${args}`);
if (AUTH_MODEL === AUTH_MODELS.LOCAL) {
Expand Down
20 changes: 20 additions & 0 deletions app/apollo/models/user.passport.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ UserPassportLocalSchema.statics.createToken = async (
});
};

UserPassportLocalSchema.statics.getCurrentUser = ({me , req_id, logger}) => {
let result = me;
if (result != null) {
result = {
type: me.type,
id: me._id,
email: me.email,
identifier: me.identifier,
org_id: me.org_id,
role: me.role,
meta: me.meta,
};
} else {
logger.debug(`Can not locate the user for the user _id: ${me._id} for the request ${req_id}`);
}
return result;
};



UserPassportLocalSchema.statics.signUp = async (models, args, secret, context) => {
logger.debug( { req_id: context.req_id }, `passport.local signUp: ${args}`);
if (AUTH_MODEL === AUTH_MODELS.PASSPORT_LOCAL) {
Expand Down
22 changes: 4 additions & 18 deletions app/apollo/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@

const userResolvers = {
Query: {
me: async (parent, args, { models, me , req_id, logger }) => {
me: async (parent, args, context) => {
const { models, me , req_id, logger } = context;
if (!me) {
logger.debug(`There is no user information on this context for the request ${req_id}`);
return null;
}
// TODO: we probably skip database query and directly return user info from
// JWT token.
let result = await models.User.findOne({ _id: me._id });
if (result != null) {
result = {
type: result.type,
id: result.getId(),
email: result.getEmail(),
identifier: typeof result.getIdentifier === 'function' ? result.getIdentifier() : null,
org_id: result.getCurrentOrgId(),
role: result.getCurrentRole(),
meta: result.getMeta(),
};
} else {
logger.debug(`Can not locate the user for the user _id: ${me._id} for the request ${req_id}`);
}
return result;

return models.User.getCurrentUser(context);
},
},

Expand Down
3 changes: 2 additions & 1 deletion app/apollo/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,13 @@ describe('user graphql', () => {
try {
token = await signUpUser(models, api, user02Data);
console.log(`user01 token=${token}`);

const {
data: {
data: { me },
},
} = await api.me(token);
console.log(JSON.stringify(me, null, 4));

expect(me.id).to.be.a('string');
expect(me.email).to.be.a('string');
expect(me.org_id).to.be.a('string');
Expand Down