Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { BaseContext, BaseResolver, StandardDeleteResponse } from '../../../src';
import {
PostCreateInput,
PostUpdateArgs,
PostWhereArgs,
PostWhereInput,
PostWhereUniqueInput
} from '../generated';
import { Post } from './post.model';
import { User } from './user.model';
// Note: we have to specify `Post` here instead of (() => Post) because for some reason this
// changes the object reference when it's trying to add the FieldResolver and things break
@Resolver(Post)
export class PostResolver extends BaseResolver {
constructor(@InjectRepository(Post) public readonly postRepository: Repository) {
super(Post, postRepository);
}
@FieldResolver(() => User)
user(@Root() post: Post, @Ctx() ctx: BaseContext): Promise {
return ctx.dataLoader.loaders.Post.user.load(post);
}
@Query(() => [Post])
async posts(@Args() { where, orderBy, limit, offset }: PostWhereArgs): Promise {
return this.find(where, orderBy, limit, offset);
}
@Query(() => Post)
import { ResolverContext } from "../../lib/types"
import { createToken, decryptToken } from "../../lib/jwt"
import { User } from "./user.entity"
import { UserService } from "./user.service"
import { UserMailer } from "./user.mailer"
import { RegisterInput } from "./inputs/register.input"
import { LoginInput } from "./inputs/login.input"
import { UpdateInput } from "./inputs/update.input"
import { ResetPasswordInput } from "./inputs/resetPassword.input"
import { UserRepository } from "./user.repository"
import { CurrentUser } from "../shared/middleware/currentUser"
import { SlackService } from "../shared/slack.service"
@Resolver(() => User)
export class UserResolver {
constructor(
private readonly userService: UserService,
private readonly userRepository: UserRepository,
private readonly userMailer: UserMailer,
private readonly slackService: SlackService,
) {}
// ME
@Authorized()
@Query(() => User, { nullable: true })
me(@CurrentUser() currentUser: User): Promise {
return this.userRepository.findById(currentUser.id)
}
// REGISTER
import { BaseContext, Fields, StandardDeleteResponse, UserId } from '../../../src';
import {
DishCreateInput,
DishCreateManyArgs,
DishUpdateArgs,
DishWhereArgs,
DishWhereInput,
DishWhereUniqueInput
} from '../../generated';
import { KitchenSink } from '../kitchen-sink/kitchen-sink.model';
import { Dish } from './dish.model';
import { DishService } from './dish.service';
@Resolver(Dish)
export class DishResolver {
constructor(@Inject('DishService') public readonly service: DishService) {}
@Authorized('kitchenSink:read')
@FieldResolver(() => KitchenSink)
kitchenSink(@Root() dish: Dish, @Ctx() ctx: BaseContext): Promise {
return ctx.dataLoader.loaders.Dish.kitchenSink.load(dish);
}
@Authorized('dish:read')
@Query(() => [Dish])
async dishes(
@Args() { where, orderBy, limit, offset }: DishWhereArgs,
@Fields() fields: string[]
): Promise {
return this.service.find(where, orderBy, limit, offset, fields);
import { generateArray } from 'src/mock/util';
import { Arg, Args, ArgsType, Field, FieldResolver, ID, Query, Resolver, Root } from 'type-graphql';
@ArgsType()
class GetPersonsArgs {
@Field({ nullable: true })
searchString: string;
@Field({ nullable: true })
filter: string;
}
let config: Configuration = {
basePath: 'https://demo-phonebook.me/api'
} as any;
@Resolver(Person)
export class PersonResolver {
@Query(() => [Person])
async getPersons(@Args() args: GetPersonsArgs): Promise {
console.log(args);
return from(new DefaultApi(config).personsGet())
.pipe(
map(res => res.data),
map(a =>
a.map((p: any) => {
let person: Person = {
id: p.Id || '',
type: p.Type || '',
title: p.Title || '',
lastName: p.Surname || '',
firstName: p.Firstname || '',
role: p.Role || '',
import * as remarkPing from "remark-ping";
import * as remark2rehype from "remark-rehype";
import { Arg, Ctx, Mutation, Resolver, UseMiddleware } from "type-graphql";
import { In, Repository } from "typeorm";
import { InjectRepository } from "typeorm-typedi-extensions";
import * as unified from "unified";
import { Comment } from "../../entity/Comment";
import { Question } from "../../entity/Question";
import { QuestionCommentNotification } from "../../entity/QuestionCommentNotification";
import { User } from "../../entity/User";
import { MyContext } from "../../types/Context";
import { isAuthenticated } from "../shared/middleware/isAuthenticated";
import { CreateCommentInput } from "./createInput";
import { CommentResponse } from "./response";
@Resolver(Comment)
export class CommentResolver {
@InjectRepository(Comment)
private readonly commentRepo: Repository;
@InjectRepository(User)
private readonly userRepo: Repository;
@InjectRepository(Question)
private readonly questionRepo: Repository;
@InjectRepository(QuestionCommentNotification)
private readonly questionCommentNotificationRepo: Repository<
QuestionCommentNotification
>;
@Mutation(() => CommentResponse)
@UseMiddleware(isAuthenticated)
async createComment(
@Arg("comment") input: CreateCommentInput,
import { Repository } from 'typeorm';
import { InjectRepository } from 'typeorm-typedi-extensions';
import { User } from '../../../entities/User';
import { UserMeta } from '../../../entities/UserMeta';
import { Context } from '../../../interfaces/Context';
import { processWebhooks } from '../../../utils/processWebhooks';
import { UserMetaRepository } from '../repositories/UserMetaRepository';
import { UserRepository } from '../repositories/UserRepository';
import { ConnectionArgs, createConnectionType } from '../types/createConnectionType';
import { UpdateUserInput } from '../types/UpdateUserInput';
import { Authorized } from '../utils/Authorized';
import { ExtendedConnection } from '../utils/ExtendedConnection';
const UserConnection = createConnectionType(User);
@Resolver(of => User)
export class UserResolver {
@InjectRepository(UserRepository)
private readonly userRepository: UserRepository;
@InjectRepository(UserMetaRepository)
private readonly userMetaRepository: UserMetaRepository;
@InjectRepository(UserEmail)
private readonly userEmailRepository: Repository;
@Authorized()
@Query(returns => User)
public User(
@Arg('id', type => ID) id: string //
) {
return this.userRepository.findOneOrFail(id);
import { InjectRepository } from 'typeorm-typedi-extensions';
import { User } from 'src/entity/User';
import { validate } from 'class-validator';
@ArgsType()
class AddUserArgs {
@Field()
email: string;
}
@ArgsType()
class DeleteUserAtgs {
@Field(type => ID)
id: string;
}
@Resolver(User)
export class UserResolver {
@InjectRepository(User)
private readonly userRepo: Repository;
@Query(() => [User])
async users(): Promise {
return this.userRepo.find();
}
@Mutation(() => User)
async addUser(@Args() { email }: AddUserArgs): Promise {
try {
const user = this.userRepo.create({ email });
const errors = await validate(user);
if (errors.length > 0) {
throw new Error();
Root,
UseMiddleware,
} from "type-graphql";
import { FindConditions, getConnection, IsNull, Repository } from "typeorm";
import { InjectRepository } from "typeorm-typedi-extensions";
import { Comment } from "../../entity/Comment";
import { Question } from "../../entity/Question";
import { DisplayError } from "../../errors/DisplayError";
import { QuestionRepository } from "../../repositories/QuestionRepo";
import { MyContext } from "../../types/Context";
import { isAuthenticated } from "../shared/middleware/isAuthenticated";
import { CreateQuestionInput } from "./createInput";
import { QuestionResponse } from "./response";
const PAGE_SIZE = 6;
@Resolver(Question)
export class QuestionResolver {
@InjectRepository(QuestionRepository)
private readonly questionRepo: QuestionRepository;
@InjectRepository(Comment)
private readonly commentRepo: Repository;
@FieldResolver()
numComments(@Root() root: Question): Promise {
return this.commentRepo.count({ where: { questionId: root.id } });
}
@Mutation(() => QuestionResponse)
@UseMiddleware(isAuthenticated)
async createQuestion(
@Arg("question") input: CreateQuestionInput,
@Ctx() ctx: MyContext
Resolver,
Query,
Mutation,
Arg,
PubSub,
Publisher,
Subscription,
Root,
ResolverFilterData,
ObjectType,
Field,
ID,
} from 'type-graphql';
import { Notification, NotificationPayload } from '../type/notification';
@Resolver()
export class SampleResolver {
private autoIncrement = 0;
@Query(() => Date)
currentDate() {
return new Date();
}
@Mutation(() => Boolean)
async pubSubMutation(
@PubSub() pubSub: PubSubEngine,
@Arg('message', { nullable: true }) message?: string,
): Promise {
const payload: NotificationPayload = { id: ++this.autoIncrement, message };
await pubSub.publish('NOTIFICATIONS', payload);
return true;
select: {
uuid: true,
author: args,
},
});
return keys
.map(key => fetchedData.find(data => data.uuid === key)!)
.map(data => data.author);
});
argsToDataLoaderMap.set(argsJSON, postAuthorDataLoader);
}
return postAuthorDataLoader;
}
}
@Resolver(_of => Post)
export class PostRelationsResolver {
@FieldResolver(_type => User, {
nullable: false,
description: undefined,
})
async author(@Root() post: Post, @Ctx() ctx: any): Promise {
ctx.getPostAuthorDataLoader = ctx.getPostAuthorDataLoader || createGetPostAuthorDataLoader(ctx.photon);
return ctx.getPostAuthorDataLoader({}).load(post.uuid);
}
}