How to use the apollo-server-express.UserInputError function in apollo-server-express

To help you get started, we’ve selected a few apollo-server-express 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 juicycleff / ultimate-backend / apps / service-auth / src / auth / auth.resolver.ts View on Github external
// Graphql input validation checks start
    const validationErrors = {};
    if (!service) {
      validationErrors[service] = 'MISSING_VALUE';
      throw new UserInputError('Missing user login service type', { validationErrors });
    }

    if (serviceType === ServiceTypes.Password) {
      if (!params.password || !params.email) {
        validationErrors[params.password || params.email] = 'MISSING_VALUE';
        throw new UserInputError('Missing user login password or email', { validationErrors });
      }
    } else {
      if (!params.accessToken || !params.accessTokenSecret) {
        validationErrors[params.accessToken || params.accessTokenSecret] = 'MISSING_VALUE';
        throw new UserInputError(`Missing user access token or secret for ${service.toString().toLowerCase()} strategy`, { validationErrors });
      }
    }

    if (service === ServiceTypes.Password) {

      // Authenticate against passport local strategy
      const auth = await context.authenticate('graphql-local', { email: params.email, password: params.password });

      auth.user.whoImI = new IdentifyMachineUtils(context.req).sender();
      context.login(auth.user);

      return {
        id: auth.user.id,
        sessionId: context.req.sessionID,
      };
    }
github juicycleff / ultimate-backend / apps / service-project / src / project / project.resolver.ts View on Github external
async projectMutations(@Args() input: ProjectMutations, @Context() ctx: any, @CurrentUser() user: UserEntity): Promise {
    const { create, delete: remove, update } = input;
    if (create === null && remove === null && update === null) { // Check to make sure input is not null
      throw new UserInputError('Mutation inputs missing'); // Throw an apollo input error
    }

    if (create) {
      // return await this.commandBus.execute(new CreateProjectCommand(create));
      return await this.projectService.create(create);
    } else if (update) {
      // return await this.commandBus.execute(new UpdateProjectCommand(update));
      return await this.projectService.update(update);
    } else if (remove) {
      // return await this.commandBus.execute(new DeleteProjectCommand(remove));
      return await this.projectService.delete(remove);
    } else {
      throw new UserInputError('Mutation inputs missing'); // Throw an apollo input error
    }
  }
github juicycleff / ultimate-backend / apps / service-payment / src / cqrs / query / handlers / plan / get-plan.handler.ts View on Github external
async execute(query: GetPlanQuery): Promise {
    this.logger.log(`Async ${query.constructor.name}...`);
    const { id } = query;

    if (!id) { throw new UserInputError('Missing plan id input'); }

    try {
      // Check cache to see if data exist
      const cacheData = await this.cacheStore.get('service-payment/plan/' + id);
      if (cacheData !== undefined && typeof cacheData !== 'undefined') {
        return cacheData;
      }

      const product = await this.stripeClient.products.retrieve(id);
      const plans = await this.stripeClient.plans.list({
        product: product.id,
      });
      const result = convertToPlan(plans, product);

      await this.cacheStore.set('service-payment/plan/' + id, result, {ttl: 50});
      return result;
github sainthkh / reasonql / snippets / auth / server / src / resolvers.js View on Github external
throw new UserInputError("Email not found", {});
      }

      // Check password
      // It is not recommended in production. Use libraries like bscript. 
      if (password == user.password) {
        // User matched
        // Create JWT Payload
        const payload = {
          id: user.id,
          name: user.name,
        };

        return await sign(payload);
      } else {
        throw new UserInputError("Password incorrect", {});
      }
    },
  }
github juicycleff / ultimate-backend / apps / service-auth / src / auth / auth.resolver.ts View on Github external
async login(
    @Args('input') { service, params }: LoginInput,
    @Context() context: any): Promise {

    const serviceType =  ServiceTypes[service];

    // Graphql input validation checks start
    const validationErrors = {};
    if (!service) {
      validationErrors[service] = 'MISSING_VALUE';
      throw new UserInputError('Missing user login service type', { validationErrors });
    }

    if (serviceType === ServiceTypes.Password) {
      if (!params.password || !params.email) {
        validationErrors[params.password || params.email] = 'MISSING_VALUE';
        throw new UserInputError('Missing user login password or email', { validationErrors });
      }
    } else {
      if (!params.accessToken || !params.accessTokenSecret) {
        validationErrors[params.accessToken || params.accessTokenSecret] = 'MISSING_VALUE';
        throw new UserInputError(`Missing user access token or secret for ${service.toString().toLowerCase()} strategy`, { validationErrors });
      }
    }

    if (service === ServiceTypes.Password) {
github vasansr / pro-mern-stack-2 / server / server.js View on Github external
function issueValidate(issue) {
  const errors = [];
  if (issue.title.length < 3) {
    errors.push('Field "title" must be at least 3 characters long.');
  }
  if (issue.status === 'Assigned' && !issue.owner) {
    errors.push('Field "owner" is required when status is "Assigned"');
  }
  if (errors.length > 0) {
    throw new UserInputError('Invalid input(s)', { errors });
  }
}
github vasansr / pro-mern-stack-2 / api / issue.js View on Github external
function validate(issue) {
  const errors = [];
  if (issue.title.length < 3) {
    errors.push('Field "title" must be at least 3 characters long.');
  }
  if (issue.status === 'Assigned' && !issue.owner) {
    errors.push('Field "owner" is required when status is "Assigned"');
  }
  if (errors.length > 0) {
    throw new UserInputError('Invalid input(s)', { errors });
  }
}
github rafaelalmeidatk / twitter-fullstack-clone / packages / server / src / graphql / schema / user / resolvers.js View on Github external
const getUser = baseResolver.createResolver(async (root, { input }) => {
  const { id, username } = input;

  if ((!id && !username) || (id && username)) {
    throw new UserInputError(
      'You need to provide the ID or the username, but not both'
    );
  }

  if (id) return await findUserById(id);
  if (username) return await findUserByUsername(username);
  return null;
});