How to use the type-graphql.FieldResolver function in type-graphql

To help you get started, weโ€™ve selected a few type-graphql 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 ReactFinland / graphql-api / server / schema / resolvers / ContactResolver.ts View on Github external
return contact.firstName || contact.name.split(" ")[0];
  }

  @FieldResolver(_ => String)
  public lastName(@Root() contact: Contact) {
    return (
      contact.lastName ||
      /* TODO: This approximation isn't accurate always */
      contact.name
        .split(" ")
        .slice(1)
        .join(" ")
    );
  }

  @FieldResolver(_ => Image)
  public image(@Root() contact: Contact, @Ctx() ctx: IContext) {
    if (!contact.image) {
      return {
        url: "",
      };
    }

    if (contact.image.url.startsWith("http")) {
      return contact.image;
    }

    // FIXME: Figure out why ctx can be missing
    return {
      url: `${ctx ? ctx.mediaUrl : "/media"}/${contact.image.url}`,
    };
  }
github birkir / prime / packages / prime-core / src / modules / internal / resolvers / DocumentResolver.ts View on Github external
publishedAt: null as any,
      }
    );
    // @todo update algolia
    const document = await this.Document(id);
    processWebhooks('document.unpublished', { document });
    return document;
  }

  @FieldResolver(returns => GraphQLJSON, { nullable: true })
  public async data(@Root() document: Document): Promise {
    const schema = await this.schemaRepository.loadOne(document.schemaId);
    return this.documentTransformer.transformOutput(document, schema);
  }

  @FieldResolver(returns => [DocumentVersion], { nullable: true })
  public async versions(@Root() document: Document): Promise {
    return this.documentRepository.find({
      where: { documentId: document.documentId, deletedAt: IsNull() },
    });
  }

  @FieldResolver(returns => Document, {
    nullable: true,
    description: 'Get published version of the document (if any)',
  })
  public async published(@Root() document: Document) {
    return this.documentRepository.loadOneByDocumentId(document.documentId, 'documentId', {
      publishedAt: Raw(alias => `${alias} IS NOT NULL`),
    });
  }
github niklaskorz / nkchat / server / src / resolvers / MessageResolver.ts View on Github external
@Resolver(of => Message)
export class MessageResolver {
  constructor(
    @InjectRepository(Message)
    private messageRepository: MongoRepository,
    @InjectRepository(Room) private roomRepository: MongoRepository,
    @InjectRepository(User) private userRepository: MongoRepository,
  ) {}

  @FieldResolver(type => Room)
  // @ManyToOne(type => Room, room => room.messages)
  async room(@Root() message: Message): Promise {
    return await this.roomRepository.findOneOrFail(message.roomId);
  }

  @FieldResolver(type => User)
  // @ManyToOne(type => User, user => user.messages)
  async author(@Root() message: Message): Promise {
    return await this.userRepository.findOneOrFail(message.authorId);
  }

  @FieldResolver()
  viewerIsAuthor(@Root() message: Message, @Ctx() ctx: Context): boolean {
    const viewer = ctx.state.viewer;
    if (!viewer) {
      return false;
    }

    return message.authorId.equals(viewer.id);
  }

  @Mutation(returns => Message, {
github goldcaddy77 / warthog / examples / 07-feature-flags / src / project / project.resolver.ts View on Github external
import { Project } from './project.model';
import { ProjectService } from './project.service';

@Resolver(Project)
export class ProjectResolver {
  constructor(@Inject('ProjectService') readonly service: ProjectService) {
    // no-empty
  }

  @FieldResolver(() => [Environment])
  environments(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.environments.load(project);
  }

  @FieldResolver(() => [Segment])
  segments(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.segments.load(project);
  }

  @FieldResolver(() => [FeatureFlag])
  featureFlags(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.featureFlags.load(project);
  }

  @FieldResolver(() => [FeatureFlagUser])
  featureFlagUsers(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.featureFlagUsers.load(project);
  }

  @FieldResolver(() => [FeatureFlagSegment])
  featureFlagSegments(
github forsigner / egg-type-graphql / example / app / graphql / recipe / resolver.ts View on Github external
return await this.items;
  }

  @Mutation(returns => Recipe)
  async addRecipe(@Arg('recipe') recipeInput: RecipeInput): Promise {
    const recipe = plainToClass(Recipe, {
      description: recipeInput.description,
      title: recipeInput.title,
      ratings: [],
      creationDate: new Date(),
    });
    await this.items.push(recipe);
    return recipe;
  }

  @FieldResolver()
  ratingsCount(
    @Root() recipe: Recipe,
    @Arg('minRate', type => Int, { defaultValue: 0.0 }) minRate: number,
  ): number {
    return recipe.ratings.filter(rating => rating >= minRate).length;
  }
}
github goldcaddy77 / warthog / examples / 07-feature-flags / src / feature-flag / feature-flag.resolver.ts View on Github external
export class FeatureFlagsForUserInput {
  @Field(() => String)
  projKey: string;

  @Field(() => String)
  envKey: string;

  @Field(() => String)
  userKey: string;
}

@Resolver(FeatureFlag)
export class FeatureFlagResolver {
  constructor(@Inject('FeatureFlagService') readonly service: FeatureFlagService) {}

  @FieldResolver(() => Project)
  project(@Root() featureFlag: FeatureFlag, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.FeatureFlag.project.load(featureFlag);
  }

  @FieldResolver(() => [FeatureFlagSegment])
  featureFlagSegments(
    @Root() featureFlag: FeatureFlag,
    @Ctx() ctx: BaseContext
  ): Promise {
    return ctx.dataLoader.loaders.FeatureFlag.featureFlagSegments.load(featureFlag);
  }

  @FieldResolver(() => [FeatureFlagUser])
  featureFlagUsers(
    @Root() featureFlag: FeatureFlag,
    @Ctx() ctx: BaseContext
github ReactFinland / graphql-api / server / schema / resolvers / ConferenceResolver.ts View on Github external
"Use `keynotes`,s `fullTalks` and `lightningTalks` instead",
  })
  public talks(@Root() conference: Conference) {
    return resolveSessions(conference.schedules, [
      SessionType.LIGHTNING_TALK,
      SessionType.TALK,
      SessionType.KEYNOTE,
    ]);
  }

  @FieldResolver(_ => [Session])
  public keynotes(@Root() conference: Conference) {
    return resolveSessions(conference.schedules, [SessionType.KEYNOTE]);
  }

  @FieldResolver(_ => [Session])
  public keynoteSpeakers(@Root() conference: Conference) {
    const talks = resolveSessions(conference.schedules, [SessionType.KEYNOTE]);

    return getSessionSpeakers(conference, talks);
  }

  @FieldResolver(_ => [Session])
  public fullTalks(@Root() conference: Conference) {
    return resolveSessions(conference.schedules, [SessionType.TALK]);
  }

  @FieldResolver(_ => [Session])
  public fullTalkSpeakers(@Root() conference: Conference) {
    const talks = resolveSessions(conference.schedules, [SessionType.TALK]);

    return getSessionSpeakers(conference, talks);
github goldcaddy77 / warthog / examples / 07-feature-flags / src / project / project.resolver.ts View on Github external
@FieldResolver(() => [Environment])
  environments(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.environments.load(project);
  }

  @FieldResolver(() => [Segment])
  segments(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.segments.load(project);
  }

  @FieldResolver(() => [FeatureFlag])
  featureFlags(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.featureFlags.load(project);
  }

  @FieldResolver(() => [FeatureFlagUser])
  featureFlagUsers(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.featureFlagUsers.load(project);
  }

  @FieldResolver(() => [FeatureFlagSegment])
  featureFlagSegments(
    @Root() project: Project,
    @Ctx() ctx: BaseContext
  ): Promise {
    return ctx.dataLoader.loaders.Project.featureFlagSegments.load(project);
  }

  @FieldResolver(() => [UserSegment])
  userSegments(@Root() project: Project, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Project.userSegments.load(project);
  }
github goldcaddy77 / warthog / examples / 07-feature-flags / src / environment / environment.resolver.ts View on Github external
featureFlagUsers(
    @Root() environment: Environment,
    @Ctx() ctx: BaseContext
  ): Promise {
    return ctx.dataLoader.loaders.Environment.featureFlagUsers.load(environment);
  }

  @FieldResolver(() => [FeatureFlagSegment])
  featureFlagSegments(
    @Root() environment: Environment,
    @Ctx() ctx: BaseContext
  ): Promise {
    return ctx.dataLoader.loaders.Environment.featureFlagSegments.load(environment);
  }

  @FieldResolver(() => [UserSegment])
  userSegments(@Root() environment: Environment, @Ctx() ctx: BaseContext): Promise {
    return ctx.dataLoader.loaders.Environment.userSegments.load(environment);
  }

  @Query(() => [Environment])
  async environments(@Args() { where, orderBy, limit, offset }: EnvironmentWhereArgs): Promise<
    Environment[]
  > {
    return this.service.find(where, orderBy, limit, offset);
  }

  @Query(() => Environment)
  async environment(@Arg('where') where: EnvironmentWhereUniqueInput): Promise {
    return this.service.findOne(where);
  }
github ReactFinland / graphql-api / server / schema / resolvers / ConferenceResolver.ts View on Github external
return;
  }

  @FieldResolver(_ => [Contact])
  public allSpeakers(@Root() conference: Conference) {
    const talks = resolveSessions(conference.schedules, [
      SessionType.TALK,
      SessionType.KEYNOTE,
      SessionType.LIGHTNING_TALK,
    ]);

    return getSessionSpeakers(conference, talks);
  }

  @FieldResolver(_ => [Contact])
  public speakers(@Root() conference: Conference) {
    const talks = resolveSessions(conference.schedules, [SessionType.TALK]);

    return getSessionSpeakers(conference, talks);
  }

  @FieldResolver(_ => [Contact])
  public async attendees(@Root() conference: Conference, @Ctx() ctx: IContext) {
    const speakers = getSessionSpeakers(
      conference,
      resolveSessions(conference.schedules, [
        SessionType.KEYNOTE,
        SessionType.LIGHTNING_TALK,
        SessionType.TALK,
        SessionType.WORKSHOP,
      ])