import { ApolloError, ForbiddenError, } from 'apollo-server-micro'; import { queryField, idArg } from '@nexus/schema'; import { authenticateQuery } from '../../../schema/authenticate-query'; import { UserType } from './index'; import { userService } from '../user.service'; /** * Resolver to Fetch a given user */ export const userQF = queryField('user', { type: UserType, args: { id: idArg({ required: true, description: 'Id of the user' }) }, resolve: async (root, args, ctx, info) => { const { user, ability } = authenticateQuery(ctx, { requireUser: true }); const requestedUser = await userService(ctx.state.db) .fetchUserById(args.id, { ability }) .catch(err => { throw new ApolloError('Unknown Error'); }); if (requestedUser === null) { throw new ForbiddenError('You are not able to view this resource'); } return requestedUser; }, });