Запрос c оператором WHERE nestjs/graphql/typeorm?
Проект реализуется на vue/graphql/nestjs/typeorm.
В БД mysql есть таблица пользователей:
mysql> select * from user;
+------+---------+------------+---------+------+---------+------------+----------+------+------+
| name | surname | patronymic | isAdmin | area | user_id | phone | password | role | note |
+------+---------+------------+---------+------+---------+------------+----------+------+------+
| John | Conr | | 1 | 1 | 17 | 9200138288 | 123 | | |
| Bob | Glover | | 0 | 12 | 18 | 9305556666 | 1234 | | |
+------+---------+------------+---------+------+---------+------------+----------+------+------+
Nestjs:
user.entity.ts:
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
@ObjectType()
export class User {
@PrimaryGeneratedColumn({
type: 'bigint',
name: 'user_id',
})
@Field((type) => Int, { nullable: true })
id: number;
@Column({ nullable: true })
@Field({ nullable: true })
name: string;
@Column({ nullable: true })
@Field({ nullable: true })
surname: string;
@Column({ nullable: true })
@Field({ nullable: true })
patronymic: string;
@Column({ nullable: true })
@Field({ nullable: true })
isAdmin: boolean;
@Column({ nullable: true })
@Field({ nullable: true })
area: string;
@Column({ nullable: true })
@Field({ nullable: true })
phone: string;
@Column({ nullable: true })
@Field({ nullable: true })
password: string;
@Column({ nullable: true })
@Field({ nullable: true })
role: string;
@Column({ nullable: true })
@Field({ nullable: true })
note: string;
}
user.servise.ts:
найти пользователя, по полю "phone", используя оператор "where":
import { Injectable } from '@nestjs/common';
import { User } from './entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { FindManyOptions, In, Repository } from 'typeorm';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User) private usersRepository: Repository<User>,
) {}
async findOneByPhone(phone: string): Promise<User[]> {
return this.usersRepository.find({
where: {
phone: phone,
},
});
}
}
user.resolver.ts:
import { Resolver, Query, Mutation, Args, Int } from '@nestjs/graphql';
import { UsersService } from './users.service';
import { User } from './entities/user.entity';
import { CreateUserInput } from './dto/create-user.input';
import { UpdateUserInput } from './dto/update-user.input';
@Resolver(() => User)
export class UsersResolver {
constructor(private readonly usersService: UsersService) {}
@Query(() => User, { name: 'userByPhone' })
findByPhone(@Args('phone', { type: () => String }) phone: string) {
return this.usersService.findOneByPhone(phone);
}
}
Фронт вызывает grqphql-запрос:
query findByPhone($phone: String!) {
userByPhone(phone: $phone) {
id
name
surname
patronymic
area
phone
isAdmin
password
role
note
}
}
с необходимыми данными:
{
"phone": "9200138288"
}
Запрос отрабатывает со статусом 200, но возвращает все данные с null:
{
"data": {
"userByPhone": {
"id": null,
"name": null,
"surname": null,
"patronymic": null,
"area": null,
"phone": null,
"isAdmin": null,
"password": null,
"role": null,
"note": null
}
}
}
Выполняя msql-запрос напрямую, в терминале, пользователя возвращает как надо:
mysql> select * from user where phone = '9200138288';
+------+---------+------------+---------+------+---------+------------+----------+------+------+
| name | surname | patronymic | isAdmin | area | user_id | phone | password | role | note |
+------+---------+------------+---------+------+---------+------------+----------+------+------+
| John | Conr | | 1 | 1 | 17 | 9200138288 | 123 | | |
+------+---------+------------+---------+------+---------+------------+----------+------+------+
Где моя ошибка? Почему grqphql-запрос возвращает null? Заранее благодарен.