How to use the type-graphql.Subscription 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 forsigner / egg-type-graphql / example / app / recipe / resolver / sample.resolver.ts View on Github external
}

  // dynamic topic

  @Mutation(() => Boolean)
  async pubSubMutationToDynamicTopic(
    @PubSub() pubSub: PubSubEngine,
    @Arg('topic') topic: string,
    @Arg('message', { nullable: true }) message?: string,
  ): Promise {
    const payload: NotificationPayload = { id: ++this.autoIncrement, message };
    await pubSub.publish(topic, payload);
    return true;
  }

  @Subscription({
    topics: ({ args }) => args.topic,
  })
  subscriptionWithFilterToDynamicTopic(
    @Arg('topic') topic: string,
    @Root() { id, message }: NotificationPayload,
  ): Notification {
    console.log('topic:', topic);
    return { id, message, date: new Date() };
  }
}
github niklaskorz / nkchat / server / src / resolvers / RoomResolver.ts View on Github external
}

  @Subscription(returns => Room, {
    description:
      'Notifies when the specified room has been updated and returns the updated room',
    topics: SubscriptionType.RoomWasUpdated,
    filter: ({ payload, args }) => payload.room.id.equals(args.roomId),
  })
  roomWasUpdated(
    @Root() payload: RoomWasUpdatedPayload,
    @Arg('roomId', type => ObjectID) roomId: ObjectID,
  ): Room {
    return payload.room;
  }

  @Subscription(returns => User, {
    description:
      'Notifies when a user has joined the specified room and returns the joined user',
    topics: SubscriptionType.UserJoinedRoom,
    filter: ({ payload, args }) => payload.roomId.equals(args.roomId),
  })
  userJoinedRoom(
    @Root() payload: UserJoinedRoomPayload,
    @Arg('roomId', type => ObjectID) roomId: ObjectID,
  ): User {
    return payload.user;
  }
}
github jorisvddonk / WelcomeToTheFuture / backend / universe / Star.resolver.ts View on Github external
return 5 * v.childComplexity;
    }
  })
  nearbyStars(@Root() star: Star, @Arg("maxRange", { nullable: true, description: "Range to search for other stars" }) maxRange: number): Star[] {
    const items = Universe.getStars().filter((x: Star) => {
      return x.name !== star.name
    }).filter((x: Star) => {
      if (maxRange !== undefined) {
        return getRangeBetweenStars(star, x) < maxRange;
      }
      return true;
    });
    return items;
  }

  @Subscription({
    topics: ["currentStar"]
  })
  currentStar(@Root() payload: Star): Star {
    return payload;
  }
}
github rtang03 / fabric-rx-cqrs / src / resolver / resolvers.ts View on Github external
topics: 'ENTITY',
      filter: ({ payload, args }) =>
        args.entityName === entName &&
        isEventArrived(args.event, payload.events) &&
        args.id === payload.id
    })
    toEntity(
      @Root() entity: Entity,
      @Arg('entityName') entityName: string,
      @Arg('event', () => [String]) event: string[],
      @Arg('id') id: string
    ): Entity {
      return entity;
    }

    @Subscription(() => Entity, {
      name: `to${capitalize(entName)}s`,
      topics: 'ENTITY',
      filter: ({ payload, args }) =>
        args.entityName === entName &&
        isEventArrived(args.event, payload.events)
    })
    toEntities(
      @Root() entity: Entity,
      @Arg('entityName') entityName: string,
      @Arg('event', () => [String]) event: string[]
    ) {
      return entity;
    }
  }
  return AbstractResolver;
};
github jorisvddonk / WelcomeToTheFuture / backend / starship / Starship.resolver.ts View on Github external
import {
  Resolver,
  Root,
  Subscription,
  FieldResolver
} from "type-graphql";
import { Starship } from "./Starship";
import { Achievements } from "../Achievements/AchievementsDAO";

@Resolver(Starship)
export class StarshipResolver {
  constructor() { }

  @Subscription({
    topics: ["starshipUpdate"]
  })
  starshipUpdate(
    @Root() payload: Starship
  ): Starship {
    return payload;
  }

  @FieldResolver()
  name(
    @Root() starship: Starship): string {
    Achievements.unlock('get_name');
    return starship.name;
  }

}
github mgm-interns / team-radio / server / src / resolvers / station / RealTimeStationResolvers.ts View on Github external
public all(): RealTimeStationWithOnlineCount[] {
    return this.manager.orderedStations.map(RealTimeStationWithOnlineCount.fromRealTimeStation);
  }

  @Subscription(returns => RealTimeStationWithOnlineCount, {
    name: 'onStationsChanged',
    topics: [StationTopic.JOIN_STATION, StationTopic.LEAVE_STATION, StationTopic.UPDATE_PLAYER_SONG],
    description: 'Subscribe for online users changes between stations.'
  })
  public subscribeStations(
    @Root() payload: StationTopic.JoinStationPayLoad | StationTopic.LeaveStationPayLoad
  ): RealTimeStationWithOnlineCount {
    return RealTimeStationWithOnlineCount.fromRealTimeStation(this.manager.findStation(payload.stationId));
  }

  @Subscription(returns => RealTimeStation, {
    name: 'onStationChanged',
    topics: [StationTopic.JOIN_STATION, StationTopic.LEAVE_STATION],
    description: 'Subscribe for station changed, whether user join or leave a station.',
    filter: ({ args, payload }) => payload.stationId === args.stationId
  })
  public subscribeStation(@Arg('stationId') stationId: string): RealTimeStation {
    return RealTimeStation.fromRealTimeStationManager(this.manager.findStation(stationId));
  }

  @Mutation({ description: 'Join specific station, this action will leave all other stations before joining another.' })
  public joinStation(
    @PubSub(StationTopic.JOIN_STATION) publishJoinStation: Publisher,
    @PubSub(StationTopic.LEAVE_STATION) publishLeaveStation: Publisher,
    @Arg('stationId') stationId: string,
    @Ctx() context: IAuthenticatedContext | IAnonymousContext
  ): boolean {
github mgm-interns / team-radio / server / src / resolvers / station / RealTimeStationPlayerResolvers.ts View on Github external
@Resolver(of => RealTimeStationPlayer)
export class RealTimeStationPlayerResolver extends BaseResolver {
  @Inject()
  private manager: RealTimeStationsManager;

  @Query(returns => RealTimeStationPlayer, {
    name: 'StationPlayer',
    description:
      'Query station current player state, ' +
      'combine with "onStationPlayerChanged" for fetching initial data then listening for changes.'
  })
  public getPlayer(@Arg('stationId') stationId: string): RealTimeStationPlayer {
    return RealTimeStationPlayer.fromRealTimeStationPlayerManager(this.manager.findStation(stationId).player);
  }

  @Subscription(returns => RealTimeStationPlayer, {
    name: 'onStationPlayerChanged',
    topics: [StationTopic.UPDATE_PLAYER_SONG],
    filter: ({ args, payload }) => payload.stationId === args.stationId,
    description: 'Subscribe on update player song player event of station player manager'
  })
  public subscribeStationPlayer(@Arg('stationId') stationId: string): RealTimeStationPlayer {
    return RealTimeStationPlayer.fromRealTimeStationPlayerManager(this.manager.findStation(stationId).player);
  }
}
github rtang03 / fabric-rx-cqrs / src / resolver / resolvers.ts View on Github external
export const getSubscriptionResolver: (entName: string) => any = (
  entName: string
) => {
  @Resolver()
  class AbstractResolver {
    @Subscription(() => Entity, {
      name: `to${capitalize(entName)}`,
      topics: 'ENTITY',
      filter: ({ payload, args }) =>
        args.entityName === entName &&
        isEventArrived(args.event, payload.events) &&
        args.id === payload.id
    })
    toEntity(
      @Root() entity: Entity,
      @Arg('entityName') entityName: string,
      @Arg('event', () => [String]) event: string[],
      @Arg('id') id: string
    ): Entity {
      return entity;
    }
github Raathigesh / majestic / server / api / workspace / resolver.ts View on Github external
}

  @Query(returns => TestFile)
  async file(@Arg("path") path: string) {
    const file = new TestFile();
    file.items = await inspect(path);
    return file;
  }

  @Query(returns => TestFileResult, { nullable: true })
  result(@Arg("path") path: string) {
    const result = this.results.getResult(path);
    return result ? result : null;
  }

  @Subscription(returns => TestFile, {
    topics: [WatcherEvents.FILE_CHANGE]
  })
  async fileChange(@Root() event: FileChangeEvent, @Arg("path") path: string) {
    const file = new TestFile();
    file.items = await inspect(event.payload.path);
    return file;
  }

  @Subscription(returns => TestFileResult, {
    topics: [
      Events.TEST_START,
      Events.TEST_RESULT,
      RunnerEvents.RUNNER_STOPPED
    ],
    filter: ({ payload: { payload }, args }) => {
      return payload.path === args.path;
github bearbytes / apollo-hooks-codegen / examples / packages / server / src / TodoItemResolver.ts View on Github external
) {
    const todoItem: TodoItem = {
      id: (this.todos.length + 1).toString(),
      title: input.title,
      description: input.description,
      dueDate: input.dueDate,
      isDone: false,
    }

    this.todos.push(todoItem)
    pubSub.publish('NEW_TODO_ITEM', todoItem)

    return todoItem
  }

  @Subscription({ topics: 'NEW_TODO_ITEM' })
  subscribeTodoItems(@Root() todoItem: TodoItem): TodoItem {
    return todoItem
  }

  todos: TodoItem[] = [
    { id: '1', title: 'Learn GraphQL', isDone: true },
    {
      id: '2',
      title: 'Use apollo-hooks-codegen',
      isDone: false,
      dueDate: new Date(),
    },
  ]
}