How to use the @nestjs/graphql.Resolver function in @nestjs/graphql

To help you get started, we’ve selected a few @nestjs/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 juicycleff / ultimate-backend / libs / nestjs-graphql-gateway / src / services / references-explorer.service.spec.ts View on Github external
/* tslint:disable:max-classes-per-file */
import { INestApplicationContext, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { Resolver } from '@nestjs/graphql';
import { GRAPHQL_MODULE_OPTIONS } from '@nestjs/graphql/dist/graphql.constants';
import { ResolveReference } from '../decorators';
import { ReferencesExplorerService } from './references-explorer.service';

@Resolver()
class AccountsResolver {
  @Resolver('User')
  @ResolveReference()
  // tslint:disable-next-line
  public user() {}
}

@Module({
  providers: [
    {
      provide: GRAPHQL_MODULE_OPTIONS,
      useValue: {},
    },
    AccountsResolver,
    MetadataScanner,
    ReferencesExplorerService,
  ],
})
github chnirt / nestjs-graphql-best-practice / src / resolvers / message.resolver.ts View on Github external
Resolver,
	Mutation,
	Args,
	Query,
	Context,
	Subscription,
} from '@nestjs/graphql'
import { getMongoRepository } from 'typeorm'
import { ForbiddenError } from 'apollo-server-core'

import { Message, Room, User } from '../models'
import { CreateMessageInput } from '../generator/graphql.schema'

import { MESSAGES_SUBSCRIPTION } from '@environments'

@Resolver('Message')
export class MessageResolver {
	@Query()
	async messages(@Args('roomId') roomId: string): Promise {
		return getMongoRepository(Message).find({
			where: { roomId },
			cache: true,
		})
	}

	@Mutation()
	async sendMessage(
		@Args('input') input: CreateMessageInput,
		@Context('currentUser') currentUser: User,
		@Context('pubsub') pubsub: any
	): Promise {
		const { roomId } = input
github vendure-ecommerce / vendure / packages / core / src / api / resolvers / admin / facet.resolver.ts View on Github external
QueryFacetsArgs,
} from '@vendure/common/lib/generated-types';
import { PaginatedList } from '@vendure/common/lib/shared-types';

import { DEFAULT_LANGUAGE_CODE } from '../../../common/constants';
import { EntityNotFoundError } from '../../../common/error/errors';
import { Translated } from '../../../common/types/locale-types';
import { FacetValue } from '../../../entity/facet-value/facet-value.entity';
import { Facet } from '../../../entity/facet/facet.entity';
import { FacetValueService } from '../../../service/services/facet-value.service';
import { FacetService } from '../../../service/services/facet.service';
import { RequestContext } from '../../common/request-context';
import { Allow } from '../../decorators/allow.decorator';
import { Ctx } from '../../decorators/request-context.decorator';

@Resolver('Facet')
export class FacetResolver {
    constructor(private facetService: FacetService, private facetValueService: FacetValueService) {}

    @Query()
    @Allow(Permission.ReadCatalog)
    facets(
        @Ctx() ctx: RequestContext,
        @Args() args: QueryFacetsArgs,
    ): Promise>> {
        return this.facetService.findAll(ctx.languageCode, args.options || undefined);
    }

    @Query()
    @Allow(Permission.ReadCatalog)
    async facet(
        @Ctx() ctx: RequestContext,
github visual-knight / platform-community-edition / apps / api / src / app / user / user.resolver.ts View on Github external
import { Resolver, Mutation, Args, Query } from '@nestjs/graphql';
import { UserService } from './user.service';
import { User as PrismaUser, Role } from '@generated/photonjs';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '../auth/guards/auth.guard';
import { CurrentUser } from '../shared/decorators/current-user.decorator';
import { UserType } from './models/user';
import { UpdateUserInput } from './dto/update-user.input';
import { AuthPayload } from '../auth/models/auth-payload';

@Resolver(of => UserType)
export class UserResolver {
  constructor(private userService: UserService) {}

  @Mutation(returns => UserType)
  @UseGuards(GqlAuthGuard)
  async deleteUser(@Args('id') id: string, @CurrentUser() user: PrismaUser): Promise {
    if (user.role !== Role.ADMIN) {
      throw new Error('Only admins are allowed to delete users');
    }
    return this.userService.deleteUser(user, id);
  }

  @Mutation(returns => UserType)
  @UseGuards(GqlAuthGuard)
  async updateUser(@Args('data') data: UpdateUserInput, @CurrentUser() user: PrismaUser): Promise {
    return this.userService.updateUser(user, data);
github chnirt / nestjs-graphql-best-practice / src / resolvers / siteShop / siteShop.resolver.ts View on Github external
import { Resolver, Query, Args, Mutation } from '@nestjs/graphql'
import { InjectRepository } from '@nestjs/typeorm'
import { MongoRepository } from 'typeorm'
import { ApolloError } from 'apollo-server-core'
import { SiteShop } from '../../models'
import { SiteShopResponse } from '../../graphql.schema'

@Resolver('siteShop')
export class SiteShopResolver {
	constructor(
		@InjectRepository(SiteShop)
		private readonly siteShopRepository: MongoRepository
	) {}

	@Query('siteShopsBySiteId')
	async getSiteShopsBySiteId(
		@Args('siteId') siteId: string
	): Promise {
		try {
			return await this.siteShopRepository
				.aggregate([
					{
						$match: {
							siteId
github jmcdo29 / zeldaPlay / apps / api / src / app / auth / user / user.resolver.ts View on Github external
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Observable } from 'rxjs';
import { GqlAuthGuard } from '../../guards/gql-auth-guard.guard';
import { returnString } from '../../models';
import {
  ofUser,
  returnUser,
  UserDTO,
  UserIdDTO,
  UserUpdateDataDTO,
} from './models';
import { UserService } from './user.service';

@Resolver(ofUser)
export class UserResolver {
  constructor(private readonly userService: UserService) {}

  @Query(returnUser, { name: 'user' })
  getUser(@Args() userId: UserIdDTO): Observable {
    return this.userService.getById(userId);
  }

  @UseGuards(GqlAuthGuard)
  @Mutation(returnUser, { name: 'updateUser' })
  updateUser(
    @Args('updateUserData') userData: UserUpdateDataDTO,
  ): Observable {
    return this.userService.updateUser(userData);
  }
github chnirt / nestjs-graphql-best-practice / src / resolvers / translate.resolver.ts View on Github external
import { Resolver, Mutation, Args } from '@nestjs/graphql'

import { translate } from '@shared'

@Resolver('Translate')
export class TranslateResolver {
	@Mutation()
	async translate(
		@Args('text') text: string,
		@Args('code') code: string
	): Promise {
		return await translate(text, code)
	}
}
github chnirt / nestjs-graphql-best-practice / src / resolvers / site / site.resolver.ts View on Github external
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql'
import { Site } from '../../models'
import { CreateSiteInput, UpdateSiteInput } from '../../graphql.schema'
import { InjectRepository } from '@nestjs/typeorm'
import { MongoRepository } from 'typeorm'
import { ApolloError } from 'apollo-server-core'

@Resolver('Site')
export class SiteResolver {
	constructor(
		@InjectRepository(Site)
		private readonly siteRepository: MongoRepository
	) {}

	@Query(() => [Site])
	async sites(): Promise {
		return this.siteRepository.find({
			cache: true
		})
	}

	@Query(() => [Site])
	async sitesByIds(@Args('ids') ids: string[]): Promise {
		const convertIds = await ids.map(item => {
github nestjs / nest / sample / 12-graphql-apollo / src / cats / cats.resolvers.ts View on Github external
import { ParseIntPipe, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { Cat } from '../graphql.schema';
import { CatsGuard } from './cats.guard';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';

const pubSub = new PubSub();

@Resolver('Cat')
export class CatsResolvers {
  constructor(private readonly catsService: CatsService) {}

  @Query()
  @UseGuards(CatsGuard)
  async getCats() {
    return await this.catsService.findAll();
  }

  @Query('cat')
  async findOneById(
    @Args('id', ParseIntPipe)
    id: number,
  ): Promise {
    return await this.catsService.findOneById(id);
  }