@nestjs/graphql#Subscription TypeScript Examples

The following examples show how to use @nestjs/graphql#Subscription. 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: user.resolver.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
@Subscription(() => UserType, {
    filter: (payload, variables: { id: string }) => {
      return payload.userAdded.id === variables.id;
    },
    resolve: (payload) => {
      return payload.userAdded;
    },
  })
  async specificUserAdded(
    @Args({ name: 'id', type: () => ID }) id: string,
    @Context() ctx: MercuriusContext,
  ) {
    return toAsyncIterator(ctx.pubsub.subscribe('USER_ADDED'));
  }
Example #2
Source File: whisp.resolver.ts    From whispr with MIT License 6 votes vote down vote up
/**
   * Subscriptions
   */

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  @Subscription(() => Whisp, {
    filter: (payload, variables) => filterPayload(variables.filter, payload.whispAdded),
  })
  whispAdded(
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    @Args('filter', { type: () => GraphQLJSONObject }) filter: Record<string, unknown>,
  ): AsyncIterator<IWhisp> {
    return this.pubSub.asyncIterator('whispAdded');
  }
Example #3
Source File: dbms.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Subscription(() => DbmsEvent, {
        resolve: async (payload: any, _variables: any, context: any) => {
            return {
                [payload.eventType]:
                    payload.eventType !== DBMS_EVENT_TYPE.UNINSTALLED
                        ? await context.environment.dbmss.info([payload.dbmsId])
                        : [payload.dbmsId],
            };
        },
    })
    async [PUBLIC_GRAPHQL_METHODS.WATCH_DBMSS](
        @Context('environment') environment: Environment,
    ): Promise<AsyncIterator<unknown, any, undefined>> {
        if (!environmentWatchers.get(environment.id)) {
            const watchPaths = [];
            // watch dbmss pid file glob
            watchPaths.push(path.join(environment.dirPaths.dbmssData, DBMSS_PID_FILE_GLOB));
            // watch dbmss directory
            watchPaths.push(path.join(environment.dirPaths.dbmssData));
            const watcher = setDbmsWatcher(watchPaths);

            // keep a record of environment watcher
            environmentWatchers.set(environment.id, watcher);
        }

        return withCancel(pubSub.asyncIterator('infoDbmssUpdate'), async () => {
            const watcher = environmentWatchers.get(environment.id);
            // close and remove watcher
            if (watcher) {
                await watcher.close();
                environmentWatchers.delete(environment.id);
            }
        });
    }
Example #4
Source File: cat.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@Subscription(() => Cat, {
    resolve: (payload) => payload,
  })
  onCatSub(@Context('pubsub') pubSub: PubSub) {
    return toAsyncIterator(pubSub.subscribe('CAT_SUB_TOPIC'));
  }
Example #5
Source File: user.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@UseInterceptors(LogInterceptor)
  @Subscription(() => UserType)
  async userAdded(@Context() ctx: MercuriusContext) {
    return ctx.pubsub.subscribe('USER_ADDED');
  }
Example #6
Source File: user.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@Subscription(() => String, {
    resolve: (payload) => {
      return payload.userAdded.id;
    },
  })
  async userAddedId(@Context() ctx: MercuriusContext) {
    return ctx.pubsub.subscribe('USER_ADDED');
  }
Example #7
Source File: user.resolver.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
@Subscription(() => User, {
    resolve: (payload) => payload.user,
  })
  onCreateUser(@Context('pubsub') pubSub: PubSub) {
    return toAsyncIterator(pubSub.subscribe('createUser'));
  }
Example #8
Source File: chat.resolver.ts    From NextJS-NestJS-GraphQL-Starter with MIT License 5 votes vote down vote up
@Subscription(() => Message, {
    name: newMessage,
  })
  newMessage(): any {
    return pubsub.asyncIterator(newMessage);
  }