How to use the apollo-server-koa.ApolloError function in apollo-server-koa

To help you get started, we’ve selected a few apollo-server-koa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github velopert / velog-server / src / graphql / series.ts View on Github external
}

      // reorder series
      // validate series order
      const seriesPostsRepo = getRepository(SeriesPosts);
      const seriesPosts = await seriesPostsRepo.find({
        where: {
          fk_series_id: id
        }
      });

      const valid =
        seriesPosts.every(sp => series_order.includes(sp.id)) &&
        seriesPosts.length === series_order.length;
      if (!valid) {
        throw new ApolloError('series_order is invalid', 'BAD_REQUEST');
      }

      // figure out which data to update
      const seriesPostsById = normalize(seriesPosts, sp => sp.id);
      type Update = { id: string; index: number };
      const updates = series_order.reduce((acc, current, index) => {
        const sp = seriesPostsById[current];
        console.log(sp.index, index + 1);
        if (sp.index !== index + 1) {
          // index mismatch
          acc.push({
            id: current,
            index: index + 1
          });
          return acc;
        }
github velopert / velog-server / src / graphql / series.ts View on Github external
const { series_id, post_id } = args as AppendToSeriesArgs;
      await getSeriesIfValid(series_id, ctx.user_id);

      const seriesPostsRepo = getRepository(SeriesPosts);
      const seriesPostsList = await seriesPostsRepo.find({
        where: {
          fk_series_id: series_id
        },
        order: {
          index: 'ASC'
        }
      });
      const exists = seriesPostsList.find(sp => sp.fk_post_id === post_id);

      if (exists) {
        throw new ApolloError('Already added to series', 'CONFLICT');
      }

      const nextIndex =
        seriesPostsList.length === 0 ? 1 : seriesPostsList[seriesPostsList.length - 1].index + 1;

      // create new seriesPost
      const seriesPosts = new SeriesPosts();
      seriesPosts.fk_post_id = post_id;
      seriesPosts.fk_series_id = series_id;
      seriesPosts.index = nextIndex;

      // save
      await seriesPostsRepo.save(seriesPosts);
      return nextIndex;
    },
    editSeries: async (parent, args, ctx) => {
github velopert / velog-server / src / graphql / post.ts View on Github external
createPostHistory: async (parent: any, args: CreatePostHistoryArgs, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }

      // check ownPost
      const { post_id, title, body, is_markdown } = args;

      const postRepo = getRepository(Post);
      const post = await postRepo.findOne(post_id);

      if (!post) {
        throw new ApolloError('Post not found', 'NOT_FOUND');
      }
      if (post.fk_user_id !== ctx.user_id) {
        throw new ApolloError('This post is not yours', 'NO_PERMISSION');
      }

      // create postHistory
      const postHistoryRepo = getRepository(PostHistory);
      const postHistory = new PostHistory();
      Object.assign(postHistory, { title, body, is_markdown, fk_post_id: post_id });

      await postHistoryRepo.save(postHistory);

      const [data, count] = await postHistoryRepo.findAndCount({
        where: {
          fk_post_id: post_id
        },
github velopert / velog-server / src / graphql / post.ts View on Github external
query.andWhere('is_temp = true');
      } else {
        query.andWhere('is_temp = false');
      }

      if (username) {
        query.andWhere('user.username = :username', { username });
      }

      // pagination
      if (cursor) {
        const post = await getRepository(Post).findOne({
          id: cursor
        });
        if (!post) {
          throw new ApolloError('invalid cursor');
        }
        query.andWhere('post.released_at < :date', {
          date: post.released_at,
          id: post.id
        });
        query.orWhere('post.released_at = :date AND post.id < :id', {
          date: post.released_at,
          id: post.id
        });
      }

      // show private posts
      if (context.user_id) {
        query.orWhere('post.is_private = true and post.fk_user_id = :user_id', {
          user_id: context.user_id
        });
github velopert / velog-server / src / graphql / post.ts View on Github external
posts: async (parent: any, { cursor, limit = 20, username, temp_only }: PostsArgs, context) => {
      const query = getManager()
        .createQueryBuilder(Post, 'post')
        .limit(limit)
        .orderBy('post.released_at', 'DESC')
        .addOrderBy('post.id', 'DESC')
        .leftJoinAndSelect('post.user', 'user')
        .where('is_private = false');

      if (temp_only) {
        if (!username) throw new ApolloError('username is missing', 'BAD_REQUEST');
        const userRepo = getRepository(User);
        const user = await userRepo.findOne({
          where: {
            username
          }
        });
        if (!user) throw new ApolloError('Invalid username', 'NOT_FOUND');
        if (user.id !== context.user_id) {
          throw new ApolloError('You have no permission to load temp posts', 'NO_PERMISSION');
        }
        query.andWhere('is_temp = true');
      } else {
        query.andWhere('is_temp = false');
      }

      if (username) {
github velopert / velog-server / src / graphql / series.ts View on Github external
createSeries: async (parent: any, args, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const { name, url_slug } = args as CreateSeriesArgs;
      const seriesRepo = getRepository(Series);
      const exists = await seriesRepo.findOne({
        where: {
          name
        }
      });
      if (exists) {
        throw new ApolloError('URL Slug already exists', 'ALREADY_EXISTS');
      }
      const series = new Series();
      series.fk_user_id = ctx.user_id;
      series.name = name;
      series.url_slug = url_slug;
      await seriesRepo.save(series);
      return series;
    },
    appendToSeries: async (parent, args, ctx) => {
github velopert / velog-server / src / graphql / user.ts View on Github external
update_about: async (parent: any, args: any, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const userProfileRepo = getRepository(UserProfile);
      const profile = await userProfileRepo.findOne({
        where: {
          fk_user_id: ctx.user_id
        }
      });
      const { about } = args as { about: string };
      if (!profile) {
        throw new ApolloError('Failed to retrieve user profile');
      }
      profile.about = about || '';
      await userProfileRepo.save(profile);
      return profile;
    }
  }
github velopert / velog-server / src / graphql / comment.ts View on Github external
removeComment: async (parent: any, { id }: any, ctx) => {
      const commentRepo = getRepository(Comment);
      const comment = await commentRepo.findOne(id);

      if (!comment) {
        throw new ApolloError('Comment not found');
      }
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      if (ctx.user_id !== comment.fk_user_id) {
        throw new ApolloError('No permission');
      }

      comment.deleted = true;
      await commentRepo.save(comment);

      const postScoreRepo = getRepository(PostScore);
      const score = await postScoreRepo
        .createQueryBuilder()
        .where('fk_post_id = :postId', { postId: comment.fk_post_id })
        .andWhere('fk_user_id = :userId', { userId: ctx.user_id })
        .andWhere("type = 'COMMENT'")
        .orderBy('created_at', 'DESC')
        .getOne();

      if (score) {
        postScoreRepo.delete(score.id);
github velopert / velog-server / src / graphql / comment.ts View on Github external
writeComment: async (parent: any, args, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const { post_id, comment_id, text } = args as WriteCommentArgs;
      const post = await getRepository(Post).findOne(post_id);
      if (!post) {
        throw new ApolloError('Post not found', 'NOT_FOUND');
      }
      const commentRepo = getRepository(Comment);
      const comment = new Comment();

      if (comment_id) {
        const commentTarget = await commentRepo.findOne(comment_id);
        if (!commentTarget) {
          throw new ApolloError('Target comment is not found', 'NOT_FOUND');
        }
        comment.level = commentTarget.level + 1;
        comment.reply_to = comment_id;

        if (comment.level >= 4) {
          throw new ApolloError('Maximum comment level is 3', 'BAD_REQUEST');
        }
github velopert / velog-server / src / graphql / series.ts View on Github external
async function getSeriesIfValid(seriesId: string, userId: string | null) {
  if (!userId) {
    throw new AuthenticationError('Not Logged In');
  }
  const seriesRepo = getRepository(Series);
  const series = await seriesRepo.findOne(seriesId);
  if (!series) {
    throw new ApolloError('Series not found', 'NOT_FOUND');
  }
  if (series.fk_user_id !== userId) {
    throw new ApolloError('This series is not yours', 'NO_PERMISSION');
  }

  return series;
}