How to use the apollo-server-core.UserInputError 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 chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async search(@Args('conditions') conditions: SearchInput): Promise {
		let result

		const { select, where, order, skip, take } = conditions

		if (Object.keys(where).length > 1) {
			throw new UserInputError('Your where must be 1 collection.')
		}

		const type = Object.keys(where)[0]

		// const createdAt = { $gte: 0, $lte: new Date().getTime() }

		result = await getMongoRepository(type).find({
			where: where[type] && JSON.parse(JSON.stringify(where[type])),
			order: order && JSON.parse(JSON.stringify(order)),
			skip,
			take,
		})

		// console.log(result)

		if (result.length === 0) {
github birkir / prime / packages / prime-core / src / modules / external / resolvers / createDocumentUpdateResolver.ts View on Github external
order: {
        createdAt: 'DESC',
      },
    });

    if (!doc && key === 'documentId') {
      doc = await documentRepository.findOne({
        where: { documentId: args.id, deletedAt: IsNull() },
        order: {
          createdAt: 'DESC',
        },
      });
    }

    if (!doc) {
      throw new UserInputError('Document not found');
    }

    const data = await payload.documentTransformer.transformInput(
      args.input,
      payload.schema,
      payload.fields
    );

    if (args.merge) {
      defaultsDeep(
        data,
        await payload.documentTransformer.transformInput(doc.data, payload.schema, payload.fields)
      );
    }

    // @todo userId
github chnirt / nestjs-graphql-best-practice / src / common / pipes / validation.pipe.ts View on Github external
async transform(value: any, { metatype }: ArgumentMetadata) {
		// destructuring metadata
		if (!metatype || !this.toValidate(metatype)) {
			return value
		}
		const object = plainToClass(metatype, value)
		const errors = await validate(object)
		if (errors.length > 0) {
			throw new UserInputError(
				`Form Arguments invalid: ${this.formatErrors(errors)}`
			)
		}
		return value
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async searchUser(@Args('userIds') userIds: string[]): Promise {
		let result

		if (userIds.length === 0) {
			throw new UserInputError('userIds can not be blank.')
		}

		result = await getMongoRepository(User).find({
			where: {
				_id: { $in: userIds },
			},
		})

		// tslint:disable-next-line:prefer-conditional-expression
		if (result.length > 1) {
			result = { users: result }
		} else {
			result = result[0]
		}

		return result
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async searchUser(@Args('userIds') userIds: string[]): Promise {
		let result

		if (userIds.length === 0) {
			throw new UserInputError('userIds can not be blank.')
		}

		result = await getMongoRepository(User).find({
			where: {
				_id: { $in: userIds }
			}
		})

		// tslint:disable-next-line:prefer-conditional-expression
		if (result.length > 1) {
			result = { users: result }
		} else {
			result = result[0]
		}

		return result
github apollographql / apollo-server / packages / apollo-server-integration-testsuite / src / ApolloServer.ts View on Github external
const formatError = jest.fn(error => {
          expect(error instanceof Error).toBe(true);
          expect(error.extensions.code).toEqual('INTERNAL_SERVER_ERROR');
          expect(error.extensions.exception.name).toEqual('ValidationError');
          expect(error.extensions.exception.message).toBeDefined();
          const inputError = new UserInputError('User Input Error');
          return {
            message: inputError.message,
            extensions: inputError.extensions,
          };
        });
github birkir / prime / packages / prime-core / src / routes / external / findAll.ts View on Github external
) AS "table"
          WHERE "table"."entryId" = ${sequelize.escape(cursor)}
          AND "table"."deletedAt" IS NULL
        `;
        const result = await sequelize.query(sql);
        if (result[0] && result[0][0]) {
          const index = Number(result[0][0].index);
          debug('cursor index: %o', index);
          if (args.after) {
            findAllPaging.offset = index;
          } else {
            findAllPaging.offset = Math.max(0, index - (args.first || defaultLimit));
          }
        } else {
          debug(result);
          throw new UserInputError('Cursor out of date');
        }
      }

      if (args.first) {
        if (args.skip) {
          findAllPaging.offset = args.skip;
        }
        findAllPaging.limit = args.first;
      }

      findAllPaging.offset = findAllPaging.offset || 0;
      findAllPaging.limit = findAllPaging.limit || defaultLimit;

      const entries = await ContentEntry.findAll({
        ...findAllOptions,
        ...findAllPaging,
github origen-chat / api / projects / api / src / server / graphql / Query / user / resolver.ts View on Github external
> = async (root, args, context) => {
  const { id: userId, username, usernameIdentifier, email } = args;

  let user: core.types.Nullable = null;

  if (userId) {
    user = await context.loaders.userById.load(userId);
  } else if (username && usernameIdentifier) {
    user = await context.loaders.userByUniqueUsername.load({
      username,
      usernameIdentifier,
    });
  } else if (email) {
    user = await context.loaders.userByEmail.load(email);
  } else {
    throw new UserInputError(
      'must provide user id, username and username identifier, or email',
    );
  }

  if (!user) {
    throw new NotFoundError({ entity: NotFoundableEntity.User });
  }

  return user;
};
github EricKit / nest-user-auth / src / users / users.resolvers.ts View on Github external
async createUser(
    @Args('createUserInput') createUserInput: CreateUserInput,
  ): Promise {
    let createdUser: User | undefined;
    try {
      createdUser = await this.usersService.create(createUserInput);
    } catch (error) {
      throw new UserInputError(error.message);
    }
    return createdUser;
  }
github EricKit / nest-user-auth / src / users / users.resolvers.ts View on Github external
async resetPassword(
    @Args('username') username: string,
    @Args('code') code: string,
    @Args('password') password: string,
  ): Promise {
    const user = await this.usersService.resetPassword(
      username,
      code,
      password,
    );
    if (!user) throw new UserInputError('The password was not reset');
    return user;
  }