@nestjs/graphql#Context TypeScript Examples

The following examples show how to use @nestjs/graphql#Context. 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: dbms-plugins.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Mutation(() => UninstallDbmsPluginReturn, {nullable: true})
    async [PUBLIC_GRAPHQL_METHODS.UNINSTALL_DBMS_PLUGIN](
        @Context('environment') environment: Environment,
        @Args() {dbmsIds}: DbmssArgs,
        @Args() {pluginName}: PluginNameArgs,
    ): Promise<UninstallDbmsPluginReturn> {
        await environment.dbmsPlugins.uninstall(dbmsIds, pluginName);
        return {
            dbmsIds,
            pluginName,
        };
    }
Example #3
Source File: form.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => [FormNotificationModel])
  @Roles('admin')
  async notifications(
    @User() user: UserEntity,
    @Parent() parent: FormModel,
    @Context('cache') cache: ContextCache,
  ): Promise<FormNotificationModel[]> {
    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')
    }

    if (!form.notifications) {
      return []
    }

    return form.notifications.map(notification => new FormNotificationModel(
      this.idService.encode(notification.id),
      notification
    ))
  }
Example #4
Source File: extension.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Query(() => AppLaunchData)
    async [PUBLIC_GRAPHQL_METHODS.APP_LAUNCH_DATA](
        @Context('environment') environment: Environment,
        @Args() {appName, launchToken}: LaunchDataArgs,
    ): Promise<AppLaunchData> {
        const {dbmsId, projectId, ...rest} = await environment.extensions.parseAppLaunchToken(appName, launchToken);
        const dbms = await environment.dbmss.get(dbmsId);
        const appLaunchData: AppLaunchData = {
            ...rest,
            dbms,
            environmentId: environment.id,
        };

        if (projectId) {
            appLaunchData.project = {
                ...(await environment.projects.get(projectId)),
                files: [],
            };
        }

        return appLaunchData;
    }
Example #5
Source File: form.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => [FormFieldModel])
  async fields(
    @User() user: UserEntity,
    @Parent() parent: FormModel,
    @Context('cache') cache: ContextCache,
  ): Promise<FormFieldModel[]> {
    const form = await cache.get<FormEntity>(cache.getCacheKey(FormEntity.name, parent._id))

    if (!form.fields) {
      return []
    }

    return form.fields
      .sort((a,b) => a.idx - b.idx)
      .map(field => {
        cache.add(cache.getCacheKey(FormFieldEntity.name, field.id), field)

        return new FormFieldModel(
          this.idService.encode(field.id),
          field,
        )
      })
  }
Example #6
Source File: project.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Query(() => [Project])
    async [PUBLIC_GRAPHQL_METHODS.LIST_PROJECTS](
        @Context('environment') environment: Environment,
        @Args() _env: EnvironmentArgs,
        @Args() {filters}: FilterArgs,
    ): Promise<List<Project>> {
        const projects = await environment.projects.list(filters);

        return projects.mapEach((project) => ({
            ...project,
            files: [],
        }));
    }
Example #7
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 #8
Source File: project.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Mutation(() => Project)
    async [PUBLIC_GRAPHQL_METHODS.INIT_PROJECT](
        @Context('environment') environment: Environment,
        @Args() {name}: InitProjectArgs,
    ): Promise<Project> {
        const project = await environment.projects.create({
            name,
            dbmss: [],
        });

        return {
            ...project,
            files: [],
        };
    }
Example #9
Source File: form.create.mutation.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@Mutation(() => FormModel)
  @Roles('admin')
  async createForm(
    @User() user: UserEntity,
    @Args({ name: 'form', type: () => FormCreateInput }) input: FormCreateInput,
    @Context('cache') cache: ContextCache,
  ): Promise<FormModel> {
    const form = await this.createService.create(user, input)

    cache.add(cache.getCacheKey(FormEntity.name, form.id), form)

    return new FormModel(this.idService.encode(form.id), form)
  }
Example #10
Source File: project.resolver.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
@Mutation(() => RelateFile)
    async [PUBLIC_GRAPHQL_METHODS.ADD_PROJECT_FILE](
        @Context('environment') environment: Environment,
        @Args() {name, fileUpload, destination, overwrite}: AddProjectFileArgs,
    ): Promise<RelateFile> {
        const {filename, createReadStream} = await fileUpload;
        const uploadedPath = await this.systemProvider.handleFileUpload(filename, createReadStream());

        return environment.projects.addFile(name, uploadedPath, destination, overwrite);
    }
Example #11
Source File: user.list.query.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@Query(() => UserPagerModel)
  @Roles('superuser')
  async listUsers(
    @Args('start', {type: () => Int, defaultValue: 0, nullable: true}) start: number,
    @Args('limit', {type: () => Int, defaultValue: 50, nullable: true}) limit: number,
    @Context('cache') cache: ContextCache,
  ): Promise<UserPagerModel> {
    const [entities, total] = await this.userService.find(start, limit)

    return new UserPagerModel(
      entities.map(entity => {
        cache.add(cache.getCacheKey(UserEntity.name, entity.id), entity)
        return new UserModel(this.idService.encode(entity.id), entity)
      }),
      total,
      limit,
      start,
    )
  }
Example #12
Source File: user.resolver.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
@Mutation(() => UserType)
  createUser(
    @Args({ name: 'input', type: () => CreateUserInput }) data: CreateUserInput,
    @Context() ctx: MercuriusContext,
  ) {
    const user = this.userService.create(data);

    return user;
  }
Example #13
Source File: user.update.mutation.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@Mutation(() => UserModel)
  @Roles('superuser')
  async updateUser(
    @User() auth: UserEntity,
    @Args({ name: 'user', type: () => UserUpdateInput }) input: UserUpdateInput,
    @Context('cache') cache: ContextCache,
  ): Promise<UserModel> {
    if (auth.id.toString() === input.id) {
      throw new Error('cannot update your own user')
    }

    const user = await this.userService.findById(this.idService.decode(input.id))

    await this.updateService.update(user, input)

    cache.add(cache.getCacheKey(UserEntity.name, user.id), user)

    return new UserModel(this.idService.encode(user.id), user)
  }
Example #14
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 #15
Source File: submission.resolver.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@ResolveField(() => [SubmissionFieldModel])
  async fields(
    @User() user: UserEntity,
    @Parent() parent: SubmissionModel,
    @Context('cache') cache: ContextCache,
  ): Promise<SubmissionFieldModel[]> {
    const submission = await cache.get<SubmissionEntity>(
      cache.getCacheKey(SubmissionEntity.name, parent._id)
    )

    return submission.fields.map(field => {
      cache.add(cache.getCacheKey(SubmissionFieldEntity.name, field.id), field)
      return new SubmissionFieldModel(this.idService.encode(field.id), field)
    })
  }
Example #16
Source File: auth.resolver.ts    From NextJS-NestJS-GraphQL-Starter with MIT License 5 votes vote down vote up
@Query(() => User)
  async gitHubAuth(@Args('input') input: SocialAuthInput, @Context() context) {
    return this.authService.githubAuth(input, context);
  }
Example #17
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 #18
Source File: db.resolver.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
@Mutation(() => String)
    async [PUBLIC_GRAPHQL_METHODS.LOAD_DB](
        @Context('environment') environment: Environment,
        @Args() {dbmsId, database, from, force, javaPath}: LoadDbArgs,
    ): Promise<string> {
        return environment.dbs.load(dbmsId, database, from, force, javaPath);
    }
Example #19
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 #20
Source File: dbms-plugins.resolver.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
@Mutation(() => [DbmsPluginSource])
    async [PUBLIC_GRAPHQL_METHODS.ADD_DBMS_PLUGIN_SOURCES](
        @Context('environment') environment: Environment,
        @Args() _env: EnvironmentArgs,
        @Args() {sources}: AddDbmsPluginSources,
    ): Promise<List<IDbmsPluginSource>> {
        return environment.dbmsPlugins.addSources(sources);
    }