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

To help you get started, we’ve selected a few apollo-server-core 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 jauhararifin / ugrade / contest / src / contest / createContestResolver.ts View on Github external
// validate input
    const schemas = yup.object().shape({
      email: emailSchema.required(),
      shortId: contestShortIdSchema.required(),
      name: contestNameSchema.required(),
      shortDescription: contestShortDescriptionSchema.required(),
      description: contestDescriptionSchema.required(),
      startTime: startTimeSchema.required(),
      finishTime: finishTimeSchema.required(),
      permittedLanguageIds: permittedLanguagesSchema.required(),
    })
    try {
      args = await schemas.validate(args)
    } catch (error) {
      if (error instanceof yup.ValidationError) {
        throw new ApolloError('Invalid Input', 'INVALID_INPUT', error.errors)
      }
    }

    // check taken shortId
    try {
      await contestStore.getContestByShortId(args.shortId)
      throw new ApolloError(
        'Contest ID Already Taken',
        'CONTEST_ID_ALREADY_TAKEN'
      )
    } catch (error) {
      if (!(error instanceof NoSuchContest)) throw error
    }

    // check languages ids
    const availableLangs = await languageStore.getAvailableLanguages()
github chnirt / nestjs-graphql-best-practice / src / resolvers / auth.resolver.ts View on Github external
lastName: profile.name.familyName,
						gender: profile.gender === '' && Gender.UNKNOWN,
						avatar: profile.photos[0].value
					})
				)
			}

			return await tradeToken(user)
		}

		if (info) {
			// console.log(info)
			const { code } = info
			switch (code) {
				case 'ETIMEDOUT':
					throw new ApolloError('Failed to reach Google: Try Again')
				default:
					throw new ApolloError('Something went wrong')
			}
		}
	}
github jauhararifin / ugrade / server-legacy / src / contest / ContestResolver.ts View on Github external
async contest(@Arg('id', { nullable: false }) id: number): Promise {
    const contest = await ContestEntity.getRepository().findOne(id)
    if (contest) return contest
    throw new ApolloError('NoSuchContest', 'No Such Contest')
  }
github chnirt / nestjs-graphql-best-practice / src / resolvers / dish / dish.resolver.ts View on Github external
async dishesByShop(@Args('shopId') shopId: string): Promise {
		try {
			return await this.dishRepository.find({
				where: { shopId, isActive: true },
				order: { createdAt: 'DESC' }
			})
		} catch (error) {
			throw new ApolloError(error)
		}
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / dish / dish.resolver.ts View on Github external
async dish(@Args('id') id: string): Promise {
		try {
			return await this.dishRepository.findOne({ _id: id })
		} catch (error) {
			throw new ApolloError(error)
		}
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / shop / shop.resolver.ts View on Github external
async shop(@Args('id') id: string): Promise {
		try {
			return await this.shopRepository.findOne({ _id: id })
		} catch (error) {
			throw new ApolloError(error)
		}
	}
github chnirt / nestjs-graphql-best-practice / src / modules / user / user.service.ts View on Github external
try {
			const message = 'Not Found: User'
			const code = '404'
			const additionalProperties = {}

			const user = await this.userRepository.findOne({ _id })

			if (!user) {
				throw new ApolloError(message, code, additionalProperties)
			}

			user.isActive = false

			return (await this.userRepository.save(user)) ? true : false
		} catch (error) {
			throw new ApolloError(error, '500', {})
		}
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / user / user.resolver.ts View on Github external
async updateUser(
		@Args('_id') _id: string,
		@Args('input') input: UpdateUserInput
	): Promise {
		try {
			const { firstName, lastName, password, gender, sites } = input

			const user = await this.userRepository.findOne({ _id })

			if (!user) {
				throw new ApolloError('Not Found: User', '404', {})
			}

			sites.map(async item => {
				const { siteId, permissions } = item

				const existedUserPermission = await getMongoRepository(
					UserPermission
				).findOne({
					userId: user._id,
					siteId
				})

				if (existedUserPermission) {
					existedUserPermission.permissions = permissions

					return getMongoRepository(UserPermission).save(existedUserPermission)
github chnirt / nestjs-graphql-best-practice / src / modules / user / user.service.ts View on Github external
const activeAdditionalProperties = {}

		if (!user.isActive) {
			throw new ApolloError(
				activeMessage,
				activeCode,
				activeAdditionalProperties
			)
		}

		const lockedMessage = 'Locked'
		const lockedCode = '423'
		const lockedAdditionalProperties = {}

		if (user.isLocked) {
			throw new ApolloError(
				lockedMessage,
				lockedCode,
				lockedAdditionalProperties
			)
		}

		const token = jwt.sign(
			{
				issuer: 'http://lunchapp2.dev.io',
				subject: user._id,
				audience: user.username
			},
			process.env.SECRET_KEY,
			{
				expiresIn: '30d'
			}
github chnirt / nestjs-graphql-best-practice / src / modules / menu / menu.service.ts View on Github external
async getMenuPublishBySite(siteId: string): Promise<menu> {
		try {
			return await this.commonService.findOneAdapter(Menu, {
				siteId,
				isPublished: true,
				isActive: true
			})
		} catch (error) {
			throw new ApolloError(error)
		}
	}
</menu>