Skip to content

Commit

Permalink
Improve performance of me query and move functionality into auth mode…
Browse files Browse the repository at this point in the history
…ls (#320)

* make me operation more efficient and move to each auth model vs in User

* fix formatting issues

* format fixes

Co-authored-by: Matt S. Posner <mposner@us.ibm.com>
  • Loading branch information
mposner305 and Matt S. Posner committed May 6, 2020
1 parent c0584cb commit 09aed24
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
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

0 comments on commit 09aed24

Please sign in to comment.