Skip to content
Snippets Groups Projects
Commit 2ae96405 authored by Jure's avatar Jure
Browse files

feat(model-user): improve eager loading in graphql

parent 1b5f7be0
No related branches found
No related tags found
No related merge requests found
const logger = require('@pubsweet/logger') const logger = require('@pubsweet/logger')
const User = require('./user')
const eager = 'teams.members.[user, alias]'
const resolvers = { const resolvers = {
Query: { Query: {
user(_, { id }, ctx) { user(_, { id }, ctx) {
return ctx.connectors.User.fetchOne(id, ctx) return ctx.connectors.User.fetchOne(id, ctx, { eager })
}, },
users(_, { where }, ctx) { users(_, { where }, ctx) {
return ctx.connectors.User.fetchAll(where, ctx, { eager: 'teams' }) return ctx.connectors.User.fetchAll(where, ctx, { eager })
}, },
// Authentication // Authentication
currentUser(_, vars, ctx) { currentUser(_, vars, ctx) {
if (!ctx.user) return null if (!ctx.user) return null
return User.find(ctx.user) return ctx.connectors.User.model.find(ctx.user)
}, },
}, },
Mutation: { Mutation: {
...@@ -33,13 +34,13 @@ const resolvers = { ...@@ -33,13 +34,13 @@ const resolvers = {
return ctx.connectors.User.update(id, input, ctx) return ctx.connectors.User.update(id, input, ctx)
}, },
// Authentication // Authentication
async loginUser(_, { input }) { async loginUser(_, { input }, ctx) {
const authentication = require('pubsweet-server/src/authentication') const authentication = require('pubsweet-server/src/authentication')
let isValid = false let isValid = false
let user let user
try { try {
user = await User.findByUsername(input.username) user = await ctx.connectors.User.model.findByUsername(input.username)
isValid = await user.validPassword(input.password) isValid = await user.validPassword(input.password)
} catch (err) { } catch (err) {
logger.debug(err) logger.debug(err)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment