@nestjs/graphql#Parent TypeScript Examples

The following examples show how to use @nestjs/graphql#Parent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: form.field.resolver.ts    From api with GNU Affero General Public License v3.0 7 votes vote down vote up
@ResolveField(() => [FormFieldOptionModel])
  async options(
    @Parent() parent: FormFieldModel,
    @Context('cache') cache: ContextCache,
  ): Promise<FormFieldOptionModel[]> {
    const field = await cache.get<FormFieldEntity>(cache.getCacheKey(
      FormFieldEntity.name,
      parent._id
    ))

    if (!field.options) {
      return []
    }

    return field.options.map(option => new FormFieldOptionModel(
      this.idService.encode(option.id),
      option,
    ))
  }
Example #2
Source File: block.resolver.ts    From amplication with Apache License 2.0 6 votes vote down vote up
@ResolveField(() => [User])
  async lockedByUser(@Parent() block: Block): Promise<User> {
    if (block.lockedByUserId) {
      return this.userService.findUser({
        where: {
          id: block.lockedByUserId
        }
      });
    } else {
      return null;
    }
  }
Example #3
Source File: form.field.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => [FormFieldLogicModel])
  async logic(
    @Parent() parent: FormFieldModel,
    @Context('cache') cache: ContextCache,
  ): Promise<FormFieldLogicModel[]> {
    const field = await cache.get<FormFieldEntity>(cache.getCacheKey(
      FormFieldEntity.name,
      parent._id
    ))

    if (!field.logic) {
      return []
    }

    return field.logic.map(logic => new FormFieldLogicModel(
      this.idService.encode(logic.id),
      logic,
    ))
  }
Example #4
Source File: block.resolver.ts    From amplication with Apache License 2.0 6 votes vote down vote up
@ResolveField(() => [BlockVersion])
  async versions(
    @Parent() entity: Block,
    @Args() args: FindManyBlockVersionArgs
  ): Promise<BlockVersion[]> {
    return this.blockService.getVersions({
      ...args,
      where: {
        ...args.where,
        block: { id: entity.id }
      }
    });
  }
Example #5
Source File: form.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => Boolean)
  @Roles('admin')
  async isLive(
    @User() user: UserEntity,
    @Parent() parent: FormModel,
    @Context('cache') cache: ContextCache,
  ): Promise<boolean> {
    const form = await cache.get<FormEntity>(cache.getCacheKey(FormEntity.name, parent._id))

    if (!this.formService.isAdmin(form, user)) {
      throw new Error('no access to field')
    }

    return form.isLive
  }
Example #6
Source File: user.resolver.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
@ResolveLoader(() => String, {
    nullable: true,
    complexity: (options) => {
      return 5;
    },
    middleware: [
      async (ctx, next) => {
        const results = await next();
        return results.map((res) => res || 'Missing');
      },
    ],
    opts: {
      cache: false,
    },
  })
  async fullName(
    @Args({ type: () => FullNameArgs }) args: FullNameArgs[],
    @Parent() p: LoaderQuery<UserType>[],
    @Context() ctx: Record<string, any>,
    @Header('authorization') auth?: string,
  ) {
    return p.map(({ obj }) => {
      if (obj.name && obj.lastName) {
        return `${obj.name} ${obj.lastName}`;
      }
      return obj.name || obj.lastName;
    });
  }
Example #7
Source File: submission.field.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => FormFieldModel, { nullable: true })
  async field(
    @Parent() parent: SubmissionFieldModel,
    @Context('cache') cache: ContextCache,
  ): Promise<FormFieldModel> {
    const submissionField = await cache.get<SubmissionFieldEntity>(
      cache.getCacheKey(SubmissionFieldEntity.name, parent._id)
    )

    const field = await cache.get<FormFieldEntity>(
      cache.getCacheKey(
        FormFieldEntity.name,
        submissionField.fieldId),
      () => this.formFieldService.findById(submissionField.fieldId, submissionField.field)
    )

    if (!field) {
      return null
    }

    return new FormFieldModel(
      this.idService.encode(field.id),
      field,
    )
  }
Example #8
Source File: app.resolver.ts    From amplication with Apache License 2.0 6 votes vote down vote up
@ResolveField(() => [Entity])
  async entities(
    @Parent() app: App,
    @Args() args: FindManyEntityArgs
  ): Promise<Entity[]> {
    return this.entityService.entities({
      ...args,
      where: { ...args.where, app: { id: app.id } }
    });
  }
Example #9
Source File: form.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => DesignModel)
  async design(
    @User() user: UserEntity,
    @Parent() parent: FormModel,
    @Context('cache') cache: ContextCache,
  ): Promise<DesignModel> {
    const form = await cache.get<FormEntity>(cache.getCacheKey(FormEntity.name, parent._id))

    return new DesignModel(form.design)
  }
Example #10
Source File: entity.resolver.ts    From amplication with Apache License 2.0 5 votes vote down vote up
@ResolveField(() => [EntityPermission])
  async permissions(@Parent() entity: Entity) {
    //the fields property on the Entity always returns the fields of the current version (versionNumber=0)
    return this.entityService.getPermissions(entity.id);
  }
Example #11
Source File: project.resolver.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
@ResolveField()
    dbmss(
        @Context('environment') environment: Environment,
        @Parent() project: Project,
        @Args() {filters}: FilterArgs,
    ): Promise<List<ProjectDbms>> {
        return environment.projects.listDbmss(project.name, filters);
    }
Example #12
Source File: workspace.resolver.ts    From amplication with Apache License 2.0 5 votes vote down vote up
@ResolveField(() => [GitOrganization])
  async gitOrganizations(
    @Parent() workspace: Workspace
  ): Promise<GitOrganization[]> {
    return this.workspaceService.findManyGitOrganizations(workspace.id);
  }
Example #13
Source File: global-id-field.resolver.ts    From nestjs-relay with MIT License 5 votes vote down vote up
export function GlobalIdFieldResolver<T>(
  classRef: Type<T>,
  idFieldOptions?: GlobalIdFieldOptions,
): Type<GlobalIdFieldResolver> {
  const globalIdFieldOptions = idFieldOptions || {};

  @Resolver(classRef, { isAbstract: true })
  abstract class GlobalIdFieldResolverHost {
    @GlobalIdField(globalIdFieldOptions)
    id(@Parent() parent: ResolverParent, @Info() info: ResolverInfo): ResolvedGlobalId {
      if (!parent || !parent.id) {
        throw new Error(`Cannot resolve id when 'parent' or 'parent.id' is null`);
      }
      switch (typeof parent.id) {
        case 'object':
          return parent.id;
        case 'string':
          return new ResolvedGlobalId({
            type: info.parentType.name,
            id: parent.id,
          });
        case 'number':
          return new ResolvedGlobalId({
            type: info.parentType.name,
            id: parent.id.toString(),
          });
      }
    }
  }
  return GlobalIdFieldResolverHost as Type<GlobalIdFieldResolver>;
}
Example #14
Source File: entityVersion.resolver.ts    From amplication with Apache License 2.0 5 votes vote down vote up
@ResolveField(() => [EntityField])
  async permissions(@Parent() entityVersion: EntityVersion) {
    const { entityId, versionNumber } = entityVersion;

    return this.entityService.getVersionPermissions(entityId, versionNumber);
  }
Example #15
Source File: users.resolver.ts    From knests with MIT License 5 votes vote down vote up
@ResolveField()
  async roles(@Parent() user) {
    return user.roles || [];
  }
Example #16
Source File: block.resolver.ts    From amplication with Apache License 2.0 5 votes vote down vote up
//resolve the parentBlock property as a generic block
  @ResolveField(() => Block, { nullable: true })
  async parentBlock(@Parent() block: Block): Promise<Block> {
    return this.blockService.getParentBlock(block);
  }
Example #17
Source File: animal.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@ResolveLoader(() => String)
  aField(@Parent() queries: LoaderQuery<Animal>[]) {
    return queries.map(({ obj }) => 'lorem');
  }
Example #18
Source File: action.resolver.ts    From amplication with Apache License 2.0 5 votes vote down vote up
@ResolveField(() => [ActionStep])
  async steps(@Parent() action: Action) {
    return this.service.getSteps(action.id);
  }
Example #19
Source File: animal.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@ResolveField(() => Boolean)
  hasPaws(@Parent() animal: Animal) {
    return true;
  }